vlan_dev.c 23.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
L
Linus Torvalds 已提交
2 3 4 5 6
/* -*- linux-c -*-
 * INET		802.1Q VLAN
 *		Ethernet-type device handling.
 *
 * Authors:	Ben Greear <greearb@candelatech.com>
P
Patrick McHardy 已提交
7
 *              Please send support related email to: netdev@vger.kernel.org
L
Linus Torvalds 已提交
8
 *              VLAN Home Page: http://www.candelatech.com/~greear/vlan.html
9
 *
L
Linus Torvalds 已提交
10 11 12 13 14 15 16 17
 * Fixes:       Mar 22 2001: Martin Bokaemper <mbokaemper@unispherenetworks.com>
 *                - reset skb->pkt_type on incoming packets when MAC was changed
 *                - see that changed MAC is saddr for outgoing packets
 *              Oct 20, 2001:  Ard van Breeman:
 *                - Fix MC-list, finally.
 *                - Flush MC-list on VLAN destroy.
 */

J
Joe Perches 已提交
18 19
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

L
Linus Torvalds 已提交
20
#include <linux/module.h>
21
#include <linux/slab.h>
L
Linus Torvalds 已提交
22 23
#include <linux/skbuff.h>
#include <linux/netdevice.h>
24
#include <linux/net_tstamp.h>
L
Linus Torvalds 已提交
25
#include <linux/etherdevice.h>
P
Patrick McHardy 已提交
26
#include <linux/ethtool.h>
27
#include <linux/phy.h>
L
Linus Torvalds 已提交
28 29 30 31 32
#include <net/arp.h>

#include "vlan.h"
#include "vlanproc.h"
#include <linux/if_vlan.h>
33
#include <linux/netpoll.h>
L
Linus Torvalds 已提交
34 35

/*
36
 *	Create the VLAN header for an arbitrary protocol layer
L
Linus Torvalds 已提交
37 38 39 40 41 42 43
 *
 *	saddr=NULL	means use device source address
 *	daddr=NULL	means leave destination address (eg unresolved arp)
 *
 *  This is called when the SKB is moving down the stack towards the
 *  physical devices.
 */
44 45 46 47
static int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
				unsigned short type,
				const void *daddr, const void *saddr,
				unsigned int len)
L
Linus Torvalds 已提交
48
{
49
	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
L
Linus Torvalds 已提交
50
	struct vlan_hdr *vhdr;
51
	unsigned int vhdrlen = 0;
52
	u16 vlan_tci = 0;
53
	int rc;
L
Linus Torvalds 已提交
54

55
	if (!(vlan->flags & VLAN_FLAG_REORDER_HDR)) {
56
		vhdr = skb_push(skb, VLAN_HLEN);
L
Linus Torvalds 已提交
57

58
		vlan_tci = vlan->vlan_id;
59
		vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb->priority);
60
		vhdr->h_vlan_TCI = htons(vlan_tci);
L
Linus Torvalds 已提交
61 62

		/*
O
Octavian Purdila 已提交
63 64
		 *  Set the protocol type. For a packet of type ETH_P_802_3/2 we
		 *  put the length in here instead.
L
Linus Torvalds 已提交
65
		 */
O
Octavian Purdila 已提交
66
		if (type != ETH_P_802_3 && type != ETH_P_802_2)
L
Linus Torvalds 已提交
67
			vhdr->h_vlan_encapsulated_proto = htons(type);
P
Patrick McHardy 已提交
68
		else
L
Linus Torvalds 已提交
69
			vhdr->h_vlan_encapsulated_proto = htons(len);
70

71 72
		skb->protocol = vlan->vlan_proto;
		type = ntohs(vlan->vlan_proto);
73
		vhdrlen = VLAN_HLEN;
L
Linus Torvalds 已提交
74 75 76 77 78 79
	}

	/* Before delegating work to the lower layer, enter our MAC-address */
	if (saddr == NULL)
		saddr = dev->dev_addr;

80
	/* Now make the underlying real hard header */
81
	dev = vlan->real_dev;
82 83 84
	rc = dev_hard_header(skb, dev, type, daddr, saddr, len + vhdrlen);
	if (rc > 0)
		rc += vhdrlen;
L
Linus Torvalds 已提交
85 86 87
	return rc;
}

88 89 90
static inline netdev_tx_t vlan_netpoll_send_skb(struct vlan_dev_priv *vlan, struct sk_buff *skb)
{
#ifdef CONFIG_NET_POLL_CONTROLLER
91
	return netpoll_send_skb(vlan->netpoll, skb);
92 93 94
#else
	BUG();
	return NETDEV_TX_OK;
95
#endif
96 97
}

