xref: /openbmc/linux/net/8021q/vlan_dev.c (revision dc8eaaa0)
11da177e4SLinus Torvalds /* -*- linux-c -*-
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:       Mar 22 2001: Martin Bokaemper <mbokaemper@unispherenetworks.com>
101da177e4SLinus Torvalds  *                - reset skb->pkt_type on incoming packets when MAC was changed
111da177e4SLinus Torvalds  *                - see that changed MAC is saddr for outgoing packets
121da177e4SLinus Torvalds  *              Oct 20, 2001:  Ard van Breeman:
131da177e4SLinus Torvalds  *                - Fix MC-list, finally.
141da177e4SLinus Torvalds  *                - Flush MC-list on VLAN destroy.
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
181da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
191da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
201da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
211da177e4SLinus Torvalds  */
221da177e4SLinus Torvalds 
23afab2d29SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24afab2d29SJoe Perches 
251da177e4SLinus Torvalds #include <linux/module.h>
265a0e3ad6STejun Heo #include <linux/slab.h>
271da177e4SLinus Torvalds #include <linux/skbuff.h>
281da177e4SLinus Torvalds #include <linux/netdevice.h>
291da177e4SLinus Torvalds #include <linux/etherdevice.h>
3075b8846aSPatrick McHardy #include <linux/ethtool.h>
311da177e4SLinus Torvalds #include <net/arp.h>
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds #include "vlan.h"
341da177e4SLinus Torvalds #include "vlanproc.h"
351da177e4SLinus Torvalds #include <linux/if_vlan.h>
366d4cdf47SBenjamin LaHaise #include <linux/netpoll.h>
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds /*
391da177e4SLinus Torvalds  *	Rebuild the Ethernet MAC header. This is called after an ARP
401da177e4SLinus Torvalds  *	(or in future other address resolution) has completed on this
411da177e4SLinus Torvalds  *	sk_buff. We now let ARP fill in the other fields.
421da177e4SLinus Torvalds  *
431da177e4SLinus Torvalds  *	This routine CANNOT use cached dst->neigh!
441da177e4SLinus Torvalds  *	Really, it is used only when dst->neigh is wrong.
451da177e4SLinus Torvalds  *
461da177e4SLinus Torvalds  * TODO:  This needs a checkup, I'm ignorant here. --BLG
471da177e4SLinus Torvalds  */
48ef3eb3e5SPatrick McHardy static int vlan_dev_rebuild_header(struct sk_buff *skb)
491da177e4SLinus Torvalds {
501da177e4SLinus Torvalds 	struct net_device *dev = skb->dev;
511da177e4SLinus Torvalds 	struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds 	switch (veth->h_vlan_encapsulated_proto) {
541da177e4SLinus Torvalds #ifdef CONFIG_INET
5560678040SArnaldo Carvalho de Melo 	case htons(ETH_P_IP):
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds 		/* TODO:  Confirm this will work with VLAN headers... */
581da177e4SLinus Torvalds 		return arp_find(veth->h_dest, skb);
591da177e4SLinus Torvalds #endif
601da177e4SLinus Torvalds 	default:
61afab2d29SJoe Perches 		pr_debug("%s: unable to resolve type %X addresses\n",
62d136fe72SAlexey Dobriyan 			 dev->name, ntohs(veth->h_vlan_encapsulated_proto));
631da177e4SLinus Torvalds 
6407fc67beSJoe Perches 		ether_addr_copy(veth->h_source, dev->dev_addr);
651da177e4SLinus Torvalds 		break;
663ff50b79SStephen Hemminger 	}
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds 	return 0;
691da177e4SLinus Torvalds }
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds /*
721da177e4SLinus Torvalds  *	Create the VLAN header for an arbitrary protocol layer
731da177e4SLinus Torvalds  *
741da177e4SLinus Torvalds  *	saddr=NULL	means use device source address
751da177e4SLinus Torvalds  *	daddr=NULL	means leave destination address (eg unresolved arp)
761da177e4SLinus Torvalds  *
771da177e4SLinus Torvalds  *  This is called when the SKB is moving down the stack towards the
781da177e4SLinus Torvalds  *  physical devices.
791da177e4SLinus Torvalds  */
80ef3eb3e5SPatrick McHardy static int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
813b04dddeSStephen Hemminger 				unsigned short type,
82ef3eb3e5SPatrick McHardy 				const void *daddr, const void *saddr,
83ef3eb3e5SPatrick McHardy 				unsigned int len)
841da177e4SLinus Torvalds {
851fd9b1fcSPatrick McHardy 	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
861da177e4SLinus Torvalds 	struct vlan_hdr *vhdr;
871349fe9aSPatrick McHardy 	unsigned int vhdrlen = 0;
889bb8582eSPatrick McHardy 	u16 vlan_tci = 0;
891349fe9aSPatrick McHardy 	int rc;
901da177e4SLinus Torvalds 
910c0667a8SWang Sheng-Hui 	if (!(vlan->flags & VLAN_FLAG_REORDER_HDR)) {
921da177e4SLinus Torvalds 		vhdr = (struct vlan_hdr *) skb_push(skb, VLAN_HLEN);
931da177e4SLinus Torvalds 
940c0667a8SWang Sheng-Hui 		vlan_tci = vlan->vlan_id;
95e267cb96SDavid S. Miller 		vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb->priority);
969bb8582eSPatrick McHardy 		vhdr->h_vlan_TCI = htons(vlan_tci);
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 		/*
99bf9ae538SOctavian Purdila 		 *  Set the protocol type. For a packet of type ETH_P_802_3/2 we
100bf9ae538SOctavian Purdila 		 *  put the length in here instead.
1011da177e4SLinus Torvalds 		 */
102bf9ae538SOctavian Purdila 		if (type != ETH_P_802_3 && type != ETH_P_802_2)
1031da177e4SLinus Torvalds 			vhdr->h_vlan_encapsulated_proto = htons(type);
1042029cc2cSPatrick McHardy 		else
1051da177e4SLinus Torvalds 			vhdr->h_vlan_encapsulated_proto = htons(len);
106279e172aSJerome Borsboom 
1071fd9b1fcSPatrick McHardy 		skb->protocol = vlan->vlan_proto;
1081fd9b1fcSPatrick McHardy 		type = ntohs(vlan->vlan_proto);
1091349fe9aSPatrick McHardy 		vhdrlen = VLAN_HLEN;
1101da177e4SLinus Torvalds 	}
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds 	/* Before delegating work to the lower layer, enter our MAC-address */
1131da177e4SLinus Torvalds 	if (saddr == NULL)
1141da177e4SLinus Torvalds 		saddr = dev->dev_addr;
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 	/* Now make the underlying real hard header */
1170c0667a8SWang Sheng-Hui 	dev = vlan->real_dev;
1181349fe9aSPatrick McHardy 	rc = dev_hard_header(skb, dev, type, daddr, saddr, len + vhdrlen);
1190c4e8581SStephen Hemminger 	if (rc > 0)
1201349fe9aSPatrick McHardy 		rc += vhdrlen;
1211da177e4SLinus Torvalds 	return rc;
1221da177e4SLinus Torvalds }
1231da177e4SLinus Torvalds 
1246eacf8adSAmerigo Wang static inline netdev_tx_t vlan_netpoll_send_skb(struct vlan_dev_priv *vlan, struct sk_buff *skb)
1256eacf8adSAmerigo Wang {
1266eacf8adSAmerigo Wang #ifdef CONFIG_NET_POLL_CONTROLLER
1276eacf8adSAmerigo Wang 	if (vlan->netpoll)
1286eacf8adSAmerigo Wang 		netpoll_send_skb(vlan->netpoll, skb);
1296eacf8adSAmerigo Wang #else
1306eacf8adSAmerigo Wang 	BUG();
1316eacf8adSAmerigo Wang #endif
1326eacf8adSAmerigo Wang 	return NETDEV_TX_OK;
1336eacf8adSAmerigo Wang }
1346eacf8adSAmerigo Wang 
1356fef4c0cSStephen Hemminger static netdev_tx_t vlan_dev_hard_start_xmit(struct sk_buff *skb,
1366fef4c0cSStephen Hemminger 					    struct net_device *dev)
1371da177e4SLinus Torvalds {
1386eacf8adSAmerigo Wang 	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
1391da177e4SLinus Torvalds 	struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
1401a123a31SEric Dumazet 	unsigned int len;
1411a123a31SEric Dumazet 	int ret;
14255b01e86SDavid S. Miller 
1431da177e4SLinus Torvalds 	/* Handle non-VLAN frames if they are sent to us, for example by DHCP.
1441da177e4SLinus Torvalds 	 *
1451da177e4SLinus Torvalds 	 * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING
1461da177e4SLinus Torvalds 	 * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs...
1471da177e4SLinus Torvalds 	 */
1481fd9b1fcSPatrick McHardy 	if (veth->h_vlan_proto != vlan->vlan_proto ||
1496eacf8adSAmerigo Wang 	    vlan->flags & VLAN_FLAG_REORDER_HDR) {
1509bb8582eSPatrick McHardy 		u16 vlan_tci;
1516eacf8adSAmerigo Wang 		vlan_tci = vlan->vlan_id;
152e267cb96SDavid S. Miller 		vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb->priority);
15386a9bad3SPatrick McHardy 		skb = __vlan_hwaccel_put_tag(skb, vlan->vlan_proto, vlan_tci);
154636e19a3SJohn Fastabend 	}
1551da177e4SLinus Torvalds 
1566eacf8adSAmerigo Wang 	skb->dev = vlan->real_dev;
1571a123a31SEric Dumazet 	len = skb->len;
1586eacf8adSAmerigo Wang 	if (unlikely(netpoll_tx_running(dev)))
1596eacf8adSAmerigo Wang 		return vlan_netpoll_send_skb(vlan, skb);
1606eacf8adSAmerigo Wang 
1611a123a31SEric Dumazet 	ret = dev_queue_xmit(skb);
1621a123a31SEric Dumazet 
1632d6c9ffcSEric Dumazet 	if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
1644af429d2SEric Dumazet 		struct vlan_pcpu_stats *stats;
1654af429d2SEric Dumazet 
1666eacf8adSAmerigo Wang 		stats = this_cpu_ptr(vlan->vlan_pcpu_stats);
1674af429d2SEric Dumazet 		u64_stats_update_begin(&stats->syncp);
1684af429d2SEric Dumazet 		stats->tx_packets++;
1694af429d2SEric Dumazet 		stats->tx_bytes += len;
170307f73dfSWei Yongjun 		u64_stats_update_end(&stats->syncp);
1714af429d2SEric Dumazet 	} else {
1726eacf8adSAmerigo Wang 		this_cpu_inc(vlan->vlan_pcpu_stats->tx_dropped);
1734af429d2SEric Dumazet 	}
1741a123a31SEric Dumazet 
175cbbef5e1SPatrick McHardy 	return ret;
1761da177e4SLinus Torvalds }
1771da177e4SLinus Torvalds 
178ef3eb3e5SPatrick McHardy static int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
1791da177e4SLinus Torvalds {
1801da177e4SLinus Torvalds 	/* TODO: gotta make sure the underlying layer can handle it,
1811da177e4SLinus Torvalds 	 * maybe an IFF_VLAN_CAPABLE flag for devices?
1821da177e4SLinus Torvalds 	 */
1837da82c06SJiri Pirko 	if (vlan_dev_priv(dev)->real_dev->mtu < new_mtu)
1841da177e4SLinus Torvalds 		return -ERANGE;
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds 	dev->mtu = new_mtu;
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 	return 0;
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
191c17d8874SPatrick McHardy void vlan_dev_set_ingress_priority(const struct net_device *dev,
1929bb8582eSPatrick McHardy 				   u32 skb_prio, u16 vlan_prio)
1931da177e4SLinus Torvalds {
1947da82c06SJiri Pirko 	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
195b020cb48SPatrick McHardy 
196b020cb48SPatrick McHardy 	if (vlan->ingress_priority_map[vlan_prio & 0x7] && !skb_prio)
197b020cb48SPatrick McHardy 		vlan->nr_ingress_mappings--;
198b020cb48SPatrick McHardy 	else if (!vlan->ingress_priority_map[vlan_prio & 0x7] && skb_prio)
199b020cb48SPatrick McHardy 		vlan->nr_ingress_mappings++;
200b020cb48SPatrick McHardy 
201b020cb48SPatrick McHardy 	vlan->ingress_priority_map[vlan_prio & 0x7] = skb_prio;
2021da177e4SLinus Torvalds }
2031da177e4SLinus Torvalds 
204c17d8874SPatrick McHardy int vlan_dev_set_egress_priority(const struct net_device *dev,
2059bb8582eSPatrick McHardy 				 u32 skb_prio, u16 vlan_prio)
2061da177e4SLinus Torvalds {
2077da82c06SJiri Pirko 	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
2081da177e4SLinus Torvalds 	struct vlan_priority_tci_mapping *mp = NULL;
2091da177e4SLinus Torvalds 	struct vlan_priority_tci_mapping *np;
21005423b24SEric Dumazet 	u32 vlan_qos = (vlan_prio << VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK;
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	/* See if a priority mapping exists.. */
213b020cb48SPatrick McHardy 	mp = vlan->egress_priority_map[skb_prio & 0xF];
2141da177e4SLinus Torvalds 	while (mp) {
2151da177e4SLinus Torvalds 		if (mp->priority == skb_prio) {
216b020cb48SPatrick McHardy 			if (mp->vlan_qos && !vlan_qos)
217b020cb48SPatrick McHardy 				vlan->nr_egress_mappings--;
218b020cb48SPatrick McHardy 			else if (!mp->vlan_qos && vlan_qos)
219b020cb48SPatrick McHardy 				vlan->nr_egress_mappings++;
220b020cb48SPatrick McHardy 			mp->vlan_qos = vlan_qos;
2211da177e4SLinus Torvalds 			return 0;
2221da177e4SLinus Torvalds 		}
2231da177e4SLinus Torvalds 		mp = mp->next;
2241da177e4SLinus Torvalds 	}
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 	/* Create a new mapping then. */
227b020cb48SPatrick McHardy 	mp = vlan->egress_priority_map[skb_prio & 0xF];
2281da177e4SLinus Torvalds 	np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL);
229c17d8874SPatrick McHardy 	if (!np)
230c17d8874SPatrick McHardy 		return -ENOBUFS;
231c17d8874SPatrick McHardy 
2321da177e4SLinus Torvalds 	np->next = mp;
2331da177e4SLinus Torvalds 	np->priority = skb_prio;
234b020cb48SPatrick McHardy 	np->vlan_qos = vlan_qos;
2353e3aac49SEric Dumazet 	/* Before inserting this element in hash table, make sure all its fields
2363e3aac49SEric Dumazet 	 * are committed to memory.
237e267cb96SDavid S. Miller 	 * coupled with smp_rmb() in vlan_dev_get_egress_qos_mask()
2383e3aac49SEric Dumazet 	 */
2393e3aac49SEric Dumazet 	smp_wmb();
240b020cb48SPatrick McHardy 	vlan->egress_priority_map[skb_prio & 0xF] = np;
241b020cb48SPatrick McHardy 	if (vlan_qos)
242b020cb48SPatrick McHardy 		vlan->nr_egress_mappings++;
2431da177e4SLinus Torvalds 	return 0;
2441da177e4SLinus Torvalds }
2451da177e4SLinus Torvalds 
246a4bf3af4SPatrick McHardy /* Flags are defined in the vlan_flags enum in include/linux/if_vlan.h file. */
247b3ce0325SPatrick McHardy int vlan_dev_change_flags(const struct net_device *dev, u32 flags, u32 mask)
2481da177e4SLinus Torvalds {
2497da82c06SJiri Pirko 	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
250b3ce0325SPatrick McHardy 	u32 old_flags = vlan->flags;
251b3ce0325SPatrick McHardy 
2525e756593SPatrick McHardy 	if (mask & ~(VLAN_FLAG_REORDER_HDR | VLAN_FLAG_GVRP |
25386fbe9bbSDavid Ward 		     VLAN_FLAG_LOOSE_BINDING | VLAN_FLAG_MVRP))
2541da177e4SLinus Torvalds 		return -EINVAL;
255b3ce0325SPatrick McHardy 
256b3ce0325SPatrick McHardy 	vlan->flags = (old_flags & ~mask) | (flags & mask);
25770c03b49SPatrick McHardy 
25870c03b49SPatrick McHardy 	if (netif_running(dev) && (vlan->flags ^ old_flags) & VLAN_FLAG_GVRP) {
25970c03b49SPatrick McHardy 		if (vlan->flags & VLAN_FLAG_GVRP)
26070c03b49SPatrick McHardy 			vlan_gvrp_request_join(dev);
26170c03b49SPatrick McHardy 		else
26270c03b49SPatrick McHardy 			vlan_gvrp_request_leave(dev);
26370c03b49SPatrick McHardy 	}
26486fbe9bbSDavid Ward 
26586fbe9bbSDavid Ward 	if (netif_running(dev) && (vlan->flags ^ old_flags) & VLAN_FLAG_MVRP) {
26686fbe9bbSDavid Ward 		if (vlan->flags & VLAN_FLAG_MVRP)
26786fbe9bbSDavid Ward 			vlan_mvrp_request_join(dev);
26886fbe9bbSDavid Ward 		else
26986fbe9bbSDavid Ward 			vlan_mvrp_request_leave(dev);
27086fbe9bbSDavid Ward 	}
271b3ce0325SPatrick McHardy 	return 0;
2721da177e4SLinus Torvalds }
2731da177e4SLinus Torvalds 
274c17d8874SPatrick McHardy void vlan_dev_get_realdev_name(const struct net_device *dev, char *result)
2751da177e4SLinus Torvalds {
2767da82c06SJiri Pirko 	strncpy(result, vlan_dev_priv(dev)->real_dev->name, 23);
2771da177e4SLinus Torvalds }
2781da177e4SLinus Torvalds 
279ef3eb3e5SPatrick McHardy static int vlan_dev_open(struct net_device *dev)
2801da177e4SLinus Torvalds {
2817da82c06SJiri Pirko 	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
2828c979c26SPatrick McHardy 	struct net_device *real_dev = vlan->real_dev;
2838c979c26SPatrick McHardy 	int err;
2848c979c26SPatrick McHardy 
2855e756593SPatrick McHardy 	if (!(real_dev->flags & IFF_UP) &&
2865e756593SPatrick McHardy 	    !(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
2871da177e4SLinus Torvalds 		return -ENETDOWN;
2881da177e4SLinus Torvalds 
28953a2b3a1SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr)) {
290a748ee24SJiri Pirko 		err = dev_uc_add(real_dev, dev->dev_addr);
2918c979c26SPatrick McHardy 		if (err < 0)
29289146504SWang Chen 			goto out;
2938c979c26SPatrick McHardy 	}
2948c979c26SPatrick McHardy 
29589146504SWang Chen 	if (dev->flags & IFF_ALLMULTI) {
29689146504SWang Chen 		err = dev_set_allmulti(real_dev, 1);
29789146504SWang Chen 		if (err < 0)
29889146504SWang Chen 			goto del_unicast;
29989146504SWang Chen 	}
30089146504SWang Chen 	if (dev->flags & IFF_PROMISC) {
30189146504SWang Chen 		err = dev_set_promiscuity(real_dev, 1);
30289146504SWang Chen 		if (err < 0)
30389146504SWang Chen 			goto clear_allmulti;
30489146504SWang Chen 	}
30589146504SWang Chen 
30607fc67beSJoe Perches 	ether_addr_copy(vlan->real_dev_addr, real_dev->dev_addr);
3076c78dcbdSPatrick McHardy 
30870c03b49SPatrick McHardy 	if (vlan->flags & VLAN_FLAG_GVRP)
30970c03b49SPatrick McHardy 		vlan_gvrp_request_join(dev);
31070c03b49SPatrick McHardy 
31186fbe9bbSDavid Ward 	if (vlan->flags & VLAN_FLAG_MVRP)
31286fbe9bbSDavid Ward 		vlan_mvrp_request_join(dev);
31386fbe9bbSDavid Ward 
3140ac820eeSPhil Oester 	if (netif_carrier_ok(real_dev))
315adc667e8SJay Vosburgh 		netif_carrier_on(dev);
3161da177e4SLinus Torvalds 	return 0;
31789146504SWang Chen 
31889146504SWang Chen clear_allmulti:
31989146504SWang Chen 	if (dev->flags & IFF_ALLMULTI)
32089146504SWang Chen 		dev_set_allmulti(real_dev, -1);
32189146504SWang Chen del_unicast:
32253a2b3a1SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr))
323a748ee24SJiri Pirko 		dev_uc_del(real_dev, dev->dev_addr);
32489146504SWang Chen out:
325adc667e8SJay Vosburgh 	netif_carrier_off(dev);
32689146504SWang Chen 	return err;
3271da177e4SLinus Torvalds }
3281da177e4SLinus Torvalds 
329ef3eb3e5SPatrick McHardy static int vlan_dev_stop(struct net_device *dev)
3301da177e4SLinus Torvalds {
3317da82c06SJiri Pirko 	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
33270c03b49SPatrick McHardy 	struct net_device *real_dev = vlan->real_dev;
33370c03b49SPatrick McHardy 
33456addd6eSPatrick McHardy 	dev_mc_unsync(real_dev, dev);
335a748ee24SJiri Pirko 	dev_uc_unsync(real_dev, dev);
3366c78dcbdSPatrick McHardy 	if (dev->flags & IFF_ALLMULTI)
3376c78dcbdSPatrick McHardy 		dev_set_allmulti(real_dev, -1);
3386c78dcbdSPatrick McHardy 	if (dev->flags & IFF_PROMISC)
3396c78dcbdSPatrick McHardy 		dev_set_promiscuity(real_dev, -1);
3406c78dcbdSPatrick McHardy 
34153a2b3a1SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr))
342a748ee24SJiri Pirko 		dev_uc_del(real_dev, dev->dev_addr);
3438c979c26SPatrick McHardy 
344adc667e8SJay Vosburgh 	netif_carrier_off(dev);
3451da177e4SLinus Torvalds 	return 0;
3461da177e4SLinus Torvalds }
3471da177e4SLinus Torvalds 
348ef3eb3e5SPatrick McHardy static int vlan_dev_set_mac_address(struct net_device *dev, void *p)
34939aaac11SPatrick McHardy {
3507da82c06SJiri Pirko 	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
35139aaac11SPatrick McHardy 	struct sockaddr *addr = p;
35239aaac11SPatrick McHardy 	int err;
35339aaac11SPatrick McHardy 
35439aaac11SPatrick McHardy 	if (!is_valid_ether_addr(addr->sa_data))
35539aaac11SPatrick McHardy 		return -EADDRNOTAVAIL;
35639aaac11SPatrick McHardy 
35739aaac11SPatrick McHardy 	if (!(dev->flags & IFF_UP))
35839aaac11SPatrick McHardy 		goto out;
35939aaac11SPatrick McHardy 
36053a2b3a1SJoe Perches 	if (!ether_addr_equal(addr->sa_data, real_dev->dev_addr)) {
361a748ee24SJiri Pirko 		err = dev_uc_add(real_dev, addr->sa_data);
36239aaac11SPatrick McHardy 		if (err < 0)
36339aaac11SPatrick McHardy 			return err;
36439aaac11SPatrick McHardy 	}
36539aaac11SPatrick McHardy 
36653a2b3a1SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr))
367a748ee24SJiri Pirko 		dev_uc_del(real_dev, dev->dev_addr);
36839aaac11SPatrick McHardy 
36939aaac11SPatrick McHardy out:
37007fc67beSJoe Perches 	ether_addr_copy(dev->dev_addr, addr->sa_data);
37139aaac11SPatrick McHardy 	return 0;
37239aaac11SPatrick McHardy }
37339aaac11SPatrick McHardy 
374ef3eb3e5SPatrick McHardy static int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3751da177e4SLinus Torvalds {
3767da82c06SJiri Pirko 	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
377656299f7SStephen Hemminger 	const struct net_device_ops *ops = real_dev->netdev_ops;
3781da177e4SLinus Torvalds 	struct ifreq ifrr;
3791da177e4SLinus Torvalds 	int err = -EOPNOTSUPP;
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	strncpy(ifrr.ifr_name, real_dev->name, IFNAMSIZ);
3821da177e4SLinus Torvalds 	ifrr.ifr_ifru = ifr->ifr_ifru;
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	switch (cmd) {
3851da177e4SLinus Torvalds 	case SIOCGMIIPHY:
3861da177e4SLinus Torvalds 	case SIOCGMIIREG:
3871da177e4SLinus Torvalds 	case SIOCSMIIREG:
388656299f7SStephen Hemminger 		if (netif_device_present(real_dev) && ops->ndo_do_ioctl)
389656299f7SStephen Hemminger 			err = ops->ndo_do_ioctl(real_dev, &ifrr, cmd);
3901da177e4SLinus Torvalds 		break;
3911da177e4SLinus Torvalds 	}
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 	if (!err)
3941da177e4SLinus Torvalds 		ifr->ifr_ifru = ifrr.ifr_ifru;
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	return err;
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds 
399cc883d16SFrank Blaschka static int vlan_dev_neigh_setup(struct net_device *dev, struct neigh_parms *pa)
400cc883d16SFrank Blaschka {
4017da82c06SJiri Pirko 	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
402cc883d16SFrank Blaschka 	const struct net_device_ops *ops = real_dev->netdev_ops;
403cc883d16SFrank Blaschka 	int err = 0;
404cc883d16SFrank Blaschka 
405cc883d16SFrank Blaschka 	if (netif_device_present(real_dev) && ops->ndo_neigh_setup)
4069d40bbdaSDavid S. Miller 		err = ops->ndo_neigh_setup(real_dev, pa);
407cc883d16SFrank Blaschka 
408cc883d16SFrank Blaschka 	return err;
409cc883d16SFrank Blaschka }
410cc883d16SFrank Blaschka 
411f4d5392eSAmerigo Wang #if IS_ENABLED(CONFIG_FCOE)
412b85daa53SVasu Dev static int vlan_dev_fcoe_ddp_setup(struct net_device *dev, u16 xid,
413b85daa53SVasu Dev 				   struct scatterlist *sgl, unsigned int sgc)
414b85daa53SVasu Dev {
4157da82c06SJiri Pirko 	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
416b85daa53SVasu Dev 	const struct net_device_ops *ops = real_dev->netdev_ops;
417b85daa53SVasu Dev 	int rc = 0;
418b85daa53SVasu Dev 
419b85daa53SVasu Dev 	if (ops->ndo_fcoe_ddp_setup)
420b85daa53SVasu Dev 		rc = ops->ndo_fcoe_ddp_setup(real_dev, xid, sgl, sgc);
421b85daa53SVasu Dev 
422b85daa53SVasu Dev 	return rc;
423b85daa53SVasu Dev }
424b85daa53SVasu Dev 
425b85daa53SVasu Dev static int vlan_dev_fcoe_ddp_done(struct net_device *dev, u16 xid)
426b85daa53SVasu Dev {
4277da82c06SJiri Pirko 	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
428b85daa53SVasu Dev 	const struct net_device_ops *ops = real_dev->netdev_ops;
429b85daa53SVasu Dev 	int len = 0;
430b85daa53SVasu Dev 
431b85daa53SVasu Dev 	if (ops->ndo_fcoe_ddp_done)
432b85daa53SVasu Dev 		len = ops->ndo_fcoe_ddp_done(real_dev, xid);
433b85daa53SVasu Dev 
434b85daa53SVasu Dev 	return len;
435b85daa53SVasu Dev }
4360af46d99SYi Zou 
4370af46d99SYi Zou static int vlan_dev_fcoe_enable(struct net_device *dev)
4380af46d99SYi Zou {
4397da82c06SJiri Pirko 	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
4400af46d99SYi Zou 	const struct net_device_ops *ops = real_dev->netdev_ops;
4410af46d99SYi Zou 	int rc = -EINVAL;
4420af46d99SYi Zou 
4430af46d99SYi Zou 	if (ops->ndo_fcoe_enable)
4440af46d99SYi Zou 		rc = ops->ndo_fcoe_enable(real_dev);
4450af46d99SYi Zou 	return rc;
4460af46d99SYi Zou }
4470af46d99SYi Zou 
4480af46d99SYi Zou static int vlan_dev_fcoe_disable(struct net_device *dev)
4490af46d99SYi Zou {
4507da82c06SJiri Pirko 	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
4510af46d99SYi Zou 	const struct net_device_ops *ops = real_dev->netdev_ops;
4520af46d99SYi Zou 	int rc = -EINVAL;
4530af46d99SYi Zou 
4540af46d99SYi Zou 	if (ops->ndo_fcoe_disable)
4550af46d99SYi Zou 		rc = ops->ndo_fcoe_disable(real_dev);
4560af46d99SYi Zou 	return rc;
4570af46d99SYi Zou }
458d6534799SYi Zou 
459d6534799SYi Zou static int vlan_dev_fcoe_get_wwn(struct net_device *dev, u64 *wwn, int type)
460d6534799SYi Zou {
4617da82c06SJiri Pirko 	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
462d6534799SYi Zou 	const struct net_device_ops *ops = real_dev->netdev_ops;
463d6534799SYi Zou 	int rc = -EINVAL;
464d6534799SYi Zou 
465d6534799SYi Zou 	if (ops->ndo_fcoe_get_wwn)
466d6534799SYi Zou 		rc = ops->ndo_fcoe_get_wwn(real_dev, wwn, type);
467d6534799SYi Zou 	return rc;
468d6534799SYi Zou }
4694ea09c9cSYi Zou 
4704ea09c9cSYi Zou static int vlan_dev_fcoe_ddp_target(struct net_device *dev, u16 xid,
4714ea09c9cSYi Zou 				    struct scatterlist *sgl, unsigned int sgc)
4724ea09c9cSYi Zou {
4737da82c06SJiri Pirko 	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
4744ea09c9cSYi Zou 	const struct net_device_ops *ops = real_dev->netdev_ops;
4754ea09c9cSYi Zou 	int rc = 0;
4764ea09c9cSYi Zou 
4774ea09c9cSYi Zou 	if (ops->ndo_fcoe_ddp_target)
4784ea09c9cSYi Zou 		rc = ops->ndo_fcoe_ddp_target(real_dev, xid, sgl, sgc);
4794ea09c9cSYi Zou 
4804ea09c9cSYi Zou 	return rc;
4814ea09c9cSYi Zou }
482b85daa53SVasu Dev #endif
483b85daa53SVasu Dev 
484ef3eb3e5SPatrick McHardy static void vlan_dev_change_rx_flags(struct net_device *dev, int change)
4856c78dcbdSPatrick McHardy {
4867da82c06SJiri Pirko 	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
4876c78dcbdSPatrick McHardy 
488deede2faSMatthijs Kooijman 	if (dev->flags & IFF_UP) {
4896c78dcbdSPatrick McHardy 		if (change & IFF_ALLMULTI)
4906c78dcbdSPatrick McHardy 			dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
4916c78dcbdSPatrick McHardy 		if (change & IFF_PROMISC)
4926c78dcbdSPatrick McHardy 			dev_set_promiscuity(real_dev, dev->flags & IFF_PROMISC ? 1 : -1);
4936c78dcbdSPatrick McHardy 	}
494deede2faSMatthijs Kooijman }
4956c78dcbdSPatrick McHardy 
496dc8eaaa0Sdingtianhong static int vlan_calculate_locking_subclass(struct net_device *real_dev)
497dc8eaaa0Sdingtianhong {
498dc8eaaa0Sdingtianhong 	int subclass = 0;
499dc8eaaa0Sdingtianhong 
500dc8eaaa0Sdingtianhong 	while (is_vlan_dev(real_dev)) {
501dc8eaaa0Sdingtianhong 		subclass++;
502dc8eaaa0Sdingtianhong 		real_dev = vlan_dev_priv(real_dev)->real_dev;
503dc8eaaa0Sdingtianhong 	}
504dc8eaaa0Sdingtianhong 
505dc8eaaa0Sdingtianhong 	return subclass;
506dc8eaaa0Sdingtianhong }
507dc8eaaa0Sdingtianhong 
508dc8eaaa0Sdingtianhong static void vlan_dev_mc_sync(struct net_device *to, struct net_device *from)
509dc8eaaa0Sdingtianhong {
510dc8eaaa0Sdingtianhong 	int err = 0, subclass;
511dc8eaaa0Sdingtianhong 
512dc8eaaa0Sdingtianhong 	subclass = vlan_calculate_locking_subclass(to);
513dc8eaaa0Sdingtianhong 
514dc8eaaa0Sdingtianhong 	spin_lock_nested(&to->addr_list_lock, subclass);
515dc8eaaa0Sdingtianhong 	err = __hw_addr_sync(&to->mc, &from->mc, to->addr_len);
516dc8eaaa0Sdingtianhong 	if (!err)
517dc8eaaa0Sdingtianhong 		__dev_set_rx_mode(to);
518dc8eaaa0Sdingtianhong 	spin_unlock(&to->addr_list_lock);
519dc8eaaa0Sdingtianhong }
520dc8eaaa0Sdingtianhong 
521dc8eaaa0Sdingtianhong static void vlan_dev_uc_sync(struct net_device *to, struct net_device *from)
522dc8eaaa0Sdingtianhong {
523dc8eaaa0Sdingtianhong 	int err = 0, subclass;
524dc8eaaa0Sdingtianhong 
525dc8eaaa0Sdingtianhong 	subclass = vlan_calculate_locking_subclass(to);
526dc8eaaa0Sdingtianhong 
527dc8eaaa0Sdingtianhong 	spin_lock_nested(&to->addr_list_lock, subclass);
528dc8eaaa0Sdingtianhong 	err = __hw_addr_sync(&to->uc, &from->uc, to->addr_len);
529dc8eaaa0Sdingtianhong 	if (!err)
530dc8eaaa0Sdingtianhong 		__dev_set_rx_mode(to);
531dc8eaaa0Sdingtianhong 	spin_unlock(&to->addr_list_lock);
532dc8eaaa0Sdingtianhong }
533dc8eaaa0Sdingtianhong 
534e83a2ea8SChris Leech static void vlan_dev_set_rx_mode(struct net_device *vlan_dev)
5351da177e4SLinus Torvalds {
536dc8eaaa0Sdingtianhong 	vlan_dev_mc_sync(vlan_dev_priv(vlan_dev)->real_dev, vlan_dev);
537dc8eaaa0Sdingtianhong 	vlan_dev_uc_sync(vlan_dev_priv(vlan_dev)->real_dev, vlan_dev);
5381da177e4SLinus Torvalds }
539ef3eb3e5SPatrick McHardy 
540ef3eb3e5SPatrick McHardy /*
541ef3eb3e5SPatrick McHardy  * vlan network devices have devices nesting below it, and are a special
542ef3eb3e5SPatrick McHardy  * "super class" of normal network devices; split their locks off into a
543ef3eb3e5SPatrick McHardy  * separate class since they always nest.
544ef3eb3e5SPatrick McHardy  */
545ef3eb3e5SPatrick McHardy static struct lock_class_key vlan_netdev_xmit_lock_key;
546cf508b12SDavid S. Miller static struct lock_class_key vlan_netdev_addr_lock_key;
547ef3eb3e5SPatrick McHardy 
548e8a0464cSDavid S. Miller static void vlan_dev_set_lockdep_one(struct net_device *dev,
549e8a0464cSDavid S. Miller 				     struct netdev_queue *txq,
550e8a0464cSDavid S. Miller 				     void *_subclass)
551c773e847SDavid S. Miller {
552c773e847SDavid S. Miller 	lockdep_set_class_and_subclass(&txq->_xmit_lock,
553e8a0464cSDavid S. Miller 				       &vlan_netdev_xmit_lock_key,
554e8a0464cSDavid S. Miller 				       *(int *)_subclass);
555c773e847SDavid S. Miller }
556c773e847SDavid S. Miller 
557c773e847SDavid S. Miller static void vlan_dev_set_lockdep_class(struct net_device *dev, int subclass)
558c773e847SDavid S. Miller {
559cf508b12SDavid S. Miller 	lockdep_set_class_and_subclass(&dev->addr_list_lock,
560cf508b12SDavid S. Miller 				       &vlan_netdev_addr_lock_key,
561cf508b12SDavid S. Miller 				       subclass);
562e8a0464cSDavid S. Miller 	netdev_for_each_tx_queue(dev, vlan_dev_set_lockdep_one, &subclass);
563c773e847SDavid S. Miller }
564c773e847SDavid S. Miller 
565ef3eb3e5SPatrick McHardy static const struct header_ops vlan_header_ops = {
566ef3eb3e5SPatrick McHardy 	.create	 = vlan_dev_hard_header,
567ef3eb3e5SPatrick McHardy 	.rebuild = vlan_dev_rebuild_header,
568ef3eb3e5SPatrick McHardy 	.parse	 = eth_header_parse,
569ef3eb3e5SPatrick McHardy };
570ef3eb3e5SPatrick McHardy 
5712205369aSDavid S. Miller static int vlan_passthru_hard_header(struct sk_buff *skb, struct net_device *dev,
5722205369aSDavid S. Miller 				     unsigned short type,
5732205369aSDavid S. Miller 				     const void *daddr, const void *saddr,
5742205369aSDavid S. Miller 				     unsigned int len)
5752205369aSDavid S. Miller {
5762205369aSDavid S. Miller 	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
5772205369aSDavid S. Miller 	struct net_device *real_dev = vlan->real_dev;
5782205369aSDavid S. Miller 
579dd38743bSPeter Boström 	if (saddr == NULL)
580dd38743bSPeter Boström 		saddr = dev->dev_addr;
581dd38743bSPeter Boström 
5822205369aSDavid S. Miller 	return dev_hard_header(skb, real_dev, type, daddr, saddr, len);
5832205369aSDavid S. Miller }
5842205369aSDavid S. Miller 
5852205369aSDavid S. Miller static const struct header_ops vlan_passthru_header_ops = {
5862205369aSDavid S. Miller 	.create	 = vlan_passthru_hard_header,
5872205369aSDavid S. Miller 	.rebuild = dev_rebuild_header,
5882205369aSDavid S. Miller 	.parse	 = eth_header_parse,
5892205369aSDavid S. Miller };
5902205369aSDavid S. Miller 
591e949b09bSDoug Goldstein static struct device_type vlan_type = {
592e949b09bSDoug Goldstein 	.name	= "vlan",
593e949b09bSDoug Goldstein };
594e949b09bSDoug Goldstein 
595213b15caSEric Dumazet static const struct net_device_ops vlan_netdev_ops;
596f7d1b9f5SEric Dumazet 
597ef3eb3e5SPatrick McHardy static int vlan_dev_init(struct net_device *dev)
598ef3eb3e5SPatrick McHardy {
5997da82c06SJiri Pirko 	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
6001c213bd2SWANG Cong 	int subclass = 0;
601ef3eb3e5SPatrick McHardy 
602adc667e8SJay Vosburgh 	netif_carrier_off(dev);
603adc667e8SJay Vosburgh 
604ef3eb3e5SPatrick McHardy 	/* IFF_BROADCAST|IFF_MULTICAST; ??? */
605194dbcc8SJohn Fastabend 	dev->flags  = real_dev->flags & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
606194dbcc8SJohn Fastabend 					  IFF_MASTER | IFF_SLAVE);
607ef3eb3e5SPatrick McHardy 	dev->iflink = real_dev->ifindex;
608ef3eb3e5SPatrick McHardy 	dev->state  = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
609ef3eb3e5SPatrick McHardy 					  (1<<__LINK_STATE_DORMANT))) |
610ef3eb3e5SPatrick McHardy 		      (1<<__LINK_STATE_PRESENT);
611ef3eb3e5SPatrick McHardy 
61262f2a3a4SMichał Mirosław 	dev->hw_features = NETIF_F_ALL_CSUM | NETIF_F_SG |
61362f2a3a4SMichał Mirosław 			   NETIF_F_FRAGLIST | NETIF_F_ALL_TSO |
61462f2a3a4SMichał Mirosław 			   NETIF_F_HIGHDMA | NETIF_F_SCTP_CSUM |
61562f2a3a4SMichał Mirosław 			   NETIF_F_ALL_FCOE;
61662f2a3a4SMichał Mirosław 
6178a0427bbSMichał Mirosław 	dev->features |= real_dev->vlan_features | NETIF_F_LLTX;
6181ae4be22SAlexander Duyck 	dev->gso_max_size = real_dev->gso_max_size;
6192adb956bSVlad Yasevich 	if (dev->features & NETIF_F_VLAN_FEATURES)
6202adb956bSVlad Yasevich 		netdev_warn(real_dev, "VLAN features are set incorrectly.  Q-in-Q configurations may not work correctly.\n");
6212adb956bSVlad Yasevich 
6225fb13570SPatrick McHardy 
623ef3eb3e5SPatrick McHardy 	/* ipv6 shared card related stuff */
624ef3eb3e5SPatrick McHardy 	dev->dev_id = real_dev->dev_id;
625ef3eb3e5SPatrick McHardy 
626ef3eb3e5SPatrick McHardy 	if (is_zero_ether_addr(dev->dev_addr))
6276b93f4a1SBjørn Mork 		eth_hw_addr_inherit(dev, real_dev);
628ef3eb3e5SPatrick McHardy 	if (is_zero_ether_addr(dev->broadcast))
629ef3eb3e5SPatrick McHardy 		memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
630ef3eb3e5SPatrick McHardy 
631f4d5392eSAmerigo Wang #if IS_ENABLED(CONFIG_FCOE)
632b85daa53SVasu Dev 	dev->fcoe_ddp_xid = real_dev->fcoe_ddp_xid;
633b85daa53SVasu Dev #endif
634b85daa53SVasu Dev 
635d870bfb9SEric Dumazet 	dev->needed_headroom = real_dev->needed_headroom;
636fc0d48b8SVlad Yasevich 	if (vlan_hw_offload_capable(real_dev->features,
637fc0d48b8SVlad Yasevich 				    vlan_dev_priv(dev)->vlan_proto)) {
6382205369aSDavid S. Miller 		dev->header_ops      = &vlan_passthru_header_ops;
639ef3eb3e5SPatrick McHardy 		dev->hard_header_len = real_dev->hard_header_len;
640ef3eb3e5SPatrick McHardy 	} else {
641ef3eb3e5SPatrick McHardy 		dev->header_ops      = &vlan_header_ops;
642ef3eb3e5SPatrick McHardy 		dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN;
643636e19a3SJohn Fastabend 	}
644636e19a3SJohn Fastabend 
645f7d1b9f5SEric Dumazet 	dev->netdev_ops = &vlan_netdev_ops;
646ef3eb3e5SPatrick McHardy 
647e949b09bSDoug Goldstein 	SET_NETDEV_DEVTYPE(dev, &vlan_type);
648e949b09bSDoug Goldstein 
649dc8eaaa0Sdingtianhong 	subclass = vlan_calculate_locking_subclass(dev);
650c773e847SDavid S. Miller 	vlan_dev_set_lockdep_class(dev, subclass);
6519793241fSEric Dumazet 
6521c213bd2SWANG Cong 	vlan_dev_priv(dev)->vlan_pcpu_stats = netdev_alloc_pcpu_stats(struct vlan_pcpu_stats);
6537da82c06SJiri Pirko 	if (!vlan_dev_priv(dev)->vlan_pcpu_stats)
6549793241fSEric Dumazet 		return -ENOMEM;
6559793241fSEric Dumazet 
656ef3eb3e5SPatrick McHardy 	return 0;
657ef3eb3e5SPatrick McHardy }
658ef3eb3e5SPatrick McHardy 
65923556323SPavel Emelyanov static void vlan_dev_uninit(struct net_device *dev)
66023556323SPavel Emelyanov {
66123556323SPavel Emelyanov 	struct vlan_priority_tci_mapping *pm;
6627da82c06SJiri Pirko 	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
66323556323SPavel Emelyanov 	int i;
66423556323SPavel Emelyanov 
6654af429d2SEric Dumazet 	free_percpu(vlan->vlan_pcpu_stats);
6664af429d2SEric Dumazet 	vlan->vlan_pcpu_stats = NULL;
66723556323SPavel Emelyanov 	for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) {
66823556323SPavel Emelyanov 		while ((pm = vlan->egress_priority_map[i]) != NULL) {
66923556323SPavel Emelyanov 			vlan->egress_priority_map[i] = pm->next;
67023556323SPavel Emelyanov 			kfree(pm);
67123556323SPavel Emelyanov 		}
67223556323SPavel Emelyanov 	}
67323556323SPavel Emelyanov }
67423556323SPavel Emelyanov 
675c8f44affSMichał Mirosław static netdev_features_t vlan_dev_fix_features(struct net_device *dev,
676c8f44affSMichał Mirosław 	netdev_features_t features)
6778a0427bbSMichał Mirosław {
6787da82c06SJiri Pirko 	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
679b29d3145SBjørn Mork 	netdev_features_t old_features = features;
6808a0427bbSMichał Mirosław 
681f0a619ccSMichał Mirosław 	features &= real_dev->vlan_features;
682bc5787c6SMichał Mirosław 	features |= NETIF_F_RXCSUM;
683bc5787c6SMichał Mirosław 	features &= real_dev->features;
684712ae51aSShan Wei 
685712ae51aSShan Wei 	features |= old_features & NETIF_F_SOFT_FEATURES;
686f0a619ccSMichał Mirosław 	features |= NETIF_F_LLTX;
6878a0427bbSMichał Mirosław 
6888a0427bbSMichał Mirosław 	return features;
6898a0427bbSMichał Mirosław }
6908a0427bbSMichał Mirosław 
691b3020061SStephen Hemminger static int vlan_ethtool_get_settings(struct net_device *dev,
692b3020061SStephen Hemminger 				     struct ethtool_cmd *cmd)
693b3020061SStephen Hemminger {
6947da82c06SJiri Pirko 	const struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
6954bc71cb9SJiri Pirko 
6964bc71cb9SJiri Pirko 	return __ethtool_get_settings(vlan->real_dev, cmd);
697b3020061SStephen Hemminger }
698b3020061SStephen Hemminger 
699b3020061SStephen Hemminger static void vlan_ethtool_get_drvinfo(struct net_device *dev,
700b3020061SStephen Hemminger 				     struct ethtool_drvinfo *info)
701b3020061SStephen Hemminger {
7027826d43fSJiri Pirko 	strlcpy(info->driver, vlan_fullname, sizeof(info->driver));
7037826d43fSJiri Pirko 	strlcpy(info->version, vlan_version, sizeof(info->version));
7047826d43fSJiri Pirko 	strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
705b3020061SStephen Hemminger }
706b3020061SStephen Hemminger 
70728172739SEric Dumazet static struct rtnl_link_stats64 *vlan_dev_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
7089793241fSEric Dumazet {
7099793241fSEric Dumazet 
7107da82c06SJiri Pirko 	if (vlan_dev_priv(dev)->vlan_pcpu_stats) {
7114af429d2SEric Dumazet 		struct vlan_pcpu_stats *p;
7124af429d2SEric Dumazet 		u32 rx_errors = 0, tx_dropped = 0;
7139793241fSEric Dumazet 		int i;
7149793241fSEric Dumazet 
7159793241fSEric Dumazet 		for_each_possible_cpu(i) {
7164af429d2SEric Dumazet 			u64 rxpackets, rxbytes, rxmulticast, txpackets, txbytes;
7179618e2ffSEric Dumazet 			unsigned int start;
7189618e2ffSEric Dumazet 
7197da82c06SJiri Pirko 			p = per_cpu_ptr(vlan_dev_priv(dev)->vlan_pcpu_stats, i);
7209618e2ffSEric Dumazet 			do {
72157a7744eSEric W. Biederman 				start = u64_stats_fetch_begin_irq(&p->syncp);
7229618e2ffSEric Dumazet 				rxpackets	= p->rx_packets;
7239618e2ffSEric Dumazet 				rxbytes		= p->rx_bytes;
7249618e2ffSEric Dumazet 				rxmulticast	= p->rx_multicast;
7254af429d2SEric Dumazet 				txpackets	= p->tx_packets;
7264af429d2SEric Dumazet 				txbytes		= p->tx_bytes;
72757a7744eSEric W. Biederman 			} while (u64_stats_fetch_retry_irq(&p->syncp, start));
7284af429d2SEric Dumazet 
7294af429d2SEric Dumazet 			stats->rx_packets	+= rxpackets;
7304af429d2SEric Dumazet 			stats->rx_bytes		+= rxbytes;
7314af429d2SEric Dumazet 			stats->multicast	+= rxmulticast;
7324af429d2SEric Dumazet 			stats->tx_packets	+= txpackets;
7334af429d2SEric Dumazet 			stats->tx_bytes		+= txbytes;
7344af429d2SEric Dumazet 			/* rx_errors & tx_dropped are u32 */
7354af429d2SEric Dumazet 			rx_errors	+= p->rx_errors;
7364af429d2SEric Dumazet 			tx_dropped	+= p->tx_dropped;
7379793241fSEric Dumazet 		}
7384af429d2SEric Dumazet 		stats->rx_errors  = rx_errors;
7394af429d2SEric Dumazet 		stats->tx_dropped = tx_dropped;
7409793241fSEric Dumazet 	}
7419793241fSEric Dumazet 	return stats;
7429793241fSEric Dumazet }
7439793241fSEric Dumazet 
7446d4cdf47SBenjamin LaHaise #ifdef CONFIG_NET_POLL_CONTROLLER
745de93cb2eSEric Dumazet static void vlan_dev_poll_controller(struct net_device *dev)
7466d4cdf47SBenjamin LaHaise {
7476d4cdf47SBenjamin LaHaise 	return;
7486d4cdf47SBenjamin LaHaise }
7496d4cdf47SBenjamin LaHaise 
750a8779ec1SEric W. Biederman static int vlan_dev_netpoll_setup(struct net_device *dev, struct netpoll_info *npinfo)
7516d4cdf47SBenjamin LaHaise {
752f3da3893SAmerigo Wang 	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
753f3da3893SAmerigo Wang 	struct net_device *real_dev = vlan->real_dev;
7546d4cdf47SBenjamin LaHaise 	struct netpoll *netpoll;
7556d4cdf47SBenjamin LaHaise 	int err = 0;
7566d4cdf47SBenjamin LaHaise 
757a8779ec1SEric W. Biederman 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
7586d4cdf47SBenjamin LaHaise 	err = -ENOMEM;
7596d4cdf47SBenjamin LaHaise 	if (!netpoll)
7606d4cdf47SBenjamin LaHaise 		goto out;
7616d4cdf47SBenjamin LaHaise 
762a8779ec1SEric W. Biederman 	err = __netpoll_setup(netpoll, real_dev);
7636d4cdf47SBenjamin LaHaise 	if (err) {
7646d4cdf47SBenjamin LaHaise 		kfree(netpoll);
7656d4cdf47SBenjamin LaHaise 		goto out;
7666d4cdf47SBenjamin LaHaise 	}
7676d4cdf47SBenjamin LaHaise 
768f3da3893SAmerigo Wang 	vlan->netpoll = netpoll;
7696d4cdf47SBenjamin LaHaise 
7706d4cdf47SBenjamin LaHaise out:
7716d4cdf47SBenjamin LaHaise 	return err;
7726d4cdf47SBenjamin LaHaise }
7736d4cdf47SBenjamin LaHaise 
774de93cb2eSEric Dumazet static void vlan_dev_netpoll_cleanup(struct net_device *dev)
7756d4cdf47SBenjamin LaHaise {
776f3da3893SAmerigo Wang 	struct vlan_dev_priv *vlan= vlan_dev_priv(dev);
777f3da3893SAmerigo Wang 	struct netpoll *netpoll = vlan->netpoll;
7786d4cdf47SBenjamin LaHaise 
7796d4cdf47SBenjamin LaHaise 	if (!netpoll)
7806d4cdf47SBenjamin LaHaise 		return;
7816d4cdf47SBenjamin LaHaise 
782f3da3893SAmerigo Wang 	vlan->netpoll = NULL;
7836d4cdf47SBenjamin LaHaise 
7842cde6acdSNeil Horman 	__netpoll_free_async(netpoll);
7856d4cdf47SBenjamin LaHaise }
7866d4cdf47SBenjamin LaHaise #endif /* CONFIG_NET_POLL_CONTROLLER */
7876d4cdf47SBenjamin LaHaise 
78875b8846aSPatrick McHardy static const struct ethtool_ops vlan_ethtool_ops = {
789b3020061SStephen Hemminger 	.get_settings	        = vlan_ethtool_get_settings,
790b3020061SStephen Hemminger 	.get_drvinfo	        = vlan_ethtool_get_drvinfo,
79175b8846aSPatrick McHardy 	.get_link		= ethtool_op_get_link,
79275b8846aSPatrick McHardy };
79375b8846aSPatrick McHardy 
794656299f7SStephen Hemminger static const struct net_device_ops vlan_netdev_ops = {
795656299f7SStephen Hemminger 	.ndo_change_mtu		= vlan_dev_change_mtu,
796656299f7SStephen Hemminger 	.ndo_init		= vlan_dev_init,
797656299f7SStephen Hemminger 	.ndo_uninit		= vlan_dev_uninit,
798656299f7SStephen Hemminger 	.ndo_open		= vlan_dev_open,
799656299f7SStephen Hemminger 	.ndo_stop		= vlan_dev_stop,
800f7d1b9f5SEric Dumazet 	.ndo_start_xmit =  vlan_dev_hard_start_xmit,
801f7d1b9f5SEric Dumazet 	.ndo_validate_addr	= eth_validate_addr,
802f7d1b9f5SEric Dumazet 	.ndo_set_mac_address	= vlan_dev_set_mac_address,
803f7d1b9f5SEric Dumazet 	.ndo_set_rx_mode	= vlan_dev_set_rx_mode,
804f7d1b9f5SEric Dumazet 	.ndo_change_rx_flags	= vlan_dev_change_rx_flags,
805f7d1b9f5SEric Dumazet 	.ndo_do_ioctl		= vlan_dev_ioctl,
806cc883d16SFrank Blaschka 	.ndo_neigh_setup	= vlan_dev_neigh_setup,
8079618e2ffSEric Dumazet 	.ndo_get_stats64	= vlan_dev_get_stats64,
808f4d5392eSAmerigo Wang #if IS_ENABLED(CONFIG_FCOE)
809b85daa53SVasu Dev 	.ndo_fcoe_ddp_setup	= vlan_dev_fcoe_ddp_setup,
810b85daa53SVasu Dev 	.ndo_fcoe_ddp_done	= vlan_dev_fcoe_ddp_done,
8110af46d99SYi Zou 	.ndo_fcoe_enable	= vlan_dev_fcoe_enable,
8120af46d99SYi Zou 	.ndo_fcoe_disable	= vlan_dev_fcoe_disable,
813d6534799SYi Zou 	.ndo_fcoe_get_wwn	= vlan_dev_fcoe_get_wwn,
8144ea09c9cSYi Zou 	.ndo_fcoe_ddp_target	= vlan_dev_fcoe_ddp_target,
815b85daa53SVasu Dev #endif
8166d4cdf47SBenjamin LaHaise #ifdef CONFIG_NET_POLL_CONTROLLER
8176d4cdf47SBenjamin LaHaise 	.ndo_poll_controller	= vlan_dev_poll_controller,
8186d4cdf47SBenjamin LaHaise 	.ndo_netpoll_setup	= vlan_dev_netpoll_setup,
8196d4cdf47SBenjamin LaHaise 	.ndo_netpoll_cleanup	= vlan_dev_netpoll_cleanup,
8206d4cdf47SBenjamin LaHaise #endif
8218a0427bbSMichał Mirosław 	.ndo_fix_features	= vlan_dev_fix_features,
822f7d1b9f5SEric Dumazet };
823f7d1b9f5SEric Dumazet 
824ef3eb3e5SPatrick McHardy void vlan_setup(struct net_device *dev)
825ef3eb3e5SPatrick McHardy {
826ef3eb3e5SPatrick McHardy 	ether_setup(dev);
827ef3eb3e5SPatrick McHardy 
828ef3eb3e5SPatrick McHardy 	dev->priv_flags		|= IFF_802_1Q_VLAN;
829550fd08cSNeil Horman 	dev->priv_flags		&= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
830ef3eb3e5SPatrick McHardy 	dev->tx_queue_len	= 0;
831ef3eb3e5SPatrick McHardy 
832656299f7SStephen Hemminger 	dev->netdev_ops		= &vlan_netdev_ops;
833ef3eb3e5SPatrick McHardy 	dev->destructor		= free_netdev;
83475b8846aSPatrick McHardy 	dev->ethtool_ops	= &vlan_ethtool_ops;
835ef3eb3e5SPatrick McHardy 
836ef3eb3e5SPatrick McHardy 	memset(dev->broadcast, 0, ETH_ALEN);
837ef3eb3e5SPatrick McHardy }
838