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