98 99
static netdev_tx_t vlan_dev_hard_start_xmit(struct sk_buff *skb,
					    struct net_device *dev)
L
Linus Torvalds 已提交
100
{
101
	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
L
Linus Torvalds 已提交
102
	struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
E
Eric Dumazet 已提交
103 104
	unsigned int len;
	int ret;
105

L
Linus Torvalds 已提交
106 107 108 109 110
	/* Handle non-VLAN frames if they are sent to us, for example by DHCP.
	 *
	 * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING
	 * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs...
	 */
111
	if (veth->h_vlan_proto != vlan->vlan_proto ||
112
	    vlan->flags & VLAN_FLAG_REORDER_HDR) {
113
		u16 vlan_tci;
114
		vlan_tci = vlan->vlan_id;
115
		vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb->priority);
116
		__vlan_hwaccel_put_tag(skb, vlan->vlan_proto, vlan_tci);
L
Linus Torvalds 已提交
117 118
	}

119
	skb->dev = vlan->real_dev;
E
Eric Dumazet 已提交
120
	len = skb->len;
121 122 123
	if (unlikely(netpoll_tx_running(dev)))
		return vlan_netpoll_send_skb(vlan, skb);

E
Eric Dumazet 已提交
124 125
	ret = dev_queue_xmit(skb);

126
	if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
E
Eric Dumazet 已提交
127 128
		struct vlan_pcpu_stats *stats;

129
		stats = this_cpu_ptr(vlan->vlan_pcpu_stats);
E
Eric Dumazet 已提交
130 131 132
		u64_stats_update_begin(&stats->syncp);
		stats->tx_packets++;
		stats->tx_bytes += len;
133
		u64_stats_update_end(&stats->syncp);
E
Eric Dumazet 已提交
134
	} else {
135
		this_cpu_inc(vlan->vlan_pcpu_stats->tx_dropped);
E
Eric Dumazet 已提交
136
	}
E
Eric Dumazet 已提交
137

138
	return ret;
L
Linus Torvalds 已提交
139 140
}

141
static int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
L
Linus Torvalds 已提交
142
{
143 144 145 146 147 148
	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
	unsigned int max_mtu = real_dev->mtu;

	if (netif_reduces_vlan_mtu(real_dev))
		max_mtu -= VLAN_HLEN;
	if (max_mtu < new_mtu)
L
Linus Torvalds 已提交
149 150 151 152 153 154 155
		return -ERANGE;

	dev->mtu = new_mtu;

	return 0;
}

156
void vlan_dev_set_ingress_priority(const struct net_device *dev,
157
				   u32 skb_prio, u16 vlan_prio)
L
Linus Torvalds 已提交
158
{
159
	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
160 161 162 163 164 165 166

	if (vlan->ingress_priority_map[vlan_prio & 0x7] && !skb_prio)
		vlan->nr_ingress_mappings--;
	else if (!vlan->ingress_priority_map[vlan_prio & 0x7] && skb_prio)
		vlan->nr_ingress_mappings++;

	vlan->ingress_priority_map[vlan_prio & 0x7] = skb_prio;
L
Linus Torvalds 已提交
167 168
}

169
int vlan_dev_set_egress_priority(const struct net_device *dev,
170
				 u32 skb_prio, u16 vlan_prio)
L
Linus Torvalds 已提交
171
{
172
	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
L
Linus Torvalds 已提交
173 174
	struct vlan_priority_tci_mapping *mp = NULL;
	struct vlan_priority_tci_mapping *np;
E
Eric Dumazet 已提交
175
	u32 vlan_qos = (vlan_prio << VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK;
176

177
	/* See if a priority mapping exists.. */
178
	mp = vlan->egress_priority_map[skb_prio & 0xF];
179 180
	while (mp) {
		if (mp->priority == skb_prio) {
181 182 183 184 185
			if (mp->vlan_qos && !vlan_qos)
				vlan->nr_egress_mappings--;
			else if (!mp->vlan_qos && vlan_qos)
				vlan->nr_egress_mappings++;
			mp->vlan_qos = vlan_qos;
186
			return 0;
L
Linus Torvalds 已提交
187
		}
188
		mp = mp->next;
L
Linus Torvalds 已提交
189
	}
190 191

	/* Create a new mapping then. */
192
	mp = vlan->egress_priority_map[skb_prio & 0xF];
193 194 195 196 197 198
	np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL);
	if (!np)
		return -ENOBUFS;

	np->next = mp;
	np->priority = skb_prio;
199
	np->vlan_qos = vlan_qos;
200 201
	/* Before inserting this element in hash table, make sure all its fields
	 * are committed to memory.
202
	 * coupled with smp_rmb() in vlan_dev_get_egress_qos_mask()
203 204
	 */
	smp_wmb();
205 206 207
	vlan->egress_priority_map[skb_prio & 0xF] = np;
	if (vlan_qos)
		vlan->nr_egress_mappings++;
208
	return 0;
L
Linus Torvalds 已提交
209 210
}

