xref: /openbmc/linux/net/8021q/vlan.c (revision ad1afb00393915a51c21b1ae8704562bf036855f)
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 
214fc268d2SRandy Dunlap #include <linux/capability.h>
221da177e4SLinus Torvalds #include <linux/module.h>
231da177e4SLinus Torvalds #include <linux/netdevice.h>
241da177e4SLinus Torvalds #include <linux/skbuff.h>
255a0e3ad6STejun Heo #include <linux/slab.h>
261da177e4SLinus Torvalds #include <linux/init.h>
2782524746SFranck Bui-Huu #include <linux/rculist.h>
281da177e4SLinus Torvalds #include <net/p8022.h>
291da177e4SLinus Torvalds #include <net/arp.h>
301da177e4SLinus Torvalds #include <linux/rtnetlink.h>
311da177e4SLinus Torvalds #include <linux/notifier.h>
3261362766SPatrick McHardy #include <net/rtnetlink.h>
33e9dc8653SEric W. Biederman #include <net/net_namespace.h>
34d9ed0f0eSPavel Emelyanov #include <net/netns/generic.h>
3561362766SPatrick McHardy #include <asm/uaccess.h>
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds #include <linux/if_vlan.h>
381da177e4SLinus Torvalds #include "vlan.h"
391da177e4SLinus Torvalds #include "vlanproc.h"
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #define DRV_VERSION "1.8"
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds /* Global VLAN variables */
441da177e4SLinus Torvalds 
45f99189b1SEric Dumazet int vlan_net_id __read_mostly;
46d9ed0f0eSPavel Emelyanov 
471da177e4SLinus Torvalds /* Our listing of VLAN group(s) */
481da177e4SLinus Torvalds static struct hlist_head vlan_group_hash[VLAN_GRP_HASH_SIZE];
491da177e4SLinus Torvalds 
50b3020061SStephen Hemminger const char vlan_fullname[] = "802.1Q VLAN Support";
51b3020061SStephen Hemminger const char vlan_version[] = DRV_VERSION;
52b3020061SStephen Hemminger static const char vlan_copyright[] = "Ben Greear <greearb@candelatech.com>";
53b3020061SStephen Hemminger static const char vlan_buggyright[] = "David S. Miller <davem@redhat.com>";
541da177e4SLinus Torvalds 
557546dd97SStephen Hemminger static struct packet_type vlan_packet_type __read_mostly = {
5609640e63SHarvey Harrison 	.type = cpu_to_be16(ETH_P_8021Q),
571da177e4SLinus Torvalds 	.func = vlan_skb_recv, /* VLAN receive method */
581da177e4SLinus Torvalds };
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds /* End of global variables definitions. */
611da177e4SLinus Torvalds 
622029cc2cSPatrick McHardy static inline unsigned int vlan_grp_hashfn(unsigned int idx)
632029cc2cSPatrick McHardy {
642029cc2cSPatrick McHardy 	return ((idx >> VLAN_GRP_HASH_SHIFT) ^ idx) & VLAN_GRP_HASH_MASK;
652029cc2cSPatrick McHardy }
662029cc2cSPatrick McHardy 
671da177e4SLinus Torvalds /* Must be invoked with RCU read lock (no preempt) */
68a9fde260SPavel Emelyanov static struct vlan_group *__vlan_find_group(struct net_device *real_dev)
691da177e4SLinus Torvalds {
701da177e4SLinus Torvalds 	struct vlan_group *grp;
711da177e4SLinus Torvalds 	struct hlist_node *n;
72a9fde260SPavel Emelyanov 	int hash = vlan_grp_hashfn(real_dev->ifindex);
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds 	hlist_for_each_entry_rcu(grp, n, &vlan_group_hash[hash], hlist) {
75a9fde260SPavel Emelyanov 		if (grp->real_dev == real_dev)
761da177e4SLinus Torvalds 			return grp;
771da177e4SLinus Torvalds 	}
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds 	return NULL;
801da177e4SLinus Torvalds }
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds /*  Find the protocol handler.  Assumes VID < VLAN_VID_MASK.
831da177e4SLinus Torvalds  *
841da177e4SLinus Torvalds  * Must be invoked with RCU read lock (no preempt)
851da177e4SLinus Torvalds  */
869bb8582eSPatrick McHardy struct net_device *__find_vlan_dev(struct net_device *real_dev, u16 vlan_id)
871da177e4SLinus Torvalds {
88a9fde260SPavel Emelyanov 	struct vlan_group *grp = __vlan_find_group(real_dev);
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 	if (grp)
919bb8582eSPatrick McHardy 		return vlan_group_get_device(grp, vlan_id);
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 	return NULL;
941da177e4SLinus Torvalds }
951da177e4SLinus Torvalds 
965c15bdecSDan Aloni static void vlan_group_free(struct vlan_group *grp)
975c15bdecSDan Aloni {
985c15bdecSDan Aloni 	int i;
995c15bdecSDan Aloni 
1005c15bdecSDan Aloni 	for (i = 0; i < VLAN_GROUP_ARRAY_SPLIT_PARTS; i++)
1015c15bdecSDan Aloni 		kfree(grp->vlan_devices_arrays[i]);
1025c15bdecSDan Aloni 	kfree(grp);
1035c15bdecSDan Aloni }
1045c15bdecSDan Aloni 
105a9fde260SPavel Emelyanov static struct vlan_group *vlan_group_alloc(struct net_device *real_dev)
10642429aaeSPatrick McHardy {
10742429aaeSPatrick McHardy 	struct vlan_group *grp;
10842429aaeSPatrick McHardy 
10942429aaeSPatrick McHardy 	grp = kzalloc(sizeof(struct vlan_group), GFP_KERNEL);
11042429aaeSPatrick McHardy 	if (!grp)
11142429aaeSPatrick McHardy 		return NULL;
11242429aaeSPatrick McHardy 
113a9fde260SPavel Emelyanov 	grp->real_dev = real_dev;
11442429aaeSPatrick McHardy 	hlist_add_head_rcu(&grp->hlist,
115a9fde260SPavel Emelyanov 			&vlan_group_hash[vlan_grp_hashfn(real_dev->ifindex)]);
11642429aaeSPatrick McHardy 	return grp;
11767727184SPavel Emelyanov }
11842429aaeSPatrick McHardy 
1199bb8582eSPatrick McHardy static int vlan_group_prealloc_vid(struct vlan_group *vg, u16 vlan_id)
12067727184SPavel Emelyanov {
12167727184SPavel Emelyanov 	struct net_device **array;
12267727184SPavel Emelyanov 	unsigned int size;
12367727184SPavel Emelyanov 
12467727184SPavel Emelyanov 	ASSERT_RTNL();
12567727184SPavel Emelyanov 
1269bb8582eSPatrick McHardy 	array = vg->vlan_devices_arrays[vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
12767727184SPavel Emelyanov 	if (array != NULL)
12867727184SPavel Emelyanov 		return 0;
12967727184SPavel Emelyanov 
13067727184SPavel Emelyanov 	size = sizeof(struct net_device *) * VLAN_GROUP_ARRAY_PART_LEN;
13167727184SPavel Emelyanov 	array = kzalloc(size, GFP_KERNEL);
13267727184SPavel Emelyanov 	if (array == NULL)
13367727184SPavel Emelyanov 		return -ENOBUFS;
13467727184SPavel Emelyanov 
1359bb8582eSPatrick McHardy 	vg->vlan_devices_arrays[vlan_id / VLAN_GROUP_ARRAY_PART_LEN] = array;
13667727184SPavel Emelyanov 	return 0;
13742429aaeSPatrick McHardy }
13842429aaeSPatrick McHardy 
1391da177e4SLinus Torvalds static void vlan_rcu_free(struct rcu_head *rcu)
1401da177e4SLinus Torvalds {
1415c15bdecSDan Aloni 	vlan_group_free(container_of(rcu, struct vlan_group, rcu));
1421da177e4SLinus Torvalds }
1431da177e4SLinus Torvalds 
14423289a37SEric Dumazet void unregister_vlan_dev(struct net_device *dev, struct list_head *head)
1451da177e4SLinus Torvalds {
1469dfebcc6SPatrick McHardy 	struct vlan_dev_info *vlan = vlan_dev_info(dev);
147af301517SPatrick McHardy 	struct net_device *real_dev = vlan->real_dev;
148656299f7SStephen Hemminger 	const struct net_device_ops *ops = real_dev->netdev_ops;
1491da177e4SLinus Torvalds 	struct vlan_group *grp;
1509bb8582eSPatrick McHardy 	u16 vlan_id = vlan->vlan_id;
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	ASSERT_RTNL();
1531da177e4SLinus Torvalds 
154a9fde260SPavel Emelyanov 	grp = __vlan_find_group(real_dev);
155af301517SPatrick McHardy 	BUG_ON(!grp);
156acc5efbcSPatrick McHardy 
157acc5efbcSPatrick McHardy 	/* Take it out of our own structures, but be sure to interlock with
158*ad1afb00SPedro Garcia 	 * HW accelerating devices or SW vlan input packet processing if
159*ad1afb00SPedro Garcia 	 * VLAN is not 0 (leave it there for 802.1p).
1601da177e4SLinus Torvalds 	 */
161*ad1afb00SPedro Garcia 	if (vlan_id && (real_dev->features & NETIF_F_HW_VLAN_FILTER))
162656299f7SStephen Hemminger 		ops->ndo_vlan_rx_kill_vid(real_dev, vlan_id);
1631da177e4SLinus Torvalds 
164af301517SPatrick McHardy 	grp->nr_vlans--;
165af301517SPatrick McHardy 
16663c8099dSEric Dumazet 	vlan_group_set_device(grp, vlan_id, NULL);
16729906f6aSPatrick McHardy 	if (!grp->killall)
1681da177e4SLinus Torvalds 		synchronize_net();
16929906f6aSPatrick McHardy 
17023289a37SEric Dumazet 	unregister_netdevice_queue(dev, head);
171ce305002SPatrick McHardy 
172acc5efbcSPatrick McHardy 	/* If the group is now empty, kill off the group. */
173af301517SPatrick McHardy 	if (grp->nr_vlans == 0) {
17470c03b49SPatrick McHardy 		vlan_gvrp_uninit_applicant(real_dev);
17570c03b49SPatrick McHardy 
1761da177e4SLinus Torvalds 		if (real_dev->features & NETIF_F_HW_VLAN_RX)
177656299f7SStephen Hemminger 			ops->ndo_vlan_rx_register(real_dev, NULL);
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 		hlist_del_rcu(&grp->hlist);
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 		/* Free the group, after all cpu's are done. */
1821da177e4SLinus Torvalds 		call_rcu(&grp->rcu, vlan_rcu_free);
1831da177e4SLinus Torvalds 	}
1841da177e4SLinus Torvalds 
185af301517SPatrick McHardy 	/* Get rid of the vlan's reference to real_dev */
186af301517SPatrick McHardy 	dev_put(real_dev);
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds 
1899bb8582eSPatrick McHardy int vlan_check_real_dev(struct net_device *real_dev, u16 vlan_id)
190c1d3ee99SPatrick McHardy {
191656299f7SStephen Hemminger 	const char *name = real_dev->name;
192656299f7SStephen Hemminger 	const struct net_device_ops *ops = real_dev->netdev_ops;
19340f98e1aSPatrick McHardy 
194c1d3ee99SPatrick McHardy 	if (real_dev->features & NETIF_F_VLAN_CHALLENGED) {
19540f98e1aSPatrick McHardy 		pr_info("8021q: VLANs not supported on %s\n", name);
196c1d3ee99SPatrick McHardy 		return -EOPNOTSUPP;
197c1d3ee99SPatrick McHardy 	}
198c1d3ee99SPatrick McHardy 
199656299f7SStephen Hemminger 	if ((real_dev->features & NETIF_F_HW_VLAN_RX) && !ops->ndo_vlan_rx_register) {
20040f98e1aSPatrick McHardy 		pr_info("8021q: device %s has buggy VLAN hw accel\n", name);
201c1d3ee99SPatrick McHardy 		return -EOPNOTSUPP;
202c1d3ee99SPatrick McHardy 	}
203c1d3ee99SPatrick McHardy 
204c1d3ee99SPatrick McHardy 	if ((real_dev->features & NETIF_F_HW_VLAN_FILTER) &&
205656299f7SStephen Hemminger 	    (!ops->ndo_vlan_rx_add_vid || !ops->ndo_vlan_rx_kill_vid)) {
20640f98e1aSPatrick McHardy 		pr_info("8021q: Device %s has buggy VLAN hw accel\n", name);
207c1d3ee99SPatrick McHardy 		return -EOPNOTSUPP;
208c1d3ee99SPatrick McHardy 	}
209c1d3ee99SPatrick McHardy 
21040f98e1aSPatrick McHardy 	if (__find_vlan_dev(real_dev, vlan_id) != NULL)
211c1d3ee99SPatrick McHardy 		return -EEXIST;
212c1d3ee99SPatrick McHardy 
213c1d3ee99SPatrick McHardy 	return 0;
214c1d3ee99SPatrick McHardy }
215c1d3ee99SPatrick McHardy 
21607b5b17eSPatrick McHardy int register_vlan_dev(struct net_device *dev)
217e89fe42cSPatrick McHardy {
2189dfebcc6SPatrick McHardy 	struct vlan_dev_info *vlan = vlan_dev_info(dev);
219e89fe42cSPatrick McHardy 	struct net_device *real_dev = vlan->real_dev;
220656299f7SStephen Hemminger 	const struct net_device_ops *ops = real_dev->netdev_ops;
2219bb8582eSPatrick McHardy 	u16 vlan_id = vlan->vlan_id;
222e89fe42cSPatrick McHardy 	struct vlan_group *grp, *ngrp = NULL;
223e89fe42cSPatrick McHardy 	int err;
224e89fe42cSPatrick McHardy 
225a9fde260SPavel Emelyanov 	grp = __vlan_find_group(real_dev);
226e89fe42cSPatrick McHardy 	if (!grp) {
227a9fde260SPavel Emelyanov 		ngrp = grp = vlan_group_alloc(real_dev);
228e89fe42cSPatrick McHardy 		if (!grp)
229e89fe42cSPatrick McHardy 			return -ENOBUFS;
23070c03b49SPatrick McHardy 		err = vlan_gvrp_init_applicant(real_dev);
23170c03b49SPatrick McHardy 		if (err < 0)
23270c03b49SPatrick McHardy 			goto out_free_group;
233e89fe42cSPatrick McHardy 	}
234e89fe42cSPatrick McHardy 
23567727184SPavel Emelyanov 	err = vlan_group_prealloc_vid(grp, vlan_id);
23667727184SPavel Emelyanov 	if (err < 0)
23770c03b49SPatrick McHardy 		goto out_uninit_applicant;
23867727184SPavel Emelyanov 
239e89fe42cSPatrick McHardy 	err = register_netdevice(dev);
240e89fe42cSPatrick McHardy 	if (err < 0)
24170c03b49SPatrick McHardy 		goto out_uninit_applicant;
242e89fe42cSPatrick McHardy 
243e89fe42cSPatrick McHardy 	/* Account for reference in struct vlan_dev_info */
244e89fe42cSPatrick McHardy 	dev_hold(real_dev);
245e89fe42cSPatrick McHardy 
246fc4a7489SPatrick Mullaney 	netif_stacked_transfer_operstate(real_dev, dev);
247e89fe42cSPatrick McHardy 	linkwatch_fire_event(dev); /* _MUST_ call rfc2863_policy() */
248e89fe42cSPatrick McHardy 
249e89fe42cSPatrick McHardy 	/* So, got the sucker initialized, now lets place
250e89fe42cSPatrick McHardy 	 * it into our local structure.
251e89fe42cSPatrick McHardy 	 */
252e89fe42cSPatrick McHardy 	vlan_group_set_device(grp, vlan_id, dev);
253af301517SPatrick McHardy 	grp->nr_vlans++;
254af301517SPatrick McHardy 
255e89fe42cSPatrick McHardy 	if (ngrp && real_dev->features & NETIF_F_HW_VLAN_RX)
256656299f7SStephen Hemminger 		ops->ndo_vlan_rx_register(real_dev, ngrp);
257e89fe42cSPatrick McHardy 	if (real_dev->features & NETIF_F_HW_VLAN_FILTER)
258656299f7SStephen Hemminger 		ops->ndo_vlan_rx_add_vid(real_dev, vlan_id);
259e89fe42cSPatrick McHardy 
260e89fe42cSPatrick McHardy 	return 0;
261e89fe42cSPatrick McHardy 
26270c03b49SPatrick McHardy out_uninit_applicant:
26370c03b49SPatrick McHardy 	if (ngrp)
26470c03b49SPatrick McHardy 		vlan_gvrp_uninit_applicant(real_dev);
265e89fe42cSPatrick McHardy out_free_group:
2666b863d1dSEric Dumazet 	if (ngrp) {
2676b863d1dSEric Dumazet 		hlist_del_rcu(&ngrp->hlist);
2686b863d1dSEric Dumazet 		/* Free the group, after all cpu's are done. */
2696b863d1dSEric Dumazet 		call_rcu(&ngrp->rcu, vlan_rcu_free);
2706b863d1dSEric Dumazet 	}
271e89fe42cSPatrick McHardy 	return err;
272e89fe42cSPatrick McHardy }
273e89fe42cSPatrick McHardy 
2741da177e4SLinus Torvalds /*  Attach a VLAN device to a mac address (ie Ethernet Card).
2752ae0bf69SPatrick McHardy  *  Returns 0 if the device was created or a negative error code otherwise.
2761da177e4SLinus Torvalds  */
2779bb8582eSPatrick McHardy static int register_vlan_device(struct net_device *real_dev, u16 vlan_id)
2781da177e4SLinus Torvalds {
2791da177e4SLinus Torvalds 	struct net_device *new_dev;
2807a17a2f7SPavel Emelyanov 	struct net *net = dev_net(real_dev);
2817a17a2f7SPavel Emelyanov 	struct vlan_net *vn = net_generic(net, vlan_net_id);
2821da177e4SLinus Torvalds 	char name[IFNAMSIZ];
2832ae0bf69SPatrick McHardy 	int err;
2841da177e4SLinus Torvalds 
2859bb8582eSPatrick McHardy 	if (vlan_id >= VLAN_VID_MASK)
2862ae0bf69SPatrick McHardy 		return -ERANGE;
2871da177e4SLinus Torvalds 
2889bb8582eSPatrick McHardy 	err = vlan_check_real_dev(real_dev, vlan_id);
2892ae0bf69SPatrick McHardy 	if (err < 0)
2902ae0bf69SPatrick McHardy 		return err;
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	/* Gotta set up the fields for the device. */
2937a17a2f7SPavel Emelyanov 	switch (vn->name_type) {
2941da177e4SLinus Torvalds 	case VLAN_NAME_TYPE_RAW_PLUS_VID:
2951da177e4SLinus Torvalds 		/* name will look like:	 eth1.0005 */
2969bb8582eSPatrick McHardy 		snprintf(name, IFNAMSIZ, "%s.%.4i", real_dev->name, vlan_id);
2971da177e4SLinus Torvalds 		break;
2981da177e4SLinus Torvalds 	case VLAN_NAME_TYPE_PLUS_VID_NO_PAD:
2991da177e4SLinus Torvalds 		/* Put our vlan.VID in the name.
3001da177e4SLinus Torvalds 		 * Name will look like:	 vlan5
3011da177e4SLinus Torvalds 		 */
3029bb8582eSPatrick McHardy 		snprintf(name, IFNAMSIZ, "vlan%i", vlan_id);
3031da177e4SLinus Torvalds 		break;
3041da177e4SLinus Torvalds 	case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD:
3051da177e4SLinus Torvalds 		/* Put our vlan.VID in the name.
3061da177e4SLinus Torvalds 		 * Name will look like:	 eth0.5
3071da177e4SLinus Torvalds 		 */
3089bb8582eSPatrick McHardy 		snprintf(name, IFNAMSIZ, "%s.%i", real_dev->name, vlan_id);
3091da177e4SLinus Torvalds 		break;
3101da177e4SLinus Torvalds 	case VLAN_NAME_TYPE_PLUS_VID:
3111da177e4SLinus Torvalds 		/* Put our vlan.VID in the name.
3121da177e4SLinus Torvalds 		 * Name will look like:	 vlan0005
3131da177e4SLinus Torvalds 		 */
3141da177e4SLinus Torvalds 	default:
3159bb8582eSPatrick McHardy 		snprintf(name, IFNAMSIZ, "vlan%.4i", vlan_id);
3163ff50b79SStephen Hemminger 	}
3171da177e4SLinus Torvalds 
3182e59af3dSEric Dumazet 	new_dev = alloc_netdev_mq(sizeof(struct vlan_dev_info), name,
3192e59af3dSEric Dumazet 				  vlan_setup, real_dev->num_tx_queues);
3205dd8d1e9SArjan van de Ven 
3211da177e4SLinus Torvalds 	if (new_dev == NULL)
3222ae0bf69SPatrick McHardy 		return -ENOBUFS;
3231da177e4SLinus Torvalds 
3242e59af3dSEric Dumazet 	new_dev->real_num_tx_queues = real_dev->real_num_tx_queues;
32565d292a2SPavel Emelyanov 	dev_net_set(new_dev, net);
3261da177e4SLinus Torvalds 	/* need 4 bytes for extra VLAN header info,
3271da177e4SLinus Torvalds 	 * hope the underlying device can handle it.
3281da177e4SLinus Torvalds 	 */
3291da177e4SLinus Torvalds 	new_dev->mtu = real_dev->mtu;
3301da177e4SLinus Torvalds 
3319bb8582eSPatrick McHardy 	vlan_dev_info(new_dev)->vlan_id = vlan_id;
3329dfebcc6SPatrick McHardy 	vlan_dev_info(new_dev)->real_dev = real_dev;
3339dfebcc6SPatrick McHardy 	vlan_dev_info(new_dev)->dent = NULL;
3349dfebcc6SPatrick McHardy 	vlan_dev_info(new_dev)->flags = VLAN_FLAG_REORDER_HDR;
3351da177e4SLinus Torvalds 
33607b5b17eSPatrick McHardy 	new_dev->rtnl_link_ops = &vlan_link_ops;
3372ae0bf69SPatrick McHardy 	err = register_vlan_dev(new_dev);
3382ae0bf69SPatrick McHardy 	if (err < 0)
33942429aaeSPatrick McHardy 		goto out_free_newdev;
3401da177e4SLinus Torvalds 
3412ae0bf69SPatrick McHardy 	return 0;
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds out_free_newdev:
3441da177e4SLinus Torvalds 	free_netdev(new_dev);
3452ae0bf69SPatrick McHardy 	return err;
3461da177e4SLinus Torvalds }
3471da177e4SLinus Torvalds 
3488c979c26SPatrick McHardy static void vlan_sync_address(struct net_device *dev,
3498c979c26SPatrick McHardy 			      struct net_device *vlandev)
3508c979c26SPatrick McHardy {
3519dfebcc6SPatrick McHardy 	struct vlan_dev_info *vlan = vlan_dev_info(vlandev);
3528c979c26SPatrick McHardy 
3538c979c26SPatrick McHardy 	/* May be called without an actual change */
3548c979c26SPatrick McHardy 	if (!compare_ether_addr(vlan->real_dev_addr, dev->dev_addr))
3558c979c26SPatrick McHardy 		return;
3568c979c26SPatrick McHardy 
3578c979c26SPatrick McHardy 	/* vlan address was different from the old address and is equal to
3588c979c26SPatrick McHardy 	 * the new address */
3598c979c26SPatrick McHardy 	if (compare_ether_addr(vlandev->dev_addr, vlan->real_dev_addr) &&
3608c979c26SPatrick McHardy 	    !compare_ether_addr(vlandev->dev_addr, dev->dev_addr))
361a748ee24SJiri Pirko 		dev_uc_del(dev, vlandev->dev_addr);
3628c979c26SPatrick McHardy 
3638c979c26SPatrick McHardy 	/* vlan address was equal to the old address and is different from
3648c979c26SPatrick McHardy 	 * the new address */
3658c979c26SPatrick McHardy 	if (!compare_ether_addr(vlandev->dev_addr, vlan->real_dev_addr) &&
3668c979c26SPatrick McHardy 	    compare_ether_addr(vlandev->dev_addr, dev->dev_addr))
367a748ee24SJiri Pirko 		dev_uc_add(dev, vlandev->dev_addr);
3688c979c26SPatrick McHardy 
3698c979c26SPatrick McHardy 	memcpy(vlan->real_dev_addr, dev->dev_addr, ETH_ALEN);
3708c979c26SPatrick McHardy }
3718c979c26SPatrick McHardy 
3725fb13570SPatrick McHardy static void vlan_transfer_features(struct net_device *dev,
3735fb13570SPatrick McHardy 				   struct net_device *vlandev)
3745fb13570SPatrick McHardy {
3755fb13570SPatrick McHardy 	unsigned long old_features = vlandev->features;
3765fb13570SPatrick McHardy 
377289c79a4SPatrick McHardy 	vlandev->features &= ~dev->vlan_features;
378289c79a4SPatrick McHardy 	vlandev->features |= dev->features & dev->vlan_features;
3791ae4be22SAlexander Duyck 	vlandev->gso_max_size = dev->gso_max_size;
380b85daa53SVasu Dev #if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
381b85daa53SVasu Dev 	vlandev->fcoe_ddp_xid = dev->fcoe_ddp_xid;
382b85daa53SVasu Dev #endif
383f6b9f4b2SVasu Dev 	vlandev->real_num_tx_queues = dev->real_num_tx_queues;
384f6b9f4b2SVasu Dev 	BUG_ON(vlandev->real_num_tx_queues > vlandev->num_tx_queues);
3855fb13570SPatrick McHardy 
3865fb13570SPatrick McHardy 	if (old_features != vlandev->features)
3875fb13570SPatrick McHardy 		netdev_features_change(vlandev);
3885fb13570SPatrick McHardy }
3895fb13570SPatrick McHardy 
390802fb176SPavel Emelyanov static void __vlan_device_event(struct net_device *dev, unsigned long event)
391802fb176SPavel Emelyanov {
392802fb176SPavel Emelyanov 	switch (event) {
393802fb176SPavel Emelyanov 	case NETDEV_CHANGENAME:
394802fb176SPavel Emelyanov 		vlan_proc_rem_dev(dev);
395802fb176SPavel Emelyanov 		if (vlan_proc_add_dev(dev) < 0)
396802fb176SPavel Emelyanov 			pr_warning("8021q: failed to change proc name for %s\n",
397802fb176SPavel Emelyanov 					dev->name);
398802fb176SPavel Emelyanov 		break;
39930688a9aSPavel Emelyanov 	case NETDEV_REGISTER:
40030688a9aSPavel Emelyanov 		if (vlan_proc_add_dev(dev) < 0)
40130688a9aSPavel Emelyanov 			pr_warning("8021q: failed to add proc entry for %s\n",
40230688a9aSPavel Emelyanov 					dev->name);
40330688a9aSPavel Emelyanov 		break;
40430688a9aSPavel Emelyanov 	case NETDEV_UNREGISTER:
40530688a9aSPavel Emelyanov 		vlan_proc_rem_dev(dev);
40630688a9aSPavel Emelyanov 		break;
407802fb176SPavel Emelyanov 	}
408802fb176SPavel Emelyanov }
409802fb176SPavel Emelyanov 
4102029cc2cSPatrick McHardy static int vlan_device_event(struct notifier_block *unused, unsigned long event,
4112029cc2cSPatrick McHardy 			     void *ptr)
4121da177e4SLinus Torvalds {
4131da177e4SLinus Torvalds 	struct net_device *dev = ptr;
414802fb176SPavel Emelyanov 	struct vlan_group *grp;
4151da177e4SLinus Torvalds 	int i, flgs;
4161da177e4SLinus Torvalds 	struct net_device *vlandev;
4175e756593SPatrick McHardy 	struct vlan_dev_info *vlan;
41829906f6aSPatrick McHardy 	LIST_HEAD(list);
4191da177e4SLinus Torvalds 
42081d85346SPatrick McHardy 	if (is_vlan_dev(dev))
421802fb176SPavel Emelyanov 		__vlan_device_event(dev, event);
422802fb176SPavel Emelyanov 
423*ad1afb00SPedro Garcia 	if ((event == NETDEV_UP) &&
424*ad1afb00SPedro Garcia 	    (dev->features & NETIF_F_HW_VLAN_FILTER) &&
425*ad1afb00SPedro Garcia 	    dev->netdev_ops->ndo_vlan_rx_add_vid) {
426*ad1afb00SPedro Garcia 		pr_info("8021q: adding VLAN 0 to HW filter on device %s\n",
427*ad1afb00SPedro Garcia 			dev->name);
428*ad1afb00SPedro Garcia 		dev->netdev_ops->ndo_vlan_rx_add_vid(dev, 0);
429*ad1afb00SPedro Garcia 	}
430*ad1afb00SPedro Garcia 
431a9fde260SPavel Emelyanov 	grp = __vlan_find_group(dev);
4321da177e4SLinus Torvalds 	if (!grp)
4331da177e4SLinus Torvalds 		goto out;
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds 	/* It is OK that we do not hold the group lock right now,
4361da177e4SLinus Torvalds 	 * as we run under the RTNL lock.
4371da177e4SLinus Torvalds 	 */
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	switch (event) {
4401da177e4SLinus Torvalds 	case NETDEV_CHANGE:
4411da177e4SLinus Torvalds 		/* Propagate real device state to vlan devices */
4421da177e4SLinus Torvalds 		for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
4435c15bdecSDan Aloni 			vlandev = vlan_group_get_device(grp, i);
4441da177e4SLinus Torvalds 			if (!vlandev)
4451da177e4SLinus Torvalds 				continue;
4461da177e4SLinus Torvalds 
447fc4a7489SPatrick Mullaney 			netif_stacked_transfer_operstate(dev, vlandev);
4481da177e4SLinus Torvalds 		}
4491da177e4SLinus Torvalds 		break;
4501da177e4SLinus Torvalds 
4518c979c26SPatrick McHardy 	case NETDEV_CHANGEADDR:
4528c979c26SPatrick McHardy 		/* Adjust unicast filters on underlying device */
4538c979c26SPatrick McHardy 		for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
4548c979c26SPatrick McHardy 			vlandev = vlan_group_get_device(grp, i);
4558c979c26SPatrick McHardy 			if (!vlandev)
4568c979c26SPatrick McHardy 				continue;
4578c979c26SPatrick McHardy 
458d932e04aSPatrick McHardy 			flgs = vlandev->flags;
459d932e04aSPatrick McHardy 			if (!(flgs & IFF_UP))
460d932e04aSPatrick McHardy 				continue;
461d932e04aSPatrick McHardy 
4628c979c26SPatrick McHardy 			vlan_sync_address(dev, vlandev);
4638c979c26SPatrick McHardy 		}
4648c979c26SPatrick McHardy 		break;
4658c979c26SPatrick McHardy 
4662e477c9bSHerbert Xu 	case NETDEV_CHANGEMTU:
4672e477c9bSHerbert Xu 		for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
4682e477c9bSHerbert Xu 			vlandev = vlan_group_get_device(grp, i);
4692e477c9bSHerbert Xu 			if (!vlandev)
4702e477c9bSHerbert Xu 				continue;
4712e477c9bSHerbert Xu 
4722e477c9bSHerbert Xu 			if (vlandev->mtu <= dev->mtu)
4732e477c9bSHerbert Xu 				continue;
4742e477c9bSHerbert Xu 
4752e477c9bSHerbert Xu 			dev_set_mtu(vlandev, dev->mtu);
4762e477c9bSHerbert Xu 		}
4772e477c9bSHerbert Xu 		break;
4782e477c9bSHerbert Xu 
4795fb13570SPatrick McHardy 	case NETDEV_FEAT_CHANGE:
4805fb13570SPatrick McHardy 		/* Propagate device features to underlying device */
4815fb13570SPatrick McHardy 		for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
4825fb13570SPatrick McHardy 			vlandev = vlan_group_get_device(grp, i);
4835fb13570SPatrick McHardy 			if (!vlandev)
4845fb13570SPatrick McHardy 				continue;
4855fb13570SPatrick McHardy 
4865fb13570SPatrick McHardy 			vlan_transfer_features(dev, vlandev);
4875fb13570SPatrick McHardy 		}
4885fb13570SPatrick McHardy 
4895fb13570SPatrick McHardy 		break;
4905fb13570SPatrick McHardy 
4911da177e4SLinus Torvalds 	case NETDEV_DOWN:
4921da177e4SLinus Torvalds 		/* Put all VLANs for this dev in the down state too.  */
4931da177e4SLinus Torvalds 		for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
4945c15bdecSDan Aloni 			vlandev = vlan_group_get_device(grp, i);
4951da177e4SLinus Torvalds 			if (!vlandev)
4961da177e4SLinus Torvalds 				continue;
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 			flgs = vlandev->flags;
4991da177e4SLinus Torvalds 			if (!(flgs & IFF_UP))
5001da177e4SLinus Torvalds 				continue;
5011da177e4SLinus Torvalds 
5025e756593SPatrick McHardy 			vlan = vlan_dev_info(vlandev);
5035e756593SPatrick McHardy 			if (!(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
5041da177e4SLinus Torvalds 				dev_change_flags(vlandev, flgs & ~IFF_UP);
505fc4a7489SPatrick Mullaney 			netif_stacked_transfer_operstate(dev, vlandev);
5061da177e4SLinus Torvalds 		}
5071da177e4SLinus Torvalds 		break;
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	case NETDEV_UP:
5101da177e4SLinus Torvalds 		/* Put all VLANs for this dev in the up state too.  */
5111da177e4SLinus Torvalds 		for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
5125c15bdecSDan Aloni 			vlandev = vlan_group_get_device(grp, i);
5131da177e4SLinus Torvalds 			if (!vlandev)
5141da177e4SLinus Torvalds 				continue;
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 			flgs = vlandev->flags;
5171da177e4SLinus Torvalds 			if (flgs & IFF_UP)
5181da177e4SLinus Torvalds 				continue;
5191da177e4SLinus Torvalds 
5205e756593SPatrick McHardy 			vlan = vlan_dev_info(vlandev);
5215e756593SPatrick McHardy 			if (!(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
5221da177e4SLinus Torvalds 				dev_change_flags(vlandev, flgs | IFF_UP);
523fc4a7489SPatrick Mullaney 			netif_stacked_transfer_operstate(dev, vlandev);
5241da177e4SLinus Torvalds 		}
5251da177e4SLinus Torvalds 		break;
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
52829906f6aSPatrick McHardy 		/* Delete all VLANs for this dev. */
52929906f6aSPatrick McHardy 		grp->killall = 1;
53029906f6aSPatrick McHardy 
53129906f6aSPatrick McHardy 		for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
53229906f6aSPatrick McHardy 			vlandev = vlan_group_get_device(grp, i);
53329906f6aSPatrick McHardy 			if (!vlandev)
53429906f6aSPatrick McHardy 				continue;
53529906f6aSPatrick McHardy 
53629906f6aSPatrick McHardy 			/* unregistration of last vlan destroys group, abort
53729906f6aSPatrick McHardy 			 * afterwards */
53829906f6aSPatrick McHardy 			if (grp->nr_vlans == 1)
53929906f6aSPatrick McHardy 				i = VLAN_GROUP_ARRAY_LEN;
54029906f6aSPatrick McHardy 
54129906f6aSPatrick McHardy 			unregister_vlan_dev(vlandev, &list);
54229906f6aSPatrick McHardy 		}
54329906f6aSPatrick McHardy 		unregister_netdevice_many(&list);
5441da177e4SLinus Torvalds 		break;
5451c01fe14SJiri Pirko 
5461c01fe14SJiri Pirko 	case NETDEV_PRE_TYPE_CHANGE:
5471c01fe14SJiri Pirko 		/* Forbid underlaying device to change its type. */
5481c01fe14SJiri Pirko 		return NOTIFY_BAD;
5493ff50b79SStephen Hemminger 	}
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds out:
5521da177e4SLinus Torvalds 	return NOTIFY_DONE;
5531da177e4SLinus Torvalds }
5541da177e4SLinus Torvalds 
55569ab4b7dSPatrick McHardy static struct notifier_block vlan_notifier_block __read_mostly = {
55669ab4b7dSPatrick McHardy 	.notifier_call = vlan_device_event,
55769ab4b7dSPatrick McHardy };
55869ab4b7dSPatrick McHardy 
5591da177e4SLinus Torvalds /*
5601da177e4SLinus Torvalds  *	VLAN IOCTL handler.
5611da177e4SLinus Torvalds  *	o execute requested action or pass command to the device driver
5621da177e4SLinus Torvalds  *   arg is really a struct vlan_ioctl_args __user *.
5631da177e4SLinus Torvalds  */
564881d966bSEric W. Biederman static int vlan_ioctl_handler(struct net *net, void __user *arg)
5651da177e4SLinus Torvalds {
566c17d8874SPatrick McHardy 	int err;
5671da177e4SLinus Torvalds 	struct vlan_ioctl_args args;
568c17d8874SPatrick McHardy 	struct net_device *dev = NULL;
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	if (copy_from_user(&args, arg, sizeof(struct vlan_ioctl_args)))
5711da177e4SLinus Torvalds 		return -EFAULT;
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds 	/* Null terminate this sucker, just in case. */
5741da177e4SLinus Torvalds 	args.device1[23] = 0;
5751da177e4SLinus Torvalds 	args.u.device2[23] = 0;
5761da177e4SLinus Torvalds 
577c17d8874SPatrick McHardy 	rtnl_lock();
578c17d8874SPatrick McHardy 
5791da177e4SLinus Torvalds 	switch (args.cmd) {
5801da177e4SLinus Torvalds 	case SET_VLAN_INGRESS_PRIORITY_CMD:
581c17d8874SPatrick McHardy 	case SET_VLAN_EGRESS_PRIORITY_CMD:
582c17d8874SPatrick McHardy 	case SET_VLAN_FLAG_CMD:
583c17d8874SPatrick McHardy 	case ADD_VLAN_CMD:
584c17d8874SPatrick McHardy 	case DEL_VLAN_CMD:
585c17d8874SPatrick McHardy 	case GET_VLAN_REALDEV_NAME_CMD:
586c17d8874SPatrick McHardy 	case GET_VLAN_VID_CMD:
587c17d8874SPatrick McHardy 		err = -ENODEV;
58865d292a2SPavel Emelyanov 		dev = __dev_get_by_name(net, args.device1);
589c17d8874SPatrick McHardy 		if (!dev)
590c17d8874SPatrick McHardy 			goto out;
591c17d8874SPatrick McHardy 
592c17d8874SPatrick McHardy 		err = -EINVAL;
59326a25239SJoonwoo Park 		if (args.cmd != ADD_VLAN_CMD && !is_vlan_dev(dev))
594c17d8874SPatrick McHardy 			goto out;
595c17d8874SPatrick McHardy 	}
596c17d8874SPatrick McHardy 
597c17d8874SPatrick McHardy 	switch (args.cmd) {
598c17d8874SPatrick McHardy 	case SET_VLAN_INGRESS_PRIORITY_CMD:
599c17d8874SPatrick McHardy 		err = -EPERM;
6001da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
601c17d8874SPatrick McHardy 			break;
602c17d8874SPatrick McHardy 		vlan_dev_set_ingress_priority(dev,
6031da177e4SLinus Torvalds 					      args.u.skb_priority,
6041da177e4SLinus Torvalds 					      args.vlan_qos);
605fffe470aSPatrick McHardy 		err = 0;
6061da177e4SLinus Torvalds 		break;
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 	case SET_VLAN_EGRESS_PRIORITY_CMD:
609c17d8874SPatrick McHardy 		err = -EPERM;
6101da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
611c17d8874SPatrick McHardy 			break;
612c17d8874SPatrick McHardy 		err = vlan_dev_set_egress_priority(dev,
6131da177e4SLinus Torvalds 						   args.u.skb_priority,
6141da177e4SLinus Torvalds 						   args.vlan_qos);
6151da177e4SLinus Torvalds 		break;
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 	case SET_VLAN_FLAG_CMD:
618c17d8874SPatrick McHardy 		err = -EPERM;
6191da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
620c17d8874SPatrick McHardy 			break;
621b3ce0325SPatrick McHardy 		err = vlan_dev_change_flags(dev,
622b3ce0325SPatrick McHardy 					    args.vlan_qos ? args.u.flag : 0,
623b3ce0325SPatrick McHardy 					    args.u.flag);
6241da177e4SLinus Torvalds 		break;
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 	case SET_VLAN_NAME_TYPE_CMD:
627c17d8874SPatrick McHardy 		err = -EPERM;
6281da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
629e35de026SPavel Emelyanov 			break;
630c17d8874SPatrick McHardy 		if ((args.u.name_type >= 0) &&
631c17d8874SPatrick McHardy 		    (args.u.name_type < VLAN_NAME_TYPE_HIGHEST)) {
6327a17a2f7SPavel Emelyanov 			struct vlan_net *vn;
6337a17a2f7SPavel Emelyanov 
6347a17a2f7SPavel Emelyanov 			vn = net_generic(net, vlan_net_id);
6357a17a2f7SPavel Emelyanov 			vn->name_type = args.u.name_type;
6361da177e4SLinus Torvalds 			err = 0;
6371da177e4SLinus Torvalds 		} else {
6381da177e4SLinus Torvalds 			err = -EINVAL;
6391da177e4SLinus Torvalds 		}
6401da177e4SLinus Torvalds 		break;
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	case ADD_VLAN_CMD:
643c17d8874SPatrick McHardy 		err = -EPERM;
6441da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
645c17d8874SPatrick McHardy 			break;
6462ae0bf69SPatrick McHardy 		err = register_vlan_device(dev, args.u.VID);
6471da177e4SLinus Torvalds 		break;
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	case DEL_VLAN_CMD:
650c17d8874SPatrick McHardy 		err = -EPERM;
6511da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
652c17d8874SPatrick McHardy 			break;
65323289a37SEric Dumazet 		unregister_vlan_dev(dev, NULL);
654af301517SPatrick McHardy 		err = 0;
6551da177e4SLinus Torvalds 		break;
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	case GET_VLAN_REALDEV_NAME_CMD:
6583f5f4346SAndrew Morton 		err = 0;
659c17d8874SPatrick McHardy 		vlan_dev_get_realdev_name(dev, args.u.device2);
6601da177e4SLinus Torvalds 		if (copy_to_user(arg, &args,
6612029cc2cSPatrick McHardy 				 sizeof(struct vlan_ioctl_args)))
6621da177e4SLinus Torvalds 			err = -EFAULT;
6631da177e4SLinus Torvalds 		break;
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds 	case GET_VLAN_VID_CMD:
6663f5f4346SAndrew Morton 		err = 0;
66722d1ba74SPatrick McHardy 		args.u.VID = vlan_dev_vlan_id(dev);
6681da177e4SLinus Torvalds 		if (copy_to_user(arg, &args,
6692029cc2cSPatrick McHardy 				 sizeof(struct vlan_ioctl_args)))
6701da177e4SLinus Torvalds 		      err = -EFAULT;
6711da177e4SLinus Torvalds 		break;
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 	default:
674198a291cSPatrick McHardy 		err = -EOPNOTSUPP;
675c17d8874SPatrick McHardy 		break;
6763ff50b79SStephen Hemminger 	}
6777eb1b3d3SMika Kukkonen out:
678c17d8874SPatrick McHardy 	rtnl_unlock();
6791da177e4SLinus Torvalds 	return err;
6801da177e4SLinus Torvalds }
6811da177e4SLinus Torvalds 
6822c8c1e72SAlexey Dobriyan static int __net_init vlan_init_net(struct net *net)
683d9ed0f0eSPavel Emelyanov {
684946d1a92SEric W. Biederman 	struct vlan_net *vn = net_generic(net, vlan_net_id);
685d9ed0f0eSPavel Emelyanov 	int err;
686d9ed0f0eSPavel Emelyanov 
6877a17a2f7SPavel Emelyanov 	vn->name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD;
6887a17a2f7SPavel Emelyanov 
689cd1c7014SPavel Emelyanov 	err = vlan_proc_init(net);
690cd1c7014SPavel Emelyanov 
691d9ed0f0eSPavel Emelyanov 	return err;
692d9ed0f0eSPavel Emelyanov }
693d9ed0f0eSPavel Emelyanov 
6942c8c1e72SAlexey Dobriyan static void __net_exit vlan_exit_net(struct net *net)
695d9ed0f0eSPavel Emelyanov {
696cd1c7014SPavel Emelyanov 	vlan_proc_cleanup(net);
697d9ed0f0eSPavel Emelyanov }
698d9ed0f0eSPavel Emelyanov 
699d9ed0f0eSPavel Emelyanov static struct pernet_operations vlan_net_ops = {
700d9ed0f0eSPavel Emelyanov 	.init = vlan_init_net,
701d9ed0f0eSPavel Emelyanov 	.exit = vlan_exit_net,
702946d1a92SEric W. Biederman 	.id   = &vlan_net_id,
703946d1a92SEric W. Biederman 	.size = sizeof(struct vlan_net),
704d9ed0f0eSPavel Emelyanov };
705d9ed0f0eSPavel Emelyanov 
70669ab4b7dSPatrick McHardy static int __init vlan_proto_init(void)
70769ab4b7dSPatrick McHardy {
70869ab4b7dSPatrick McHardy 	int err;
70969ab4b7dSPatrick McHardy 
71069ab4b7dSPatrick McHardy 	pr_info("%s v%s %s\n", vlan_fullname, vlan_version, vlan_copyright);
71169ab4b7dSPatrick McHardy 	pr_info("All bugs added by %s\n", vlan_buggyright);
71269ab4b7dSPatrick McHardy 
71391e2ff35SEric W. Biederman 	err = register_pernet_subsys(&vlan_net_ops);
714d9ed0f0eSPavel Emelyanov 	if (err < 0)
715d9ed0f0eSPavel Emelyanov 		goto err0;
716d9ed0f0eSPavel Emelyanov 
71769ab4b7dSPatrick McHardy 	err = register_netdevice_notifier(&vlan_notifier_block);
71869ab4b7dSPatrick McHardy 	if (err < 0)
71969ab4b7dSPatrick McHardy 		goto err2;
72069ab4b7dSPatrick McHardy 
72170c03b49SPatrick McHardy 	err = vlan_gvrp_init();
72269ab4b7dSPatrick McHardy 	if (err < 0)
72369ab4b7dSPatrick McHardy 		goto err3;
72469ab4b7dSPatrick McHardy 
72570c03b49SPatrick McHardy 	err = vlan_netlink_init();
72670c03b49SPatrick McHardy 	if (err < 0)
72770c03b49SPatrick McHardy 		goto err4;
72870c03b49SPatrick McHardy 
72969ab4b7dSPatrick McHardy 	dev_add_pack(&vlan_packet_type);
73069ab4b7dSPatrick McHardy 	vlan_ioctl_set(vlan_ioctl_handler);
73169ab4b7dSPatrick McHardy 	return 0;
73269ab4b7dSPatrick McHardy 
73370c03b49SPatrick McHardy err4:
73470c03b49SPatrick McHardy 	vlan_gvrp_uninit();
73569ab4b7dSPatrick McHardy err3:
73669ab4b7dSPatrick McHardy 	unregister_netdevice_notifier(&vlan_notifier_block);
73769ab4b7dSPatrick McHardy err2:
73891e2ff35SEric W. Biederman 	unregister_pernet_subsys(&vlan_net_ops);
739d9ed0f0eSPavel Emelyanov err0:
74069ab4b7dSPatrick McHardy 	return err;
74169ab4b7dSPatrick McHardy }
74269ab4b7dSPatrick McHardy 
74369ab4b7dSPatrick McHardy static void __exit vlan_cleanup_module(void)
74469ab4b7dSPatrick McHardy {
74569ab4b7dSPatrick McHardy 	unsigned int i;
74669ab4b7dSPatrick McHardy 
74769ab4b7dSPatrick McHardy 	vlan_ioctl_set(NULL);
74869ab4b7dSPatrick McHardy 	vlan_netlink_fini();
74969ab4b7dSPatrick McHardy 
75069ab4b7dSPatrick McHardy 	unregister_netdevice_notifier(&vlan_notifier_block);
75169ab4b7dSPatrick McHardy 
75269ab4b7dSPatrick McHardy 	dev_remove_pack(&vlan_packet_type);
75369ab4b7dSPatrick McHardy 
75469ab4b7dSPatrick McHardy 	/* This table must be empty if there are no module references left. */
75569ab4b7dSPatrick McHardy 	for (i = 0; i < VLAN_GRP_HASH_SIZE; i++)
75669ab4b7dSPatrick McHardy 		BUG_ON(!hlist_empty(&vlan_group_hash[i]));
75769ab4b7dSPatrick McHardy 
75891e2ff35SEric W. Biederman 	unregister_pernet_subsys(&vlan_net_ops);
7596e327c11SJesper Dangaard Brouer 	rcu_barrier(); /* Wait for completion of call_rcu()'s */
76070c03b49SPatrick McHardy 
76170c03b49SPatrick McHardy 	vlan_gvrp_uninit();
76269ab4b7dSPatrick McHardy }
76369ab4b7dSPatrick McHardy 
76469ab4b7dSPatrick McHardy module_init(vlan_proto_init);
76569ab4b7dSPatrick McHardy module_exit(vlan_cleanup_module);
76669ab4b7dSPatrick McHardy 
7671da177e4SLinus Torvalds MODULE_LICENSE("GPL");
7681da177e4SLinus Torvalds MODULE_VERSION(DRV_VERSION);
769