xref: /linux/net/8021q/vlan_dev.c (revision cd1c188db1091991fc1d7f565824d077d659425b)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* -*- linux-c -*-
3  * INET		802.1Q VLAN
4  *		Ethernet-type device handling.
5  *
6  * Authors:	Ben Greear <greearb@candelatech.com>
7  *              Please send support related email to: netdev@vger.kernel.org
8  *              VLAN Home Page: http://www.candelatech.com/~greear/vlan.html
9  *
10  * Fixes:       Mar 22 2001: Martin Bokaemper <mbokaemper@unispherenetworks.com>
11  *                - reset skb->pkt_type on incoming packets when MAC was changed
12  *                - see that changed MAC is saddr for outgoing packets
13  *              Oct 20, 2001:  Ard van Breeman:
14  *                - Fix MC-list, finally.
15  *                - Flush MC-list on VLAN destroy.
16  */
17 
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/skbuff.h>
23 #include <linux/netdevice.h>
24 #include <linux/net_tstamp.h>
25 #include <linux/etherdevice.h>
26 #include <linux/ethtool.h>
27 #include <linux/phy.h>
28 #include <net/arp.h>
29 #include <net/macsec.h>
30 #include <net/netdev_lock.h>
31 
32 #include "vlan.h"
33 #include "vlanproc.h"
34 #include <linux/if_vlan.h>
35 #include <linux/netpoll.h>
36 
37 /*
38  *	Create the VLAN header for an arbitrary protocol layer
39  *
40  *	saddr=NULL	means use device source address
41  *	daddr=NULL	means leave destination address (eg unresolved arp)
42  *
43  *  This is called when the SKB is moving down the stack towards the
44  *  physical devices.
45  */
46 static int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
47 				unsigned short type,
48 				const void *daddr, const void *saddr,
49 				unsigned int len)
50 {
51 	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
52 	struct vlan_hdr *vhdr;
53 	unsigned int vhdrlen = 0;
54 	u16 vlan_tci = 0;
55 	int rc;
56 
57 	if (!(vlan->flags & VLAN_FLAG_REORDER_HDR)) {
58 		vhdr = skb_push(skb, VLAN_HLEN);
59 
60 		vlan_tci = vlan->vlan_id;
61 		vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb->priority);
62 		vhdr->h_vlan_TCI = htons(vlan_tci);
63 
64 		/*
65 		 *  Set the protocol type. For a packet of type ETH_P_802_3/2 we
66 		 *  put the length in here instead.
67 		 */
68 		if (type != ETH_P_802_3 && type != ETH_P_802_2)
69 			vhdr->h_vlan_encapsulated_proto = htons(type);
70 		else
71 			vhdr->h_vlan_encapsulated_proto = htons(len);
72 
73 		skb->protocol = vlan->vlan_proto;
74 		type = ntohs(vlan->vlan_proto);
75 		vhdrlen = VLAN_HLEN;
76 	}
77 
78 	/* Before delegating work to the lower layer, enter our MAC-address */
79 	if (saddr == NULL)
80 		saddr = dev->dev_addr;
81 
82 	/* Now make the underlying real hard header */
83 	dev = vlan->real_dev;
84 	rc = dev_hard_header(skb, dev, type, daddr, saddr, len + vhdrlen);
85 	if (rc > 0)
86 		rc += vhdrlen;
87 	return rc;
88 }
89 
90 static inline netdev_tx_t vlan_netpoll_send_skb(struct vlan_dev_priv *vlan, struct sk_buff *skb)
91 {
92 #ifdef CONFIG_NET_POLL_CONTROLLER
93 	return netpoll_send_skb(vlan->netpoll, skb);
94 #else
95 	BUG();
96 	return NETDEV_TX_OK;
97 #endif
98 }
99 
100 static netdev_tx_t vlan_dev_hard_start_xmit(struct sk_buff *skb,
101 					    struct net_device *dev)
102 {
103 	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
104 	struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
105 	unsigned int len;
106 	int ret;
107 
108 	/* Handle non-VLAN frames if they are sent to us, for example by DHCP.
109 	 *
110 	 * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING
111 	 * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs...
112 	 */
113 	if (vlan->flags & VLAN_FLAG_REORDER_HDR ||
114 	    veth->h_vlan_proto != vlan->vlan_proto) {
115 		u16 vlan_tci;
116 		vlan_tci = vlan->vlan_id;
117 		vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb->priority);
118 		__vlan_hwaccel_put_tag(skb, vlan->vlan_proto, vlan_tci);
119 	}
120 
121 	skb->dev = vlan->real_dev;
122 	len = skb->len;
123 	if (unlikely(netpoll_tx_running(dev)))
124 		return vlan_netpoll_send_skb(vlan, skb);
125 
126 	ret = dev_queue_xmit(skb);
127 
128 	if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
129 		struct vlan_pcpu_stats *stats;
130 
131 		stats = this_cpu_ptr(vlan->vlan_pcpu_stats);
132 		u64_stats_update_begin(&stats->syncp);
133 		u64_stats_inc(&stats->tx_packets);
134 		u64_stats_add(&stats->tx_bytes, len);
135 		u64_stats_update_end(&stats->syncp);
136 	} else {
137 		this_cpu_inc(vlan->vlan_pcpu_stats->tx_dropped);
138 	}
139 
140 	return ret;
141 }
142 
143 static int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
144 {
145 	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
146 	unsigned int max_mtu = real_dev->mtu;
147 
148 	if (netif_reduces_vlan_mtu(real_dev))
149 		max_mtu -= VLAN_HLEN;
150 	if (max_mtu < new_mtu)
151 		return -ERANGE;
152 
153 	WRITE_ONCE(dev->mtu, new_mtu);
154 
155 	return 0;
156 }
157 
158 void vlan_dev_set_ingress_priority(const struct net_device *dev,
159 				   u32 skb_prio, u16 vlan_prio)
160 {
161 	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
162 
163 	if (vlan->ingress_priority_map[vlan_prio & 0x7] && !skb_prio)
164 		vlan->nr_ingress_mappings--;
165 	else if (!vlan->ingress_priority_map[vlan_prio & 0x7] && skb_prio)
166 		vlan->nr_ingress_mappings++;
167 
168 	vlan->ingress_priority_map[vlan_prio & 0x7] = skb_prio;
169 }
170 
171 int vlan_dev_set_egress_priority(const struct net_device *dev,
172 				 u32 skb_prio, u16 vlan_prio)
173 {
174 	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
175 	struct vlan_priority_tci_mapping __rcu **mpp;
176 	struct vlan_priority_tci_mapping *mp;
177 	struct vlan_priority_tci_mapping *np;
178 	u32 bucket = skb_prio & 0xF;
179 	u32 vlan_qos = (vlan_prio << VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK;
180 
181 	/* See if a priority mapping exists.. */
182 	mpp = &vlan->egress_priority_map[bucket];
183 	mp = rtnl_dereference(*mpp);
184 	while (mp) {
185 		if (mp->priority == skb_prio) {
186 			if (!vlan_qos) {
187 				rcu_assign_pointer(*mpp, rtnl_dereference(mp->next));
188 				vlan->nr_egress_mappings--;
189 				kfree_rcu(mp, rcu);
190 			} else {
191 				WRITE_ONCE(mp->vlan_qos, vlan_qos);
192 			}
193 			return 0;
194 		}
195 		mpp = &mp->next;
196 		mp = rtnl_dereference(*mpp);
197 	}
198 
199 	/* Create a new mapping then. */
200 	if (!vlan_qos)
201 		return 0;
202 
203 	np = kmalloc_obj(struct vlan_priority_tci_mapping);
204 	if (!np)
205 		return -ENOBUFS;
206 
207 	np->priority = skb_prio;
208 	np->vlan_qos = vlan_qos;
209 	RCU_INIT_POINTER(np->next, rtnl_dereference(vlan->egress_priority_map[bucket]));
210 	rcu_assign_pointer(vlan->egress_priority_map[bucket], np);
211 	if (vlan_qos)
212 		vlan->nr_egress_mappings++;
213 	return 0;
214 }
215 
216 /* Flags are defined in the vlan_flags enum in
217  * include/uapi/linux/if_vlan.h file.
218  */
219 int vlan_dev_change_flags(const struct net_device *dev, u32 flags, u32 mask)
220 {
221 	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
222 	u32 old_flags = vlan->flags;
223 
224 	if (mask & ~(VLAN_FLAG_REORDER_HDR | VLAN_FLAG_GVRP |
225 		     VLAN_FLAG_LOOSE_BINDING | VLAN_FLAG_MVRP |
226 		     VLAN_FLAG_BRIDGE_BINDING))
227 		return -EINVAL;
228 
229 	vlan->flags = (old_flags & ~mask) | (flags & mask);
230 
231 	if (netif_running(dev) && (vlan->flags ^ old_flags) & VLAN_FLAG_GVRP) {
232 		if (vlan->flags & VLAN_FLAG_GVRP)
233 			vlan_gvrp_request_join(dev);
234 		else
235 			vlan_gvrp_request_leave(dev);
236 	}
237 
238 	if (netif_running(dev) && (vlan->flags ^ old_flags) & VLAN_FLAG_MVRP) {
239 		if (vlan->flags & VLAN_FLAG_MVRP)
240 			vlan_mvrp_request_join(dev);
241 		else
242 			vlan_mvrp_request_leave(dev);
243 	}
244 	return 0;
245 }
246 
247 void vlan_dev_get_realdev_name(const struct net_device *dev, char *result, size_t size)
248 {
249 	strscpy_pad(result, vlan_dev_priv(dev)->real_dev->name, size);
250 }
251 
252 bool vlan_dev_inherit_address(struct net_device *dev,
253 			      struct net_device *real_dev)
254 {
255 	if (dev->addr_assign_type != NET_ADDR_STOLEN)
256 		return false;
257 
258 	eth_hw_addr_set(dev, real_dev->dev_addr);
259 	call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
260 	return true;
261 }
262 
263 static int vlan_dev_open(struct net_device *dev)
264 {
265 	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
266 	struct net_device *real_dev = vlan->real_dev;
267 	int err;
268 
269 	if (!(real_dev->flags & IFF_UP) &&
270 	    !(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
271 		return -ENETDOWN;
272 
273 	/* The explicit open supersedes any deferred link-state sync */
274 	netdev_work_cancel(dev, VLAN_WORK_LINK_STATE);
275 
276 	if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr) &&
277 	    !vlan_dev_inherit_address(dev, real_dev)) {
278 		err = dev_uc_add(real_dev, dev->dev_addr);
279 		if (err < 0)
280 			goto out;
281 	}
282 
283 	ether_addr_copy(vlan->real_dev_addr, real_dev->dev_addr);
284 
285 	if (vlan->flags & VLAN_FLAG_GVRP)
286 		vlan_gvrp_request_join(dev);
287 
288 	if (vlan->flags & VLAN_FLAG_MVRP)
289 		vlan_mvrp_request_join(dev);
290 
291 	if (netif_carrier_ok(real_dev) &&
292 	    !(vlan->flags & VLAN_FLAG_BRIDGE_BINDING))
293 		netif_carrier_on(dev);
294 	return 0;
295 
296 out:
297 	netif_carrier_off(dev);
298 	return err;
299 }
300 
301 static int vlan_dev_stop(struct net_device *dev)
302 {
303 	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
304 	struct net_device *real_dev = vlan->real_dev;
305 
306 	/* The explicit close supersedes any deferred link-state sync */
307 	netdev_work_cancel(dev, VLAN_WORK_LINK_STATE);
308 
309 	dev_mc_unsync(real_dev, dev);
310 	dev_uc_unsync(real_dev, dev);
311 
312 	if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr))
313 		dev_uc_del(real_dev, dev->dev_addr);
314 
315 	if (!(vlan->flags & VLAN_FLAG_BRIDGE_BINDING))
316 		netif_carrier_off(dev);
317 	return 0;
318 }
319 
320 static int vlan_dev_set_mac_address(struct net_device *dev, void *p)
321 {
322 	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
323 	struct sockaddr *addr = p;
324 	int err;
325 
326 	if (!is_valid_ether_addr(addr->sa_data))
327 		return -EADDRNOTAVAIL;
328 
329 	if (!(dev->flags & IFF_UP))
330 		goto out;
331 
332 	if (!ether_addr_equal(addr->sa_data, real_dev->dev_addr)) {
333 		err = dev_uc_add(real_dev, addr->sa_data);
334 		if (err < 0)
335 			return err;
336 	}
337 
338 	if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr))
339 		dev_uc_del(real_dev, dev->dev_addr);
340 
341 out:
342 	eth_hw_addr_set(dev, addr->sa_data);
343 	return 0;
344 }
345 
346 static int vlan_hwtstamp_get(struct net_device *dev,
347 			     struct kernel_hwtstamp_config *cfg)
348 {
349 	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
350 
351 	return generic_hwtstamp_get_lower(real_dev, cfg);
352 }
353 
354 static int vlan_hwtstamp_set(struct net_device *dev,
355 			     struct kernel_hwtstamp_config *cfg,
356 			     struct netlink_ext_ack *extack)
357 {
358 	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
359 
360 	if (!net_eq(dev_net(dev), dev_net(real_dev)))
361 		return -EOPNOTSUPP;
362 
363 	return generic_hwtstamp_set_lower(real_dev, cfg, extack);
364 }
365 
366 static int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
367 {
368 	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
369 	struct ifreq ifrr;
370 	int err = -EOPNOTSUPP;
371 
372 	strscpy_pad(ifrr.ifr_name, real_dev->name, IFNAMSIZ);
373 	ifrr.ifr_ifru = ifr->ifr_ifru;
374 
375 	switch (cmd) {
376 	case SIOCGMIIPHY:
377 	case SIOCGMIIREG:
378 	case SIOCSMIIREG:
379 		err = dev_eth_ioctl(real_dev, &ifrr, cmd);
380 		break;
381 	}
382 
383 	if (!err)
384 		ifr->ifr_ifru = ifrr.ifr_ifru;
385 
386 	return err;
387 }
388 
389 static int vlan_dev_neigh_setup(struct net_device *dev, struct neigh_parms *pa)
390 {
391 	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
392 	const struct net_device_ops *ops = real_dev->netdev_ops;
393 	int err = 0;
394 
395 	if (netif_device_present(real_dev) && ops->ndo_neigh_setup)
396 		err = ops->ndo_neigh_setup(real_dev, pa);
397 
398 	return err;
399 }
400 
401 #if IS_ENABLED(CONFIG_FCOE)
402 static int vlan_dev_fcoe_ddp_setup(struct net_device *dev, u16 xid,
403 				   struct scatterlist *sgl, unsigned int sgc)
404 {
405 	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
406 	const struct net_device_ops *ops = real_dev->netdev_ops;
407 	int rc = 0;
408 
409 	if (ops->ndo_fcoe_ddp_setup)
410 		rc = ops->ndo_fcoe_ddp_setup(real_dev, xid, sgl, sgc);
411 
412 	return rc;
413 }
414 
415 static int vlan_dev_fcoe_ddp_done(struct net_device *dev, u16 xid)
416 {
417 	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
418 	const struct net_device_ops *ops = real_dev->netdev_ops;
419 	int len = 0;
420 
421 	if (ops->ndo_fcoe_ddp_done)
422 		len = ops->ndo_fcoe_ddp_done(real_dev, xid);
423 
424 	return len;
425 }
426 
427 static int vlan_dev_fcoe_enable(struct net_device *dev)
428 {
429 	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
430 	const struct net_device_ops *ops = real_dev->netdev_ops;
431 	int rc = -EINVAL;
432 
433 	if (ops->ndo_fcoe_enable)
434 		rc = ops->ndo_fcoe_enable(real_dev);
435 	return rc;
436 }
437 
438 static int vlan_dev_fcoe_disable(struct net_device *dev)
439 {
440 	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
441 	const struct net_device_ops *ops = real_dev->netdev_ops;
442 	int rc = -EINVAL;
443 
444 	if (ops->ndo_fcoe_disable)
445 		rc = ops->ndo_fcoe_disable(real_dev);
446 	return rc;
447 }
448 
449 static int vlan_dev_fcoe_ddp_target(struct net_device *dev, u16 xid,
450 				    struct scatterlist *sgl, unsigned int sgc)
451 {
452 	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
453 	const struct net_device_ops *ops = real_dev->netdev_ops;
454 	int rc = 0;
455 
456 	if (ops->ndo_fcoe_ddp_target)
457 		rc = ops->ndo_fcoe_ddp_target(real_dev, xid, sgl, sgc);
458 
459 	return rc;
460 }
461 #endif
462 
463 #ifdef NETDEV_FCOE_WWNN
464 static int vlan_dev_fcoe_get_wwn(struct net_device *dev, u64 *wwn, int type)
465 {
466 	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
467 	const struct net_device_ops *ops = real_dev->netdev_ops;
468 	int rc = -EINVAL;
469 
470 	if (ops->ndo_fcoe_get_wwn)
471 		rc = ops->ndo_fcoe_get_wwn(real_dev, wwn, type);
472 	return rc;
473 }
474 #endif
475 
476 static void vlan_dev_change_rx_flags(struct net_device *dev, int change)
477 {
478 	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
479 
480 	if (change & IFF_ALLMULTI)
481 		dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
482 	if (change & IFF_PROMISC)
483 		dev_set_promiscuity(real_dev, dev->flags & IFF_PROMISC ? 1 : -1);
484 }
485 
486 static void vlan_dev_set_rx_mode(struct net_device *vlan_dev)
487 {
488 	dev_mc_sync(vlan_dev_priv(vlan_dev)->real_dev, vlan_dev);
489 	dev_uc_sync(vlan_dev_priv(vlan_dev)->real_dev, vlan_dev);
490 }
491 
492 static __be16 vlan_parse_protocol(const struct sk_buff *skb)
493 {
494 	struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
495 
496 	return __vlan_get_protocol(skb, veth->h_vlan_proto, NULL);
497 }
498 
499 static const struct header_ops vlan_header_ops = {
500 	.create	 = vlan_dev_hard_header,
501 	.parse	 = eth_header_parse,
502 	.parse_protocol = vlan_parse_protocol,
503 };
504 
505 static int vlan_passthru_hard_header(struct sk_buff *skb, struct net_device *dev,
506 				     unsigned short type,
507 				     const void *daddr, const void *saddr,
508 				     unsigned int len)
509 {
510 	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
511 	struct net_device *real_dev = vlan->real_dev;
512 
513 	if (saddr == NULL)
514 		saddr = dev->dev_addr;
515 
516 	return dev_hard_header(skb, real_dev, type, daddr, saddr, len);
517 }
518 
519 static const struct header_ops vlan_passthru_header_ops = {
520 	.create	 = vlan_passthru_hard_header,
521 	.parse	 = eth_header_parse,
522 	.parse_protocol = vlan_parse_protocol,
523 };
524 
525 static const struct device_type vlan_type = {
526 	.name	= "vlan",
527 };
528 
529 static const struct net_device_ops vlan_netdev_ops;
530 
531 static int vlan_dev_init(struct net_device *dev)
532 {
533 	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
534 	struct net_device *real_dev = vlan->real_dev;
535 
536 	netif_carrier_off(dev);
537 
538 	/* IFF_BROADCAST|IFF_MULTICAST; ??? */
539 	dev->flags  = real_dev->flags & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
540 					  IFF_MASTER | IFF_SLAVE);
541 	dev->state  = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
542 					  (1<<__LINK_STATE_DORMANT))) |
543 		      (1<<__LINK_STATE_PRESENT);
544 
545 	if (vlan->flags & VLAN_FLAG_BRIDGE_BINDING)
546 		dev->state |= (1 << __LINK_STATE_NOCARRIER);
547 
548 	dev->hw_features = NETIF_F_HW_CSUM | NETIF_F_SG |
549 			   NETIF_F_FRAGLIST | NETIF_F_GSO_SOFTWARE |
550 			   NETIF_F_GSO_ENCAP_ALL |
551 			   NETIF_F_HIGHDMA | NETIF_F_SCTP_CRC |
552 			   NETIF_F_FCOE_CRC | NETIF_F_FSO;
553 
554 	if (real_dev->vlan_features & NETIF_F_HW_MACSEC)
555 		dev->hw_features |= NETIF_F_HW_MACSEC;
556 
557 	dev->features |= dev->hw_features;
558 	dev->lltx = true;
559 	dev->fcoe_mtu = true;
560 	netif_inherit_tso_max(dev, real_dev);
561 	if (dev->features & NETIF_F_VLAN_FEATURES)
562 		netdev_warn(real_dev, "VLAN features are set incorrectly.  Q-in-Q configurations may not work correctly.\n");
563 
564 	dev->vlan_features = real_dev->vlan_features &
565 			     ~(NETIF_F_FCOE_CRC | NETIF_F_FSO);
566 	dev->hw_enc_features = vlan_tnl_features(real_dev);
567 	dev->mpls_features = real_dev->mpls_features;
568 
569 	/* ipv6 shared card related stuff */
570 	dev->dev_id = real_dev->dev_id;
571 
572 	if (is_zero_ether_addr(dev->dev_addr)) {
573 		eth_hw_addr_set(dev, real_dev->dev_addr);
574 		dev->addr_assign_type = NET_ADDR_STOLEN;
575 	}
576 	if (is_zero_ether_addr(dev->broadcast))
577 		memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
578 
579 #if IS_ENABLED(CONFIG_FCOE)
580 	dev->fcoe_ddp_xid = real_dev->fcoe_ddp_xid;
581 #endif
582 
583 	dev->needed_headroom = real_dev->needed_headroom;
584 	if (vlan_hw_offload_capable(real_dev->features, vlan->vlan_proto)) {
585 		dev->header_ops      = &vlan_passthru_header_ops;
586 		dev->hard_header_len = real_dev->hard_header_len;
587 	} else {
588 		dev->header_ops      = &vlan_header_ops;
589 		dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN;
590 	}
591 
592 	dev->netdev_ops = &vlan_netdev_ops;
593 
594 	SET_NETDEV_DEVTYPE(dev, &vlan_type);
595 
596 	netdev_lockdep_set_classes(dev);
597 
598 	vlan->vlan_pcpu_stats = netdev_alloc_pcpu_stats(struct vlan_pcpu_stats);
599 	if (!vlan->vlan_pcpu_stats)
600 		return -ENOMEM;
601 
602 	/* Get vlan's reference to real_dev */
603 	netdev_hold(real_dev, &vlan->dev_tracker, GFP_KERNEL);
604 
605 	return 0;
606 }
607 
608 /* Note: this function might be called multiple times for the same device. */
609 void vlan_dev_free_egress_priority(const struct net_device *dev)
610 {
611 	struct vlan_priority_tci_mapping *pm;
612 	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
613 	int i;
614 
615 	for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) {
616 		pm = rtnl_dereference(vlan->egress_priority_map[i]);
617 		RCU_INIT_POINTER(vlan->egress_priority_map[i], NULL);
618 		while (pm) {
619 			struct vlan_priority_tci_mapping *next;
620 
621 			next = rtnl_dereference(pm->next);
622 			kfree_rcu(pm, rcu);
623 			pm = next;
624 		}
625 	}
626 	vlan->nr_egress_mappings = 0;
627 }
628 
629 static void vlan_dev_uninit(struct net_device *dev)
630 {
631 	vlan_dev_free_egress_priority(dev);
632 }
633 
634 static netdev_features_t vlan_dev_fix_features(struct net_device *dev,
635 	netdev_features_t features)
636 {
637 	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
638 	netdev_features_t old_features = features;
639 	netdev_features_t lower_features;
640 
641 	lower_features = netdev_intersect_features((real_dev->vlan_features |
642 						    NETIF_F_RXCSUM),
643 						   real_dev->features);
644 
645 	/* Add HW_CSUM setting to preserve user ability to control
646 	 * checksum offload on the vlan device.
647 	 */
648 	if (lower_features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))
649 		lower_features |= NETIF_F_HW_CSUM;
650 	features = netdev_intersect_features(features, lower_features);
651 	features |= old_features & (NETIF_F_SOFT_FEATURES | NETIF_F_GSO_SOFTWARE);
652 
653 	return features;
654 }
655 
656 static int vlan_ethtool_get_link_ksettings(struct net_device *dev,
657 					   struct ethtool_link_ksettings *cmd)
658 {
659 	const struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
660 
661 	return __ethtool_get_link_ksettings(vlan->real_dev, cmd);
662 }
663 
664 static void vlan_ethtool_get_drvinfo(struct net_device *dev,
665 				     struct ethtool_drvinfo *info)
666 {
667 	strscpy(info->driver, vlan_fullname, sizeof(info->driver));
668 	strscpy(info->version, vlan_version, sizeof(info->version));
669 	strscpy(info->fw_version, "N/A", sizeof(info->fw_version));
670 }
671 
672 static int vlan_ethtool_get_ts_info(struct net_device *dev,
673 				    struct kernel_ethtool_ts_info *info)
674 {
675 	const struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
676 	return ethtool_get_ts_info_by_layer(vlan->real_dev, info);
677 }
678 
679 static void vlan_dev_get_stats64(struct net_device *dev,
680 				 struct rtnl_link_stats64 *stats)
681 {
682 	struct vlan_pcpu_stats *p;
683 	u32 rx_errors = 0, tx_dropped = 0;
684 	int i;
685 
686 	for_each_possible_cpu(i) {
687 		u64 rxpackets, rxbytes, rxmulticast, txpackets, txbytes;
688 		unsigned int start;
689 
690 		p = per_cpu_ptr(vlan_dev_priv(dev)->vlan_pcpu_stats, i);
691 		do {
692 			start = u64_stats_fetch_begin(&p->syncp);
693 			rxpackets	= u64_stats_read(&p->rx_packets);
694 			rxbytes		= u64_stats_read(&p->rx_bytes);
695 			rxmulticast	= u64_stats_read(&p->rx_multicast);
696 			txpackets	= u64_stats_read(&p->tx_packets);
697 			txbytes		= u64_stats_read(&p->tx_bytes);
698 		} while (u64_stats_fetch_retry(&p->syncp, start));
699 
700 		stats->rx_packets	+= rxpackets;
701 		stats->rx_bytes		+= rxbytes;
702 		stats->multicast	+= rxmulticast;
703 		stats->tx_packets	+= txpackets;
704 		stats->tx_bytes		+= txbytes;
705 		/* rx_errors & tx_dropped are u32 */
706 		rx_errors	+= READ_ONCE(p->rx_errors);
707 		tx_dropped	+= READ_ONCE(p->tx_dropped);
708 	}
709 	stats->rx_errors  = rx_errors;
710 	stats->tx_dropped = tx_dropped;
711 }
712 
713 #ifdef CONFIG_NET_POLL_CONTROLLER
714 static void vlan_dev_poll_controller(struct net_device *dev)
715 {
716 	return;
717 }
718 
719 static int vlan_dev_netpoll_setup(struct net_device *dev)
720 {
721 	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
722 	struct net_device *real_dev = vlan->real_dev;
723 	struct netpoll *netpoll;
724 	int err = 0;
725 
726 	netpoll = kzalloc_obj(*netpoll);
727 	err = -ENOMEM;
728 	if (!netpoll)
729 		goto out;
730 
731 	err = __netpoll_setup(netpoll, real_dev);
732 	if (err) {
733 		kfree(netpoll);
734 		goto out;
735 	}
736 
737 	vlan->netpoll = netpoll;
738 
739 out:
740 	return err;
741 }
742 
743 static void vlan_dev_netpoll_cleanup(struct net_device *dev)
744 {
745 	struct vlan_dev_priv *vlan= vlan_dev_priv(dev);
746 	struct netpoll *netpoll = vlan->netpoll;
747 
748 	if (!netpoll)
749 		return;
750 
751 	vlan->netpoll = NULL;
752 	__netpoll_free(netpoll);
753 }
754 #endif /* CONFIG_NET_POLL_CONTROLLER */
755 
756 static int vlan_dev_get_iflink(const struct net_device *dev)
757 {
758 	const struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
759 
760 	return READ_ONCE(real_dev->ifindex);
761 }
762 
763 static int vlan_dev_fill_forward_path(struct net_device_path_ctx *ctx,
764 				      struct net_device_path *path)
765 {
766 	struct vlan_dev_priv *vlan = vlan_dev_priv(ctx->dev);
767 
768 	path->type = DEV_PATH_VLAN;
769 	path->encap.id = vlan->vlan_id;
770 	path->encap.proto = vlan->vlan_proto;
771 	path->dev = ctx->dev;
772 	ctx->dev = vlan->real_dev;
773 	if (ctx->num_vlans >= ARRAY_SIZE(ctx->vlan))
774 		return -ENOSPC;
775 
776 	ctx->vlan[ctx->num_vlans].id = vlan->vlan_id;
777 	ctx->vlan[ctx->num_vlans].proto = vlan->vlan_proto;
778 	ctx->num_vlans++;
779 
780 	return 0;
781 }
782 
783 #if IS_ENABLED(CONFIG_MACSEC)
784 
785 static const struct macsec_ops *vlan_get_macsec_ops(const struct macsec_context *ctx)
786 {
787 	return vlan_dev_priv(ctx->netdev)->real_dev->macsec_ops;
788 }
789 
790 static int vlan_macsec_offload(int (* const func)(struct macsec_context *),
791 			       struct macsec_context *ctx)
792 {
793 	if (unlikely(!func))
794 		return 0;
795 
796 	return (*func)(ctx);
797 }
798 
799 static int vlan_macsec_dev_open(struct macsec_context *ctx)
800 {
801 	const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
802 
803 	if (!ops)
804 		return -EOPNOTSUPP;
805 
806 	return vlan_macsec_offload(ops->mdo_dev_open, ctx);
807 }
808 
809 static int vlan_macsec_dev_stop(struct macsec_context *ctx)
810 {
811 	const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
812 
813 	if (!ops)
814 		return -EOPNOTSUPP;
815 
816 	return vlan_macsec_offload(ops->mdo_dev_stop, ctx);
817 }
818 
819 static int vlan_macsec_add_secy(struct macsec_context *ctx)
820 {
821 	const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
822 
823 	if (!ops)
824 		return -EOPNOTSUPP;
825 
826 	return vlan_macsec_offload(ops->mdo_add_secy, ctx);
827 }
828 
829 static int vlan_macsec_upd_secy(struct macsec_context *ctx)
830 {
831 	const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
832 
833 	if (!ops)
834 		return -EOPNOTSUPP;
835 
836 	return vlan_macsec_offload(ops->mdo_upd_secy, ctx);
837 }
838 
839 static int vlan_macsec_del_secy(struct macsec_context *ctx)
840 {
841 	const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
842 
843 	if (!ops)
844 		return -EOPNOTSUPP;
845 
846 	return vlan_macsec_offload(ops->mdo_del_secy, ctx);
847 }
848 
849 static int vlan_macsec_add_rxsc(struct macsec_context *ctx)
850 {
851 	const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
852 
853 	if (!ops)
854 		return -EOPNOTSUPP;
855 
856 	return vlan_macsec_offload(ops->mdo_add_rxsc, ctx);
857 }
858 
859 static int vlan_macsec_upd_rxsc(struct macsec_context *ctx)
860 {
861 	const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
862 
863 	if (!ops)
864 		return -EOPNOTSUPP;
865 
866 	return vlan_macsec_offload(ops->mdo_upd_rxsc, ctx);
867 }
868 
869 static int vlan_macsec_del_rxsc(struct macsec_context *ctx)
870 {
871 	const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
872 
873 	if (!ops)
874 		return -EOPNOTSUPP;
875 
876 	return vlan_macsec_offload(ops->mdo_del_rxsc, ctx);
877 }
878 
879 static int vlan_macsec_add_rxsa(struct macsec_context *ctx)
880 {
881 	const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
882 
883 	if (!ops)
884 		return -EOPNOTSUPP;
885 
886 	return vlan_macsec_offload(ops->mdo_add_rxsa, ctx);
887 }
888 
889 static int vlan_macsec_upd_rxsa(struct macsec_context *ctx)
890 {
891 	const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
892 
893 	if (!ops)
894 		return -EOPNOTSUPP;
895 
896 	return vlan_macsec_offload(ops->mdo_upd_rxsa, ctx);
897 }
898 
899 static int vlan_macsec_del_rxsa(struct macsec_context *ctx)
900 {
901 	const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
902 
903 	if (!ops)
904 		return -EOPNOTSUPP;
905 
906 	return vlan_macsec_offload(ops->mdo_del_rxsa, ctx);
907 }
908 
909 static int vlan_macsec_add_txsa(struct macsec_context *ctx)
910 {
911 	const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
912 
913 	if (!ops)
914 		return -EOPNOTSUPP;
915 
916 	return vlan_macsec_offload(ops->mdo_add_txsa, ctx);
917 }
918 
919 static int vlan_macsec_upd_txsa(struct macsec_context *ctx)
920 {
921 	const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
922 
923 	if (!ops)
924 		return -EOPNOTSUPP;
925 
926 	return vlan_macsec_offload(ops->mdo_upd_txsa, ctx);
927 }
928 
929 static int vlan_macsec_del_txsa(struct macsec_context *ctx)
930 {
931 	const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
932 
933 	if (!ops)
934 		return -EOPNOTSUPP;
935 
936 	return vlan_macsec_offload(ops->mdo_del_txsa, ctx);
937 }
938 
939 static int vlan_macsec_get_dev_stats(struct macsec_context *ctx)
940 {
941 	const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
942 
943 	if (!ops)
944 		return -EOPNOTSUPP;
945 
946 	return vlan_macsec_offload(ops->mdo_get_dev_stats, ctx);
947 }
948 
949 static int vlan_macsec_get_tx_sc_stats(struct macsec_context *ctx)
950 {
951 	const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
952 
953 	if (!ops)
954 		return -EOPNOTSUPP;
955 
956 	return vlan_macsec_offload(ops->mdo_get_tx_sc_stats, ctx);
957 }
958 
959 static int vlan_macsec_get_tx_sa_stats(struct macsec_context *ctx)
960 {
961 	const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
962 
963 	if (!ops)
964 		return -EOPNOTSUPP;
965 
966 	return vlan_macsec_offload(ops->mdo_get_tx_sa_stats, ctx);
967 }
968 
969 static int vlan_macsec_get_rx_sc_stats(struct macsec_context *ctx)
970 {
971 	const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
972 
973 	if (!ops)
974 		return -EOPNOTSUPP;
975 
976 	return vlan_macsec_offload(ops->mdo_get_rx_sc_stats, ctx);
977 }
978 
979 static int vlan_macsec_get_rx_sa_stats(struct macsec_context *ctx)
980 {
981 	const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
982 
983 	if (!ops)
984 		return -EOPNOTSUPP;
985 
986 	return vlan_macsec_offload(ops->mdo_get_rx_sa_stats, ctx);
987 }
988 
989 static const struct macsec_ops macsec_offload_ops = {
990 	/* Device wide */
991 	.mdo_dev_open = vlan_macsec_dev_open,
992 	.mdo_dev_stop = vlan_macsec_dev_stop,
993 	/* SecY */
994 	.mdo_add_secy = vlan_macsec_add_secy,
995 	.mdo_upd_secy = vlan_macsec_upd_secy,
996 	.mdo_del_secy = vlan_macsec_del_secy,
997 	/* Security channels */
998 	.mdo_add_rxsc = vlan_macsec_add_rxsc,
999 	.mdo_upd_rxsc = vlan_macsec_upd_rxsc,
1000 	.mdo_del_rxsc = vlan_macsec_del_rxsc,
1001 	/* Security associations */
1002 	.mdo_add_rxsa = vlan_macsec_add_rxsa,
1003 	.mdo_upd_rxsa = vlan_macsec_upd_rxsa,
1004 	.mdo_del_rxsa = vlan_macsec_del_rxsa,
1005 	.mdo_add_txsa = vlan_macsec_add_txsa,
1006 	.mdo_upd_txsa = vlan_macsec_upd_txsa,
1007 	.mdo_del_txsa = vlan_macsec_del_txsa,
1008 	/* Statistics */
1009 	.mdo_get_dev_stats = vlan_macsec_get_dev_stats,
1010 	.mdo_get_tx_sc_stats = vlan_macsec_get_tx_sc_stats,
1011 	.mdo_get_tx_sa_stats = vlan_macsec_get_tx_sa_stats,
1012 	.mdo_get_rx_sc_stats = vlan_macsec_get_rx_sc_stats,
1013 	.mdo_get_rx_sa_stats = vlan_macsec_get_rx_sa_stats,
1014 };
1015 
1016 #endif
1017 
1018 static const struct ethtool_ops vlan_ethtool_ops = {
1019 	.get_link_ksettings	= vlan_ethtool_get_link_ksettings,
1020 	.get_drvinfo	        = vlan_ethtool_get_drvinfo,
1021 	.get_link		= ethtool_op_get_link,
1022 	.get_ts_info		= vlan_ethtool_get_ts_info,
1023 };
1024 
1025 static void vlan_transfer_features(struct net_device *dev,
1026 				   struct net_device *vlandev)
1027 {
1028 	struct vlan_dev_priv *vlan = vlan_dev_priv(vlandev);
1029 
1030 	netif_inherit_tso_max(vlandev, dev);
1031 
1032 	if (vlan_hw_offload_capable(dev->features, vlan->vlan_proto))
1033 		vlandev->hard_header_len = dev->hard_header_len;
1034 	else
1035 		vlandev->hard_header_len = dev->hard_header_len + VLAN_HLEN;
1036 
1037 #if IS_ENABLED(CONFIG_FCOE)
1038 	vlandev->fcoe_ddp_xid = dev->fcoe_ddp_xid;
1039 #endif
1040 
1041 	vlandev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
1042 	vlandev->priv_flags |= (vlan->real_dev->priv_flags & IFF_XMIT_DST_RELEASE);
1043 	vlandev->hw_enc_features = vlan_tnl_features(vlan->real_dev);
1044 
1045 	netdev_update_features(vlandev);
1046 }
1047 
1048 static void vlan_dev_work(struct net_device *vlandev, unsigned long events)
1049 {
1050 	struct vlan_dev_priv *vlan = vlan_dev_priv(vlandev);
1051 	struct net_device *real_dev = vlan->real_dev;
1052 	bool loose = vlan->flags & VLAN_FLAG_LOOSE_BINDING;
1053 	unsigned int flgs;
1054 
1055 	if (events & VLAN_WORK_LINK_STATE) {
1056 		flgs = netif_get_flags(vlandev);
1057 		if (real_dev->flags & IFF_UP) {
1058 			if (!(flgs & IFF_UP)) {
1059 				if (!loose)
1060 					netif_change_flags(vlandev,
1061 							   flgs | IFF_UP, NULL);
1062 				vlan_stacked_transfer_operstate(real_dev,
1063 								vlandev, vlan);
1064 			}
1065 		} else if ((flgs & IFF_UP) && !loose) {
1066 			netif_change_flags(vlandev, flgs & ~IFF_UP, NULL);
1067 			vlan_stacked_transfer_operstate(real_dev, vlandev, vlan);
1068 		}
1069 	}
1070 
1071 	if ((events & VLAN_WORK_MTU) && vlandev->mtu > real_dev->mtu)
1072 		netif_set_mtu(vlandev, real_dev->mtu);
1073 
1074 	if (events & VLAN_WORK_FEATURES)
1075 		vlan_transfer_features(real_dev, vlandev);
1076 }
1077 
1078 static const struct net_device_ops vlan_netdev_ops = {
1079 	.ndo_change_mtu		= vlan_dev_change_mtu,
1080 	.ndo_init		= vlan_dev_init,
1081 	.ndo_uninit		= vlan_dev_uninit,
1082 	.ndo_open		= vlan_dev_open,
1083 	.ndo_stop		= vlan_dev_stop,
1084 	.ndo_start_xmit =  vlan_dev_hard_start_xmit,
1085 	.ndo_validate_addr	= eth_validate_addr,
1086 	.ndo_set_mac_address	= vlan_dev_set_mac_address,
1087 	.ndo_set_rx_mode	= vlan_dev_set_rx_mode,
1088 	.ndo_change_rx_flags	= vlan_dev_change_rx_flags,
1089 	.ndo_work		= vlan_dev_work,
1090 	.ndo_eth_ioctl		= vlan_dev_ioctl,
1091 	.ndo_neigh_setup	= vlan_dev_neigh_setup,
1092 	.ndo_get_stats64	= vlan_dev_get_stats64,
1093 #if IS_ENABLED(CONFIG_FCOE)
1094 	.ndo_fcoe_ddp_setup	= vlan_dev_fcoe_ddp_setup,
1095 	.ndo_fcoe_ddp_done	= vlan_dev_fcoe_ddp_done,
1096 	.ndo_fcoe_enable	= vlan_dev_fcoe_enable,
1097 	.ndo_fcoe_disable	= vlan_dev_fcoe_disable,
1098 	.ndo_fcoe_ddp_target	= vlan_dev_fcoe_ddp_target,
1099 #endif
1100 #ifdef NETDEV_FCOE_WWNN
1101 	.ndo_fcoe_get_wwn	= vlan_dev_fcoe_get_wwn,
1102 #endif
1103 #ifdef CONFIG_NET_POLL_CONTROLLER
1104 	.ndo_poll_controller	= vlan_dev_poll_controller,
1105 	.ndo_netpoll_setup	= vlan_dev_netpoll_setup,
1106 	.ndo_netpoll_cleanup	= vlan_dev_netpoll_cleanup,
1107 #endif
1108 	.ndo_fix_features	= vlan_dev_fix_features,
1109 	.ndo_get_iflink		= vlan_dev_get_iflink,
1110 	.ndo_fill_forward_path	= vlan_dev_fill_forward_path,
1111 	.ndo_hwtstamp_get	= vlan_hwtstamp_get,
1112 	.ndo_hwtstamp_set	= vlan_hwtstamp_set,
1113 };
1114 
1115 static void vlan_dev_free(struct net_device *dev)
1116 {
1117 	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
1118 
1119 	free_percpu(vlan->vlan_pcpu_stats);
1120 	vlan->vlan_pcpu_stats = NULL;
1121 
1122 	/* Get rid of the vlan's reference to real_dev */
1123 	netdev_put(vlan->real_dev, &vlan->dev_tracker);
1124 }
1125 
1126 void vlan_setup(struct net_device *dev)
1127 {
1128 	ether_setup(dev);
1129 
1130 	dev->priv_flags		|= IFF_802_1Q_VLAN | IFF_NO_QUEUE;
1131 	dev->priv_flags		|= IFF_UNICAST_FLT;
1132 	dev->priv_flags		&= ~IFF_TX_SKB_SHARING;
1133 	netif_keep_dst(dev);
1134 
1135 	dev->netdev_ops		= &vlan_netdev_ops;
1136 	dev->needs_free_netdev	= true;
1137 	dev->priv_destructor	= vlan_dev_free;
1138 	dev->ethtool_ops	= &vlan_ethtool_ops;
1139 
1140 #if IS_ENABLED(CONFIG_MACSEC)
1141 	dev->macsec_ops		= &macsec_offload_ops;
1142 #endif
1143 	dev->min_mtu		= 0;
1144 	dev->max_mtu		= ETH_MAX_MTU;
1145 
1146 	eth_zero_addr(dev->broadcast);
1147 }
1148