211 212 213
/* Flags are defined in the vlan_flags enum in
 * include/uapi/linux/if_vlan.h file.
 */
214
int vlan_dev_change_flags(const struct net_device *dev, u32 flags, u32 mask)
L
Linus Torvalds 已提交
215
{
216
	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
217 218
	u32 old_flags = vlan->flags;

219
	if (mask & ~(VLAN_FLAG_REORDER_HDR | VLAN_FLAG_GVRP |
220 221
		     VLAN_FLAG_LOOSE_BINDING | VLAN_FLAG_MVRP |
		     VLAN_FLAG_BRIDGE_BINDING))
222 223 224
		return -EINVAL;

	vlan->flags = (old_flags & ~mask) | (flags & mask);
P
Patrick McHardy 已提交
225 226 227 228 229 230 231

	if (netif_running(dev) && (vlan->flags ^ old_flags) & VLAN_FLAG_GVRP) {
		if (vlan->flags & VLAN_FLAG_GVRP)
			vlan_gvrp_request_join(dev);
		else
			vlan_gvrp_request_leave(dev);
	}
232 233 234 235 236 237 238

	if (netif_running(dev) && (vlan->flags ^ old_flags) & VLAN_FLAG_MVRP) {
		if (vlan->flags & VLAN_FLAG_MVRP)
			vlan_mvrp_request_join(dev);
		else
			vlan_mvrp_request_leave(dev);
	}
239
	return 0;
L
Linus Torvalds 已提交
240 241
}

K
Kees Cook 已提交
242
void vlan_dev_get_realdev_name(const struct net_device *dev, char *result, size_t size)
L
Linus Torvalds 已提交
243
{
K
Kees Cook 已提交
244
	strscpy_pad(result, vlan_dev_priv(dev)->real_dev->name, size);
L
Linus Torvalds 已提交
245 246
}

247 248 249 250 251 252
bool vlan_dev_inherit_address(struct net_device *dev,
			      struct net_device *real_dev)
{
	if (dev->addr_assign_type != NET_ADDR_STOLEN)
		return false;

253
	eth_hw_addr_set(dev, real_dev->dev_addr);
254 255 256 257
	call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
	return true;
}

258
static int vlan_dev_open(struct net_device *dev)
L
Linus Torvalds 已提交
259
{
260
	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
261 262 263
	struct net_device *real_dev = vlan->real_dev;
	int err;

264 265
	if (!(real_dev->flags & IFF_UP) &&
	    !(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
L
Linus Torvalds 已提交
266 267
		return -ENETDOWN;

268 269
	if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr) &&
	    !vlan_dev_inherit_address(dev, real_dev)) {
270
		err = dev_uc_add(real_dev, dev->dev_addr);
271
		if (err < 0)
272
			goto out;
273 274
	}

275 276 277 278 279 280 281 282 283 284 285
	if (dev->flags & IFF_ALLMULTI) {
		err = dev_set_allmulti(real_dev, 1);
		if (err < 0)
			goto del_unicast;
	}
	if (dev->flags & IFF_PROMISC) {
		err = dev_set_promiscuity(real_dev, 1);
		if (err < 0)
			goto clear_allmulti;
	}

J
Joe Perches 已提交
286
	ether_addr_copy(vlan->real_dev_addr, real_dev->dev_addr);
287

P
Patrick McHardy 已提交
288 289 290
	if (vlan->flags & VLAN_FLAG_GVRP)
		vlan_gvrp_request_join(dev);

291 292 293
	if (vlan->flags & VLAN_FLAG_MVRP)
		vlan_mvrp_request_join(dev);

294 295
	if (netif_carrier_ok(real_dev) &&
	    !(vlan->flags & VLAN_FLAG_BRIDGE_BINDING))
296
		netif_carrier_on(dev);
L
Linus Torvalds 已提交
297
	return 0;
298 299 300 301 302

clear_allmulti:
	if (dev->flags & IFF_ALLMULTI)
		dev_set_allmulti(real_dev, -1);
del_unicast:
303
	if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr))
304
		dev_uc_del(real_dev, dev->dev_addr);
305
out:
306
	netif_carrier_off(dev);
307
	return err;
L
Linus Torvalds 已提交
308 309
}

310
static int vlan_dev_stop(struct net_device *dev)
L
Linus Torvalds 已提交
311
{
312
	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
P
Patrick McHardy 已提交
313 314
	struct net_device *real_dev = vlan->real_dev;

315
	dev_mc_unsync(real_dev, dev);
316
	dev_uc_unsync(real_dev, dev);
317 318 319 320 321
	if (dev->flags & IFF_ALLMULTI)
		dev_set_allmulti(real_dev, -1);
	if (dev->flags & IFF_PROMISC)
		dev_set_promiscuity(real_dev, -1);

322
	if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr))
323
		dev_uc_del(real_dev, dev->dev_addr);
324

325 326
	if (!(vlan->flags & VLAN_FLAG_BRIDGE_BINDING))
		netif_carrier_off(dev);
L
Linus Torvalds 已提交
327 328 329
	return 0;
}

330
static int vlan_dev_set_mac_address(struct net_device *dev, void *p)
331
{
332
	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
333 334 335 336 337 338 339 340 341
	struct sockaddr *addr = p;
	int err;

	if (!is_valid_ether_addr(addr->sa_data))
		return -EADDRNOTAVAIL;

	if (!(dev->flags & IFF_UP))
		goto out;

342
	if (!ether_addr_equal(addr->sa_data, real_dev->dev_addr)) {
343
		err = dev_uc_add(real_dev, addr->sa_data);
344 345 346 347
		if (err < 0)
			return err;
	}

348
	if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr))
349
		dev_uc_del(real_dev, dev->dev_addr);
350 351

out:
352
	eth_hw_addr_set(dev, addr->sa_data);
353 354 355
	return 0;
}

356
static int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
L
Linus Torvalds 已提交
357
{
358
	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
359
	const struct net_device_ops *ops = real_dev->netdev_ops;
L
Linus Torvalds 已提交
360 361 362
	struct ifreq ifrr;
	int err = -EOPNOTSUPP;

K
Kees Cook 已提交
363
	strscpy_pad(ifrr.ifr_name, real_dev->name, IFNAMSIZ);
L
Linus Torvalds 已提交
364 365
	ifrr.ifr_ifru = ifr->ifr_ifru;

P
Patrick McHardy 已提交
366
	switch (cmd) {
367 368 369
	case SIOCSHWTSTAMP:
		if (!net_eq(dev_net(dev), &init_net))
			break;
370
		fallthrough;
L
Linus Torvalds 已提交
371 372 373
	case SIOCGMIIPHY:
	case SIOCGMIIREG:
	case SIOCSMIIREG:
374
	case SIOCGHWTSTAMP:
375 376
		if (netif_device_present(real_dev) && ops->ndo_eth_ioctl)
			err = ops->ndo_eth_ioctl(real_dev, &ifrr, cmd);
L
Linus Torvalds 已提交
377 378 379
		break;
	}

380
	if (!err)
L
Linus Torvalds 已提交
381 382 383 384 385
		ifr->ifr_ifru = ifrr.ifr_ifru;

	return err;
}

F
Frank Blaschka 已提交
386 387
static int vlan_dev_neigh_setup(struct net_device *dev, struct neigh_parms *pa)
{
388
	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
F
Frank Blaschka 已提交
389 390 391 392
	const struct net_device_ops *ops = real_dev->netdev_ops;
	int err = 0;

	if (netif_device_present(real_dev) && ops->ndo_neigh_setup)
393
		err = ops->ndo_neigh_setup(real_dev, pa);
F
Frank Blaschka 已提交
394 395 396 397

	return err;
}

A
Amerigo Wang 已提交
398
#if IS_ENABLED(CONFIG_FCOE)
399 400 401
static int vlan_dev_fcoe_ddp_setup(struct net_device *dev, u16 xid,
				   struct scatterlist *sgl, unsigned int sgc)
{
402
	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
403 404 405 406 407 408 409 410 411 412 413
	const struct net_device_ops *ops = real_dev->netdev_ops;
	int rc = 0;

	if (ops->ndo_fcoe_ddp_setup)
		rc = ops->ndo_fcoe_ddp_setup(real_dev, xid, sgl, sgc);

	return rc;
}

static int vlan_dev_fcoe_ddp_done(struct net_device *dev, u16 xid)
{
414
	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
415 416 417 418 419 420 421 422
	const struct net_device_ops *ops = real_dev->netdev_ops;
	int len = 0;

	if (ops->ndo_fcoe_ddp_done)
		len = ops->ndo_fcoe_ddp_done(real_dev, xid);

	return len;
}
423 424 425

static int vlan_dev_fcoe_enable(struct net_device *dev)
{
426
	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
427 428 429 430 431 432 433 434 435 436
	const struct net_device_ops *ops = real_dev->netdev_ops;
	int rc = -EINVAL;

	if (ops->ndo_fcoe_enable)
		rc = ops->ndo_fcoe_enable(real_dev);
	return rc;
}

static int vlan_dev_fcoe_disable(struct net_device *dev)
{
437
	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
438 439 440 441 442 443 444
	const struct net_device_ops *ops = real_dev->netdev_ops;
	int rc = -EINVAL;

	if (ops->ndo_fcoe_disable)
		rc = ops->ndo_fcoe_disable(real_dev);
	return rc;
}
445

446 447
static int vlan_dev_fcoe_ddp_target(struct net_device *dev, u16 xid,
				    struct scatterlist *sgl, unsigned int sgc)
448
{
449
	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
450
	const struct net_device_ops *ops = real_dev->netdev_ops;
451 452 453 454
	int rc = 0;

	if (ops->ndo_fcoe_ddp_target)
		rc = ops->ndo_fcoe_ddp_target(real_dev, xid, sgl, sgc);
455 456 457

	return rc;
}
458
#endif
459

460 461
#ifdef NETDEV_FCOE_WWNN
static int vlan_dev_fcoe_get_wwn(struct net_device *dev, u64 *wwn, int type)
462
{
463
	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
464
	const struct net_device_ops *ops = real_dev->netdev_ops;
465
	int rc = -EINVAL;
466

467 468
	if (ops->ndo_fcoe_get_wwn)
		rc = ops->ndo_fcoe_get_wwn(real_dev, wwn, type);
469 470
	return rc;
}
471 472
#endif

473
static void vlan_dev_change_rx_flags(struct net_device *dev, int change)
474
{
475
	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
476

477 478 479 480 481 482
	if (dev->flags & IFF_UP) {
		if (change & IFF_ALLMULTI)
			dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
		if (change & IFF_PROMISC)
			dev_set_promiscuity(real_dev, dev->flags & IFF_PROMISC ? 1 : -1);
	}
483 484
}

485
static void vlan_dev_set_rx_mode(struct net_device *vlan_dev)
L
Linus Torvalds 已提交
486
{
487 488
	dev_mc_sync(vlan_dev_priv(vlan_dev)->real_dev, vlan_dev);
	dev_uc_sync(vlan_dev_priv(vlan_dev)->real_dev, vlan_dev);
L
Linus Torvalds 已提交
489
}
490

491 492 493 494 495 496
/*
 * vlan network devices have devices nesting below it, and are a special
 * "super class" of normal network devices; split their locks off into a
 * separate class since they always nest.
 */
static struct lock_class_key vlan_netdev_xmit_lock_key;
497
static struct lock_class_key vlan_netdev_addr_lock_key;
498 499 500 501 502 503 504 505

static void vlan_dev_set_lockdep_one(struct net_device *dev,
				     struct netdev_queue *txq,
				     void *unused)
{
	lockdep_set_class(&txq->_xmit_lock, &vlan_netdev_xmit_lock_key);
}

506
static void vlan_dev_set_lockdep_class(struct net_device *dev)
507
{
508 509
	lockdep_set_class(&dev->addr_list_lock,
			  &vlan_netdev_addr_lock_key);
510 511 512
	netdev_for_each_tx_queue(dev, vlan_dev_set_lockdep_one, NULL);
}

513 514 515 516 517 518 519
static __be16 vlan_parse_protocol(const struct sk_buff *skb)
{
	struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);

	return __vlan_get_protocol(skb, veth->h_vlan_proto, NULL);
}

520 521 522
static const struct header_ops vlan_header_ops = {
	.create	 = vlan_dev_hard_header,
	.parse	 = eth_header_parse,
523
	.parse_protocol = vlan_parse_protocol,
524 525
};

526 527 528 529 530 531 532 533
static int vlan_passthru_hard_header(struct sk_buff *skb, struct net_device *dev,
				     unsigned short type,
				     const void *daddr, const void *saddr,
				     unsigned int len)
{
	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
	struct net_device *real_dev = vlan->real_dev;

534 535 536
	if (saddr == NULL)
		saddr = dev->dev_addr;

537 538 539 540 541 542
	return dev_hard_header(skb, real_dev, type, daddr, saddr, len);
}

static const struct header_ops vlan_passthru_header_ops = {
	.create	 = vlan_passthru_hard_header,
	.parse	 = eth_header_parse,
543
	.parse_protocol = vlan_parse_protocol,
544 545
};

546 547 548 549
static struct device_type vlan_type = {
	.name	= "vlan",
};

550
static const struct net_device_ops vlan_netdev_ops;
551

552 553
static int vlan_dev_init(struct net_device *dev)
{
554 555
	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
	struct net_device *real_dev = vlan->real_dev;
556

557 558
	netif_carrier_off(dev);

559
	/* IFF_BROADCAST|IFF_MULTICAST; ??? */
560 561
	dev->flags  = real_dev->flags & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
					  IFF_MASTER | IFF_SLAVE);
562 563 564 565
	dev->state  = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
					  (1<<__LINK_STATE_DORMANT))) |
		      (1<<__LINK_STATE_PRESENT);

566 567 568
	if (vlan->flags & VLAN_FLAG_BRIDGE_BINDING)
		dev->state |= (1 << __LINK_STATE_NOCARRIER);

569
	dev->hw_features = NETIF_F_HW_CSUM | NETIF_F_SG |
570
			   NETIF_F_FRAGLIST | NETIF_F_GSO_SOFTWARE |
571
			   NETIF_F_GSO_ENCAP_ALL |
572
			   NETIF_F_HIGHDMA | NETIF_F_SCTP_CRC |
573 574
			   NETIF_F_ALL_FCOE;

575
	dev->features |= dev->hw_features | NETIF_F_LLTX;
576
	netif_inherit_tso_max(dev, real_dev);
577 578 579
	if (dev->features & NETIF_F_VLAN_FEATURES)
		netdev_warn(real_dev, "VLAN features are set incorrectly.  Q-in-Q configurations may not work correctly.\n");

580
	dev->vlan_features = real_dev->vlan_features & ~NETIF_F_ALL_FCOE;
581
	dev->hw_enc_features = vlan_tnl_features(real_dev);
582
	dev->mpls_features = real_dev->mpls_features;
583

584 585 586
	/* ipv6 shared card related stuff */
	dev->dev_id = real_dev->dev_id;

587
	if (is_zero_ether_addr(dev->dev_addr)) {
588
		eth_hw_addr_set(dev, real_dev->dev_addr);
589 590
		dev->addr_assign_type = NET_ADDR_STOLEN;
	}
591 592 593
	if (is_zero_ether_addr(dev->broadcast))
		memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);

A
Amerigo Wang 已提交
594
#if IS_ENABLED(CONFIG_FCOE)
595 596 597
	dev->fcoe_ddp_xid = real_dev->fcoe_ddp_xid;
#endif

598
	dev->needed_headroom = real_dev->needed_headroom;
599
	if (vlan_hw_offload_capable(real_dev->features, vlan->vlan_proto)) {
600
		dev->header_ops      = &vlan_passthru_header_ops;
601 602 603 604 605 606
		dev->hard_header_len = real_dev->hard_header_len;
	} else {
		dev->header_ops      = &vlan_header_ops;
		dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN;
	}

607
	dev->netdev_ops = &vlan_netdev_ops;
J
John Fastabend 已提交
608

609 610
	SET_NETDEV_DEVTYPE(dev, &vlan_type);

611
	vlan_dev_set_lockdep_class(dev);
612

613 614
	vlan->vlan_pcpu_stats = netdev_alloc_pcpu_stats(struct vlan_pcpu_stats);
	if (!vlan->vlan_pcpu_stats)
E
Eric Dumazet 已提交
615 616
		return -ENOMEM;

617
	/* Get vlan's reference to real_dev */
618
	dev_hold_track(real_dev, &vlan->dev_tracker, GFP_KERNEL);
619

620 621 622
	return 0;
}

623
/* Note: this function might be called multiple times for the same device. */
624
void vlan_dev_free_egress_priority(const struct net_device *dev)
625 626
{
	struct vlan_priority_tci_mapping *pm;
627
	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
628 629 630 631 632 633 634 635 636 637
	int i;

	for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) {
		while ((pm = vlan->egress_priority_map[i]) != NULL) {
			vlan->egress_priority_map[i] = pm->next;
			kfree(pm);
		}
	}
}

638 639 640 641 642
static void vlan_dev_uninit(struct net_device *dev)
{
	vlan_dev_free_egress_priority(dev);
}

643 644
static netdev_features_t vlan_dev_fix_features(struct net_device *dev,
	netdev_features_t features)
645
{
646
	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
647
	netdev_features_t old_features = features;
648
	netdev_features_t lower_features;
649

650 651 652
	lower_features = netdev_intersect_features((real_dev->vlan_features |
						    NETIF_F_RXCSUM),
						   real_dev->features);
653

654 655 656 657 658 659
	/* Add HW_CSUM setting to preserve user ability to control
	 * checksum offload on the vlan device.
	 */
	if (lower_features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))
		lower_features |= NETIF_F_HW_CSUM;
	features = netdev_intersect_features(features, lower_features);
660
	features |= old_features & (NETIF_F_SOFT_FEATURES | NETIF_F_GSO_SOFTWARE);
661
	features |= NETIF_F_LLTX;
662 663 664 665

	return features;
}

666 667
static int vlan_ethtool_get_link_ksettings(struct net_device *dev,
					   struct ethtool_link_ksettings *cmd)
668
{
669
	const struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
670

671
	return __ethtool_get_link_ksettings(vlan->real_dev, cmd);
672 673 674 675 676
}

static void vlan_ethtool_get_drvinfo(struct net_device *dev,
				     struct ethtool_drvinfo *info)
{
677 678 679
	strlcpy(info->driver, vlan_fullname, sizeof(info->driver));
	strlcpy(info->version, vlan_version, sizeof(info->version));
	strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
680 681
}

682 683 684 685 686
static int vlan_ethtool_get_ts_info(struct net_device *dev,
				    struct ethtool_ts_info *info)
{
	const struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
	const struct ethtool_ops *ops = vlan->real_dev->ethtool_ops;
687
	struct phy_device *phydev = vlan->real_dev->phydev;
688

689 690
	if (phy_has_tsinfo(phydev)) {
		return phy_ts_info(phydev, info);
691
	} else if (ops->get_ts_info) {
692 693 694 695 696 697 698 699 700 701
		return ops->get_ts_info(vlan->real_dev, info);
	} else {
		info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE |
			SOF_TIMESTAMPING_SOFTWARE;
		info->phc_index = -1;
	}

	return 0;
}

702 703
static void vlan_dev_get_stats64(struct net_device *dev,
				 struct rtnl_link_stats64 *stats)
E
Eric Dumazet 已提交
704
{
705 706 707
	struct vlan_pcpu_stats *p;
	u32 rx_errors = 0, tx_dropped = 0;
	int i;
E
Eric Dumazet 已提交
708

709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
	for_each_possible_cpu(i) {
		u64 rxpackets, rxbytes, rxmulticast, txpackets, txbytes;
		unsigned int start;

		p = per_cpu_ptr(vlan_dev_priv(dev)->vlan_pcpu_stats, i);
		do {
			start = u64_stats_fetch_begin_irq(&p->syncp);
			rxpackets	= p->rx_packets;
			rxbytes		= p->rx_bytes;
			rxmulticast	= p->rx_multicast;
			txpackets	= p->tx_packets;
			txbytes		= p->tx_bytes;
		} while (u64_stats_fetch_retry_irq(&p->syncp, start));

		stats->rx_packets	+= rxpackets;
		stats->rx_bytes		+= rxbytes;
		stats->multicast	+= rxmulticast;
		stats->tx_packets	+= txpackets;
		stats->tx_bytes		+= txbytes;
		/* rx_errors & tx_dropped are u32 */
		rx_errors	+= p->rx_errors;
		tx_dropped	+= p->tx_dropped;
E
Eric Dumazet 已提交
731
	}
732 733
	stats->rx_errors  = rx_errors;
	stats->tx_dropped = tx_dropped;
E
Eric Dumazet 已提交
734 735
}

736
#ifdef CONFIG_NET_POLL_CONTROLLER
E
Eric Dumazet 已提交
737
static void vlan_dev_poll_controller(struct net_device *dev)
738 739 740 741
{
	return;
}

742
static int vlan_dev_netpoll_setup(struct net_device *dev, struct netpoll_info *npinfo)
743
{
744 745
	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
	struct net_device *real_dev = vlan->real_dev;
746 747 748
	struct netpoll *netpoll;
	int err = 0;

749
	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
750 751 752 753
	err = -ENOMEM;
	if (!netpoll)
		goto out;

754
	err = __netpoll_setup(netpoll, real_dev);
755 756 757 758 759
	if (err) {
		kfree(netpoll);
		goto out;
	}

760
	vlan->netpoll = netpoll;
761 762 763 764 765

out:
	return err;
}

E
Eric Dumazet 已提交
766
static void vlan_dev_netpoll_cleanup(struct net_device *dev)
767
{
768 769
	struct vlan_dev_priv *vlan= vlan_dev_priv(dev);
	struct netpoll *netpoll = vlan->netpoll;
770 771 772 773

	if (!netpoll)
		return;

774
	vlan->netpoll = NULL;
775
	__netpoll_free(netpoll);
776 777 778
}
#endif /* CONFIG_NET_POLL_CONTROLLER */

N
Nicolas Dichtel 已提交
779 780 781 782 783 784 785
static int vlan_dev_get_iflink(const struct net_device *dev)
{
	struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;

	return real_dev->ifindex;
}

786 787 788 789 790 791 792 793 794 795
static int vlan_dev_fill_forward_path(struct net_device_path_ctx *ctx,
				      struct net_device_path *path)
{
	struct vlan_dev_priv *vlan = vlan_dev_priv(ctx->dev);

	path->type = DEV_PATH_VLAN;
	path->encap.id = vlan->vlan_id;
	path->encap.proto = vlan->vlan_proto;
	path->dev = ctx->dev;
	ctx->dev = vlan->real_dev;
796 797 798 799 800 801
	if (ctx->num_vlans >= ARRAY_SIZE(ctx->vlan))
		return -ENOSPC;

	ctx->vlan[ctx->num_vlans].id = vlan->vlan_id;
	ctx->vlan[ctx->num_vlans].proto = vlan->vlan_proto;
	ctx->num_vlans++;
802 803 804 805

	return 0;
}

P
Patrick McHardy 已提交
806
static const struct ethtool_ops vlan_ethtool_ops = {
807
	.get_link_ksettings	= vlan_ethtool_get_link_ksettings,
808
	.get_drvinfo	        = vlan_ethtool_get_drvinfo,
P
Patrick McHardy 已提交
809
	.get_link		= ethtool_op_get_link,
810
	.get_ts_info		= vlan_ethtool_get_ts_info,
P
Patrick McHardy 已提交
811 812
};

813 814 815 816 817 818
static const struct net_device_ops vlan_netdev_ops = {
	.ndo_change_mtu		= vlan_dev_change_mtu,
	.ndo_init		= vlan_dev_init,
	.ndo_uninit		= vlan_dev_uninit,
	.ndo_open		= vlan_dev_open,
	.ndo_stop		= vlan_dev_stop,
V
Vasu Dev 已提交
819 820 821 822 823
	.ndo_start_xmit =  vlan_dev_hard_start_xmit,
	.ndo_validate_addr	= eth_validate_addr,
	.ndo_set_mac_address	= vlan_dev_set_mac_address,
	.ndo_set_rx_mode	= vlan_dev_set_rx_mode,
	.ndo_change_rx_flags	= vlan_dev_change_rx_flags,
824
	.ndo_eth_ioctl		= vlan_dev_ioctl,
V
Vasu Dev 已提交
825
	.ndo_neigh_setup	= vlan_dev_neigh_setup,
E
Eric Dumazet 已提交
826
	.ndo_get_stats64	= vlan_dev_get_stats64,
A
Amerigo Wang 已提交
827
#if IS_ENABLED(CONFIG_FCOE)
V
Vasu Dev 已提交
828 829 830 831
	.ndo_fcoe_ddp_setup	= vlan_dev_fcoe_ddp_setup,
	.ndo_fcoe_ddp_done	= vlan_dev_fcoe_ddp_done,
	.ndo_fcoe_enable	= vlan_dev_fcoe_enable,
	.ndo_fcoe_disable	= vlan_dev_fcoe_disable,
832
	.ndo_fcoe_ddp_target	= vlan_dev_fcoe_ddp_target,
833
#endif
834 835 836
#ifdef NETDEV_FCOE_WWNN
	.ndo_fcoe_get_wwn	= vlan_dev_fcoe_get_wwn,
#endif
837 838 839 840
#ifdef CONFIG_NET_POLL_CONTROLLER
	.ndo_poll_controller	= vlan_dev_poll_controller,
	.ndo_netpoll_setup	= vlan_dev_netpoll_setup,
	.ndo_netpoll_cleanup	= vlan_dev_netpoll_cleanup,
V
Vasu Dev 已提交
841
#endif
842
	.ndo_fix_features	= vlan_dev_fix_features,
N
Nicolas Dichtel 已提交
843
	.ndo_get_iflink		= vlan_dev_get_iflink,
844
	.ndo_fill_forward_path	= vlan_dev_fill_forward_path,
V
Vasu Dev 已提交
845 846
};

847 848 849 850 851 852
static void vlan_dev_free(struct net_device *dev)
{
	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);

	free_percpu(vlan->vlan_pcpu_stats);
	vlan->vlan_pcpu_stats = NULL;
853 854 855

	/* Get rid of the vlan's reference to real_dev */
	dev_put_track(vlan->real_dev, &vlan->dev_tracker);
856 857
}

858 859 860 861
void vlan_setup(struct net_device *dev)
{
	ether_setup(dev);

862
	dev->priv_flags		|= IFF_802_1Q_VLAN | IFF_NO_QUEUE;
863
	dev->priv_flags		|= IFF_UNICAST_FLT;
864 865
	dev->priv_flags		&= ~IFF_TX_SKB_SHARING;
	netif_keep_dst(dev);
866

867
	dev->netdev_ops		= &vlan_netdev_ops;
868 869
	dev->needs_free_netdev	= true;
	dev->priv_destructor	= vlan_dev_free;
P
Patrick McHardy 已提交
870
	dev->ethtool_ops	= &vlan_ethtool_ops;
871

872 873 874
	dev->min_mtu		= 0;
	dev->max_mtu		= ETH_MAX_MTU;

875
	eth_zero_addr(dev->broadcast);
876
}