Merge remote-tracking branch 'remotes/powerpc/topic/ppc-kvm' into kvm-ppc-next
[linux-block.git] / net / 8021q / vlan_dev.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/* -*- linux-c -*-
3 * INET 802.1Q VLAN
4 * Ethernet-type device handling.
5 *
6 * Authors: Ben Greear <greearb@candelatech.com>
ad712087 7 * Please send support related email to: netdev@vger.kernel.org
1da177e4 8 * VLAN Home Page: http://www.candelatech.com/~greear/vlan.html
122952fc 9 *
1da177e4
LT
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.
1da177e4
LT
16 */
17
afab2d29
JP
18#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
1da177e4 20#include <linux/module.h>
5a0e3ad6 21#include <linux/slab.h>
1da177e4
LT
22#include <linux/skbuff.h>
23#include <linux/netdevice.h>
37dd9255 24#include <linux/net_tstamp.h>
1da177e4 25#include <linux/etherdevice.h>
75b8846a 26#include <linux/ethtool.h>
ec1d8ccb 27#include <linux/phy.h>
1da177e4
LT
28#include <net/arp.h>
29
30#include "vlan.h"
31#include "vlanproc.h"
32#include <linux/if_vlan.h>
6d4cdf47 33#include <linux/netpoll.h>
1da177e4 34
1da177e4 35/*
122952fc 36 * Create the VLAN header for an arbitrary protocol layer
1da177e4
LT
37 *
38 * saddr=NULL means use device source address
39 * daddr=NULL means leave destination address (eg unresolved arp)
40 *
41 * This is called when the SKB is moving down the stack towards the
42 * physical devices.
43 */
ef3eb3e5
PM
44static int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
45 unsigned short type,
46 const void *daddr, const void *saddr,
47 unsigned int len)
1da177e4 48{
1fd9b1fc 49 struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
1da177e4 50 struct vlan_hdr *vhdr;
1349fe9a 51 unsigned int vhdrlen = 0;
9bb8582e 52 u16 vlan_tci = 0;
1349fe9a 53 int rc;
1da177e4 54
0c0667a8 55 if (!(vlan->flags & VLAN_FLAG_REORDER_HDR)) {
d58ff351 56 vhdr = skb_push(skb, VLAN_HLEN);
1da177e4 57
0c0667a8 58 vlan_tci = vlan->vlan_id;
e267cb96 59 vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb->priority);
9bb8582e 60 vhdr->h_vlan_TCI = htons(vlan_tci);
1da177e4
LT
61
62 /*
bf9ae538
OP
63 * Set the protocol type. For a packet of type ETH_P_802_3/2 we
64 * put the length in here instead.
1da177e4 65 */
bf9ae538 66 if (type != ETH_P_802_3 && type != ETH_P_802_2)
1da177e4 67 vhdr->h_vlan_encapsulated_proto = htons(type);
2029cc2c 68 else
1da177e4 69 vhdr->h_vlan_encapsulated_proto = htons(len);
279e172a 70
1fd9b1fc
PM
71 skb->protocol = vlan->vlan_proto;
72 type = ntohs(vlan->vlan_proto);
1349fe9a 73 vhdrlen = VLAN_HLEN;
1da177e4
LT
74 }
75
76 /* Before delegating work to the lower layer, enter our MAC-address */
77 if (saddr == NULL)
78 saddr = dev->dev_addr;
79
1349fe9a 80 /* Now make the underlying real hard header */
0c0667a8 81 dev = vlan->real_dev;
1349fe9a
PM
82 rc = dev_hard_header(skb, dev, type, daddr, saddr, len + vhdrlen);
83 if (rc > 0)
84 rc += vhdrlen;
1da177e4
LT
85 return rc;
86}
87
6eacf8ad
AW
88static inline netdev_tx_t vlan_netpoll_send_skb(struct vlan_dev_priv *vlan, struct sk_buff *skb)
89{
90#ifdef CONFIG_NET_POLL_CONTROLLER
91 if (vlan->netpoll)
92 netpoll_send_skb(vlan->netpoll, skb);
93#else
94 BUG();
95#endif
96 return NETDEV_TX_OK;
97}
98
6fef4c0c
SH
99static netdev_tx_t vlan_dev_hard_start_xmit(struct sk_buff *skb,
100 struct net_device *dev)
1da177e4 101{
6eacf8ad 102 struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
1da177e4 103 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
1a123a31
ED
104 unsigned int len;
105 int ret;
55b01e86 106
1da177e4
LT
107 /* Handle non-VLAN frames if they are sent to us, for example by DHCP.
108 *
109 * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING
110 * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs...
111 */
1fd9b1fc 112 if (veth->h_vlan_proto != vlan->vlan_proto ||
6eacf8ad 113 vlan->flags & VLAN_FLAG_REORDER_HDR) {
9bb8582e 114 u16 vlan_tci;
6eacf8ad 115 vlan_tci = vlan->vlan_id;
e267cb96 116 vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb->priority);
b960a0ac 117 __vlan_hwaccel_put_tag(skb, vlan->vlan_proto, vlan_tci);
1da177e4
LT
118 }
119
6eacf8ad 120 skb->dev = vlan->real_dev;
1a123a31 121 len = skb->len;
6eacf8ad
AW
122 if (unlikely(netpoll_tx_running(dev)))
123 return vlan_netpoll_send_skb(vlan, skb);
124
1a123a31
ED
125 ret = dev_queue_xmit(skb);
126
2d6c9ffc 127 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
4af429d2
ED
128 struct vlan_pcpu_stats *stats;
129
6eacf8ad 130 stats = this_cpu_ptr(vlan->vlan_pcpu_stats);
4af429d2
ED
131 u64_stats_update_begin(&stats->syncp);
132 stats->tx_packets++;
133 stats->tx_bytes += len;
307f73df 134 u64_stats_update_end(&stats->syncp);
4af429d2 135 } else {
6eacf8ad 136 this_cpu_inc(vlan->vlan_pcpu_stats->tx_dropped);
4af429d2 137 }
1a123a31 138
cbbef5e1 139 return ret;
1da177e4
LT
140}
141
ef3eb3e5 142static int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
1da177e4 143{
18d3df3e
PA
144 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
145 unsigned int max_mtu = real_dev->mtu;
146
147 if (netif_reduces_vlan_mtu(real_dev))
148 max_mtu -= VLAN_HLEN;
149 if (max_mtu < new_mtu)
1da177e4
LT
150 return -ERANGE;
151
152 dev->mtu = new_mtu;
153
154 return 0;
155}
156
c17d8874 157void vlan_dev_set_ingress_priority(const struct net_device *dev,
9bb8582e 158 u32 skb_prio, u16 vlan_prio)
1da177e4 159{
7da82c06 160 struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
b020cb48
PM
161
162 if (vlan->ingress_priority_map[vlan_prio & 0x7] && !skb_prio)
163 vlan->nr_ingress_mappings--;
164 else if (!vlan->ingress_priority_map[vlan_prio & 0x7] && skb_prio)
165 vlan->nr_ingress_mappings++;
166
167 vlan->ingress_priority_map[vlan_prio & 0x7] = skb_prio;
1da177e4
LT
168}
169
c17d8874 170int vlan_dev_set_egress_priority(const struct net_device *dev,
9bb8582e 171 u32 skb_prio, u16 vlan_prio)
1da177e4 172{
7da82c06 173 struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
1da177e4
LT
174 struct vlan_priority_tci_mapping *mp = NULL;
175 struct vlan_priority_tci_mapping *np;
05423b24 176 u32 vlan_qos = (vlan_prio << VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK;
122952fc 177
c17d8874 178 /* See if a priority mapping exists.. */
b020cb48 179 mp = vlan->egress_priority_map[skb_prio & 0xF];
c17d8874
PM
180 while (mp) {
181 if (mp->priority == skb_prio) {
b020cb48
PM
182 if (mp->vlan_qos && !vlan_qos)
183 vlan->nr_egress_mappings--;
184 else if (!mp->vlan_qos && vlan_qos)
185 vlan->nr_egress_mappings++;
186 mp->vlan_qos = vlan_qos;
c17d8874 187 return 0;
1da177e4 188 }
c17d8874 189 mp = mp->next;
1da177e4 190 }
c17d8874
PM
191
192 /* Create a new mapping then. */
b020cb48 193 mp = vlan->egress_priority_map[skb_prio & 0xF];
c17d8874
PM
194 np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL);
195 if (!np)
196 return -ENOBUFS;
197
198 np->next = mp;
199 np->priority = skb_prio;
b020cb48 200 np->vlan_qos = vlan_qos;
3e3aac49
ED
201 /* Before inserting this element in hash table, make sure all its fields
202 * are committed to memory.
e267cb96 203 * coupled with smp_rmb() in vlan_dev_get_egress_qos_mask()
3e3aac49
ED
204 */
205 smp_wmb();
b020cb48
PM
206 vlan->egress_priority_map[skb_prio & 0xF] = np;
207 if (vlan_qos)
208 vlan->nr_egress_mappings++;
c17d8874 209 return 0;
1da177e4
LT
210}
211
84a2fba2
SL
212/* Flags are defined in the vlan_flags enum in
213 * include/uapi/linux/if_vlan.h file.
214 */
b3ce0325 215int vlan_dev_change_flags(const struct net_device *dev, u32 flags, u32 mask)
1da177e4 216{
7da82c06 217 struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
b3ce0325
PM
218 u32 old_flags = vlan->flags;
219
5e756593 220 if (mask & ~(VLAN_FLAG_REORDER_HDR | VLAN_FLAG_GVRP |
8c8b3458
MM
221 VLAN_FLAG_LOOSE_BINDING | VLAN_FLAG_MVRP |
222 VLAN_FLAG_BRIDGE_BINDING))
b3ce0325
PM
223 return -EINVAL;
224
225 vlan->flags = (old_flags & ~mask) | (flags & mask);
70c03b49
PM
226
227 if (netif_running(dev) && (vlan->flags ^ old_flags) & VLAN_FLAG_GVRP) {
228 if (vlan->flags & VLAN_FLAG_GVRP)
229 vlan_gvrp_request_join(dev);
230 else
231 vlan_gvrp_request_leave(dev);
232 }
86fbe9bb
DW
233
234 if (netif_running(dev) && (vlan->flags ^ old_flags) & VLAN_FLAG_MVRP) {
235 if (vlan->flags & VLAN_FLAG_MVRP)
236 vlan_mvrp_request_join(dev);
237 else
238 vlan_mvrp_request_leave(dev);
239 }
b3ce0325 240 return 0;
1da177e4
LT
241}
242
c17d8874 243void vlan_dev_get_realdev_name(const struct net_device *dev, char *result)
1da177e4 244{
7da82c06 245 strncpy(result, vlan_dev_priv(dev)->real_dev->name, 23);
1da177e4
LT
246}
247
308453aa
MM
248bool vlan_dev_inherit_address(struct net_device *dev,
249 struct net_device *real_dev)
250{
251 if (dev->addr_assign_type != NET_ADDR_STOLEN)
252 return false;
253
254 ether_addr_copy(dev->dev_addr, real_dev->dev_addr);
255 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
256 return true;
257}
258
ef3eb3e5 259static int vlan_dev_open(struct net_device *dev)
1da177e4 260{
7da82c06 261 struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
8c979c26
PM
262 struct net_device *real_dev = vlan->real_dev;
263 int err;
264
5e756593
PM
265 if (!(real_dev->flags & IFF_UP) &&
266 !(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
1da177e4
LT
267 return -ENETDOWN;
268
308453aa
MM
269 if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr) &&
270 !vlan_dev_inherit_address(dev, real_dev)) {
a748ee24 271 err = dev_uc_add(real_dev, dev->dev_addr);
8c979c26 272 if (err < 0)
89146504 273 goto out;
8c979c26 274 }
8c979c26 275
89146504
WC
276 if (dev->flags & IFF_ALLMULTI) {
277 err = dev_set_allmulti(real_dev, 1);
278 if (err < 0)
279 goto del_unicast;
280 }
281 if (dev->flags & IFF_PROMISC) {
282 err = dev_set_promiscuity(real_dev, 1);
283 if (err < 0)
284 goto clear_allmulti;
285 }
286
07fc67be 287 ether_addr_copy(vlan->real_dev_addr, real_dev->dev_addr);
6c78dcbd 288
70c03b49
PM
289 if (vlan->flags & VLAN_FLAG_GVRP)
290 vlan_gvrp_request_join(dev);
291
86fbe9bb
DW
292 if (vlan->flags & VLAN_FLAG_MVRP)
293 vlan_mvrp_request_join(dev);
294
76052d8c
MM
295 if (netif_carrier_ok(real_dev) &&
296 !(vlan->flags & VLAN_FLAG_BRIDGE_BINDING))
0ac820ee 297 netif_carrier_on(dev);
1da177e4 298 return 0;
89146504
WC
299
300clear_allmulti:
301 if (dev->flags & IFF_ALLMULTI)
302 dev_set_allmulti(real_dev, -1);
303del_unicast:
53a2b3a1 304 if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr))
a748ee24 305 dev_uc_del(real_dev, dev->dev_addr);
89146504 306out:
adc667e8 307 netif_carrier_off(dev);
89146504 308 return err;
1da177e4
LT
309}
310
ef3eb3e5 311static int vlan_dev_stop(struct net_device *dev)
1da177e4 312{
7da82c06 313 struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
70c03b49
PM
314 struct net_device *real_dev = vlan->real_dev;
315
56addd6e 316 dev_mc_unsync(real_dev, dev);
a748ee24 317 dev_uc_unsync(real_dev, dev);
6c78dcbd
PM
318 if (dev->flags & IFF_ALLMULTI)
319 dev_set_allmulti(real_dev, -1);
320 if (dev->flags & IFF_PROMISC)
321 dev_set_promiscuity(real_dev, -1);
322
53a2b3a1 323 if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr))
a748ee24 324 dev_uc_del(real_dev, dev->dev_addr);
8c979c26 325
76052d8c
MM
326 if (!(vlan->flags & VLAN_FLAG_BRIDGE_BINDING))
327 netif_carrier_off(dev);
1da177e4
LT
328 return 0;
329}
330
ef3eb3e5 331static int vlan_dev_set_mac_address(struct net_device *dev, void *p)
39aaac11 332{
7da82c06 333 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
39aaac11
PM
334 struct sockaddr *addr = p;
335 int err;
336
337 if (!is_valid_ether_addr(addr->sa_data))
338 return -EADDRNOTAVAIL;
339
340 if (!(dev->flags & IFF_UP))
341 goto out;
342
53a2b3a1 343 if (!ether_addr_equal(addr->sa_data, real_dev->dev_addr)) {
a748ee24 344 err = dev_uc_add(real_dev, addr->sa_data);
39aaac11
PM
345 if (err < 0)
346 return err;
347 }
348
53a2b3a1 349 if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr))
a748ee24 350 dev_uc_del(real_dev, dev->dev_addr);
39aaac11
PM
351
352out:
07fc67be 353 ether_addr_copy(dev->dev_addr, addr->sa_data);
39aaac11
PM
354 return 0;
355}
356
ef3eb3e5 357static int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1da177e4 358{
7da82c06 359 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
656299f7 360 const struct net_device_ops *ops = real_dev->netdev_ops;
1da177e4
LT
361 struct ifreq ifrr;
362 int err = -EOPNOTSUPP;
363
364 strncpy(ifrr.ifr_name, real_dev->name, IFNAMSIZ);
365 ifrr.ifr_ifru = ifr->ifr_ifru;
366
2029cc2c 367 switch (cmd) {
873017af
HL
368 case SIOCSHWTSTAMP:
369 if (!net_eq(dev_net(dev), &init_net))
370 break;
fa2c52be 371 /* fall through */
1da177e4
LT
372 case SIOCGMIIPHY:
373 case SIOCGMIIREG:
374 case SIOCSMIIREG:
a6111d3c 375 case SIOCGHWTSTAMP:
656299f7
SH
376 if (netif_device_present(real_dev) && ops->ndo_do_ioctl)
377 err = ops->ndo_do_ioctl(real_dev, &ifrr, cmd);
1da177e4 378 break;
1da177e4
LT
379 }
380
122952fc 381 if (!err)
1da177e4
LT
382 ifr->ifr_ifru = ifrr.ifr_ifru;
383
384 return err;
385}
386
cc883d16
FB
387static int vlan_dev_neigh_setup(struct net_device *dev, struct neigh_parms *pa)
388{
7da82c06 389 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
cc883d16
FB
390 const struct net_device_ops *ops = real_dev->netdev_ops;
391 int err = 0;
392
393 if (netif_device_present(real_dev) && ops->ndo_neigh_setup)
9d40bbda 394 err = ops->ndo_neigh_setup(real_dev, pa);
cc883d16
FB
395
396 return err;
397}
398
f4d5392e 399#if IS_ENABLED(CONFIG_FCOE)
b85daa53
VD
400static int vlan_dev_fcoe_ddp_setup(struct net_device *dev, u16 xid,
401 struct scatterlist *sgl, unsigned int sgc)
402{
7da82c06 403 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
b85daa53
VD
404 const struct net_device_ops *ops = real_dev->netdev_ops;
405 int rc = 0;
406
407 if (ops->ndo_fcoe_ddp_setup)
408 rc = ops->ndo_fcoe_ddp_setup(real_dev, xid, sgl, sgc);
409
410 return rc;
411}
412
413static int vlan_dev_fcoe_ddp_done(struct net_device *dev, u16 xid)
414{
7da82c06 415 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
b85daa53
VD
416 const struct net_device_ops *ops = real_dev->netdev_ops;
417 int len = 0;
418
419 if (ops->ndo_fcoe_ddp_done)
420 len = ops->ndo_fcoe_ddp_done(real_dev, xid);
421
422 return len;
423}
0af46d99
YZ
424
425static int vlan_dev_fcoe_enable(struct net_device *dev)
426{
7da82c06 427 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
0af46d99
YZ
428 const struct net_device_ops *ops = real_dev->netdev_ops;
429 int rc = -EINVAL;
430
431 if (ops->ndo_fcoe_enable)
432 rc = ops->ndo_fcoe_enable(real_dev);
433 return rc;
434}
435
436static int vlan_dev_fcoe_disable(struct net_device *dev)
437{
7da82c06 438 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
0af46d99
YZ
439 const struct net_device_ops *ops = real_dev->netdev_ops;
440 int rc = -EINVAL;
441
442 if (ops->ndo_fcoe_disable)
443 rc = ops->ndo_fcoe_disable(real_dev);
444 return rc;
445}
d6534799 446
0a89eb92
CL
447static int vlan_dev_fcoe_ddp_target(struct net_device *dev, u16 xid,
448 struct scatterlist *sgl, unsigned int sgc)
d6534799 449{
7da82c06 450 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
d6534799 451 const struct net_device_ops *ops = real_dev->netdev_ops;
0a89eb92
CL
452 int rc = 0;
453
454 if (ops->ndo_fcoe_ddp_target)
455 rc = ops->ndo_fcoe_ddp_target(real_dev, xid, sgl, sgc);
d6534799 456
d6534799
YZ
457 return rc;
458}
0a89eb92 459#endif
4ea09c9c 460
0a89eb92
CL
461#ifdef NETDEV_FCOE_WWNN
462static int vlan_dev_fcoe_get_wwn(struct net_device *dev, u64 *wwn, int type)
4ea09c9c 463{
7da82c06 464 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
4ea09c9c 465 const struct net_device_ops *ops = real_dev->netdev_ops;
0a89eb92 466 int rc = -EINVAL;
4ea09c9c 467
0a89eb92
CL
468 if (ops->ndo_fcoe_get_wwn)
469 rc = ops->ndo_fcoe_get_wwn(real_dev, wwn, type);
4ea09c9c
YZ
470 return rc;
471}
b85daa53
VD
472#endif
473
ef3eb3e5 474static void vlan_dev_change_rx_flags(struct net_device *dev, int change)
6c78dcbd 475{
7da82c06 476 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
6c78dcbd 477
deede2fa
MK
478 if (dev->flags & IFF_UP) {
479 if (change & IFF_ALLMULTI)
480 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
481 if (change & IFF_PROMISC)
482 dev_set_promiscuity(real_dev, dev->flags & IFF_PROMISC ? 1 : -1);
483 }
6c78dcbd
PM
484}
485
e83a2ea8 486static void vlan_dev_set_rx_mode(struct net_device *vlan_dev)
1da177e4 487{
d38569ab
VY
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);
1da177e4 490}
ef3eb3e5
PM
491
492/*
493 * vlan network devices have devices nesting below it, and are a special
494 * "super class" of normal network devices; split their locks off into a
495 * separate class since they always nest.
496 */
497static struct lock_class_key vlan_netdev_xmit_lock_key;
cf508b12 498static struct lock_class_key vlan_netdev_addr_lock_key;
ef3eb3e5 499
e8a0464c
DM
500static void vlan_dev_set_lockdep_one(struct net_device *dev,
501 struct netdev_queue *txq,
502 void *_subclass)
c773e847
DM
503{
504 lockdep_set_class_and_subclass(&txq->_xmit_lock,
e8a0464c
DM
505 &vlan_netdev_xmit_lock_key,
506 *(int *)_subclass);
c773e847
DM
507}
508
509static void vlan_dev_set_lockdep_class(struct net_device *dev, int subclass)
510{
cf508b12
DM
511 lockdep_set_class_and_subclass(&dev->addr_list_lock,
512 &vlan_netdev_addr_lock_key,
513 subclass);
e8a0464c 514 netdev_for_each_tx_queue(dev, vlan_dev_set_lockdep_one, &subclass);
c773e847
DM
515}
516
d38569ab
VY
517static int vlan_dev_get_lock_subclass(struct net_device *dev)
518{
519 return vlan_dev_priv(dev)->nest_level;
520}
521
ef3eb3e5
PM
522static const struct header_ops vlan_header_ops = {
523 .create = vlan_dev_hard_header,
ef3eb3e5
PM
524 .parse = eth_header_parse,
525};
526
2205369a
DM
527static int vlan_passthru_hard_header(struct sk_buff *skb, struct net_device *dev,
528 unsigned short type,
529 const void *daddr, const void *saddr,
530 unsigned int len)
531{
532 struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
533 struct net_device *real_dev = vlan->real_dev;
534
dd38743b
PB
535 if (saddr == NULL)
536 saddr = dev->dev_addr;
537
2205369a
DM
538 return dev_hard_header(skb, real_dev, type, daddr, saddr, len);
539}
540
541static const struct header_ops vlan_passthru_header_ops = {
542 .create = vlan_passthru_hard_header,
2205369a
DM
543 .parse = eth_header_parse,
544};
545
e949b09b
DG
546static struct device_type vlan_type = {
547 .name = "vlan",
548};
549
213b15ca 550static const struct net_device_ops vlan_netdev_ops;
f7d1b9f5 551
ef3eb3e5
PM
552static int vlan_dev_init(struct net_device *dev)
553{
76052d8c
MM
554 struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
555 struct net_device *real_dev = vlan->real_dev;
ef3eb3e5 556
adc667e8
JV
557 netif_carrier_off(dev);
558
ef3eb3e5 559 /* IFF_BROADCAST|IFF_MULTICAST; ??? */
194dbcc8
JF
560 dev->flags = real_dev->flags & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
561 IFF_MASTER | IFF_SLAVE);
ef3eb3e5
PM
562 dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
563 (1<<__LINK_STATE_DORMANT))) |
564 (1<<__LINK_STATE_PRESENT);
565
76052d8c
MM
566 if (vlan->flags & VLAN_FLAG_BRIDGE_BINDING)
567 dev->state |= (1 << __LINK_STATE_NOCARRIER);
568
a188222b 569 dev->hw_features = NETIF_F_HW_CSUM | NETIF_F_SG |
53f6b708 570 NETIF_F_FRAGLIST | NETIF_F_GSO_SOFTWARE |
7dad9937 571 NETIF_F_GSO_ENCAP_ALL |
53692b1d 572 NETIF_F_HIGHDMA | NETIF_F_SCTP_CRC |
62f2a3a4
MM
573 NETIF_F_ALL_FCOE;
574
88997e42 575 dev->features |= dev->hw_features | NETIF_F_LLTX;
1ae4be22 576 dev->gso_max_size = real_dev->gso_max_size;
f6773c5e 577 dev->gso_max_segs = real_dev->gso_max_segs;
2adb956b
VY
578 if (dev->features & NETIF_F_VLAN_FEATURES)
579 netdev_warn(real_dev, "VLAN features are set incorrectly. Q-in-Q configurations may not work correctly.\n");
580
8d463504 581 dev->vlan_features = real_dev->vlan_features & ~NETIF_F_ALL_FCOE;
7dad9937 582 dev->hw_enc_features = vlan_tnl_features(real_dev);
8b6912a5 583 dev->mpls_features = real_dev->mpls_features;
5fb13570 584
ef3eb3e5
PM
585 /* ipv6 shared card related stuff */
586 dev->dev_id = real_dev->dev_id;
587
308453aa
MM
588 if (is_zero_ether_addr(dev->dev_addr)) {
589 ether_addr_copy(dev->dev_addr, real_dev->dev_addr);
590 dev->addr_assign_type = NET_ADDR_STOLEN;
591 }
ef3eb3e5
PM
592 if (is_zero_ether_addr(dev->broadcast))
593 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
594
f4d5392e 595#if IS_ENABLED(CONFIG_FCOE)
b85daa53
VD
596 dev->fcoe_ddp_xid = real_dev->fcoe_ddp_xid;
597#endif
598
d870bfb9 599 dev->needed_headroom = real_dev->needed_headroom;
76052d8c 600 if (vlan_hw_offload_capable(real_dev->features, vlan->vlan_proto)) {
2205369a 601 dev->header_ops = &vlan_passthru_header_ops;
ef3eb3e5 602 dev->hard_header_len = real_dev->hard_header_len;
ef3eb3e5
PM
603 } else {
604 dev->header_ops = &vlan_header_ops;
605 dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN;
ef3eb3e5
PM
606 }
607
213b15ca 608 dev->netdev_ops = &vlan_netdev_ops;
636e19a3 609
e949b09b
DG
610 SET_NETDEV_DEVTYPE(dev, &vlan_type);
611
d38569ab 612 vlan_dev_set_lockdep_class(dev, vlan_dev_get_lock_subclass(dev));
9793241f 613
76052d8c
MM
614 vlan->vlan_pcpu_stats = netdev_alloc_pcpu_stats(struct vlan_pcpu_stats);
615 if (!vlan->vlan_pcpu_stats)
9793241f
ED
616 return -ENOMEM;
617
ef3eb3e5
PM
618 return 0;
619}
620
23556323
PE
621static void vlan_dev_uninit(struct net_device *dev)
622{
623 struct vlan_priority_tci_mapping *pm;
7da82c06 624 struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
23556323
PE
625 int i;
626
627 for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) {
628 while ((pm = vlan->egress_priority_map[i]) != NULL) {
629 vlan->egress_priority_map[i] = pm->next;
630 kfree(pm);
631 }
632 }
633}
634
c8f44aff
MM
635static netdev_features_t vlan_dev_fix_features(struct net_device *dev,
636 netdev_features_t features)
8a0427bb 637{
7da82c06 638 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
b29d3145 639 netdev_features_t old_features = features;
8403debe 640 netdev_features_t lower_features;
8a0427bb 641
8403debe
VY
642 lower_features = netdev_intersect_features((real_dev->vlan_features |
643 NETIF_F_RXCSUM),
644 real_dev->features);
712ae51a 645
8403debe
VY
646 /* Add HW_CSUM setting to preserve user ability to control
647 * checksum offload on the vlan device.
648 */
649 if (lower_features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))
650 lower_features |= NETIF_F_HW_CSUM;
651 features = netdev_intersect_features(features, lower_features);
53f6b708 652 features |= old_features & (NETIF_F_SOFT_FEATURES | NETIF_F_GSO_SOFTWARE);
f0a619cc 653 features |= NETIF_F_LLTX;
8a0427bb
MM
654
655 return features;
656}
657
57709798
DD
658static int vlan_ethtool_get_link_ksettings(struct net_device *dev,
659 struct ethtool_link_ksettings *cmd)
b3020061 660{
7da82c06 661 const struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
4bc71cb9 662
57709798 663 return __ethtool_get_link_ksettings(vlan->real_dev, cmd);
b3020061
SH
664}
665
666static void vlan_ethtool_get_drvinfo(struct net_device *dev,
667 struct ethtool_drvinfo *info)
668{
7826d43f
JP
669 strlcpy(info->driver, vlan_fullname, sizeof(info->driver));
670 strlcpy(info->version, vlan_version, sizeof(info->version));
671 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
b3020061
SH
672}
673
37dd9255
RC
674static int vlan_ethtool_get_ts_info(struct net_device *dev,
675 struct ethtool_ts_info *info)
676{
677 const struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
678 const struct ethtool_ops *ops = vlan->real_dev->ethtool_ops;
ec1d8ccb 679 struct phy_device *phydev = vlan->real_dev->phydev;
37dd9255 680
ec1d8ccb
HL
681 if (phydev && phydev->drv && phydev->drv->ts_info) {
682 return phydev->drv->ts_info(phydev, info);
683 } else if (ops->get_ts_info) {
37dd9255
RC
684 return ops->get_ts_info(vlan->real_dev, info);
685 } else {
686 info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE |
687 SOF_TIMESTAMPING_SOFTWARE;
688 info->phc_index = -1;
689 }
690
691 return 0;
692}
693
bc1f4470 694static void vlan_dev_get_stats64(struct net_device *dev,
695 struct rtnl_link_stats64 *stats)
9793241f 696{
5a4ae5f6
LR
697 struct vlan_pcpu_stats *p;
698 u32 rx_errors = 0, tx_dropped = 0;
699 int i;
9793241f 700
5a4ae5f6
LR
701 for_each_possible_cpu(i) {
702 u64 rxpackets, rxbytes, rxmulticast, txpackets, txbytes;
703 unsigned int start;
704
705 p = per_cpu_ptr(vlan_dev_priv(dev)->vlan_pcpu_stats, i);
706 do {
707 start = u64_stats_fetch_begin_irq(&p->syncp);
708 rxpackets = p->rx_packets;
709 rxbytes = p->rx_bytes;
710 rxmulticast = p->rx_multicast;
711 txpackets = p->tx_packets;
712 txbytes = p->tx_bytes;
713 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
714
715 stats->rx_packets += rxpackets;
716 stats->rx_bytes += rxbytes;
717 stats->multicast += rxmulticast;
718 stats->tx_packets += txpackets;
719 stats->tx_bytes += txbytes;
720 /* rx_errors & tx_dropped are u32 */
721 rx_errors += p->rx_errors;
722 tx_dropped += p->tx_dropped;
9793241f 723 }
5a4ae5f6
LR
724 stats->rx_errors = rx_errors;
725 stats->tx_dropped = tx_dropped;
9793241f
ED
726}
727
6d4cdf47 728#ifdef CONFIG_NET_POLL_CONTROLLER
de93cb2e 729static void vlan_dev_poll_controller(struct net_device *dev)
6d4cdf47
BL
730{
731 return;
732}
733
a8779ec1 734static int vlan_dev_netpoll_setup(struct net_device *dev, struct netpoll_info *npinfo)
6d4cdf47 735{
f3da3893
AW
736 struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
737 struct net_device *real_dev = vlan->real_dev;
6d4cdf47
BL
738 struct netpoll *netpoll;
739 int err = 0;
740
a8779ec1 741 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
6d4cdf47
BL
742 err = -ENOMEM;
743 if (!netpoll)
744 goto out;
745
a8779ec1 746 err = __netpoll_setup(netpoll, real_dev);
6d4cdf47
BL
747 if (err) {
748 kfree(netpoll);
749 goto out;
750 }
751
f3da3893 752 vlan->netpoll = netpoll;
6d4cdf47
BL
753
754out:
755 return err;
756}
757
de93cb2e 758static void vlan_dev_netpoll_cleanup(struct net_device *dev)
6d4cdf47 759{
f3da3893
AW
760 struct vlan_dev_priv *vlan= vlan_dev_priv(dev);
761 struct netpoll *netpoll = vlan->netpoll;
6d4cdf47
BL
762
763 if (!netpoll)
764 return;
765
f3da3893 766 vlan->netpoll = NULL;
c9fbd71f 767 __netpoll_free(netpoll);
6d4cdf47
BL
768}
769#endif /* CONFIG_NET_POLL_CONTROLLER */
770
2dbf6b50
ND
771static int vlan_dev_get_iflink(const struct net_device *dev)
772{
773 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
774
775 return real_dev->ifindex;
776}
777
75b8846a 778static const struct ethtool_ops vlan_ethtool_ops = {
57709798 779 .get_link_ksettings = vlan_ethtool_get_link_ksettings,
b3020061 780 .get_drvinfo = vlan_ethtool_get_drvinfo,
75b8846a 781 .get_link = ethtool_op_get_link,
37dd9255 782 .get_ts_info = vlan_ethtool_get_ts_info,
75b8846a
PM
783};
784
656299f7
SH
785static const struct net_device_ops vlan_netdev_ops = {
786 .ndo_change_mtu = vlan_dev_change_mtu,
787 .ndo_init = vlan_dev_init,
788 .ndo_uninit = vlan_dev_uninit,
789 .ndo_open = vlan_dev_open,
790 .ndo_stop = vlan_dev_stop,
669d3e0b
VD
791 .ndo_start_xmit = vlan_dev_hard_start_xmit,
792 .ndo_validate_addr = eth_validate_addr,
793 .ndo_set_mac_address = vlan_dev_set_mac_address,
794 .ndo_set_rx_mode = vlan_dev_set_rx_mode,
669d3e0b
VD
795 .ndo_change_rx_flags = vlan_dev_change_rx_flags,
796 .ndo_do_ioctl = vlan_dev_ioctl,
797 .ndo_neigh_setup = vlan_dev_neigh_setup,
9618e2ff 798 .ndo_get_stats64 = vlan_dev_get_stats64,
f4d5392e 799#if IS_ENABLED(CONFIG_FCOE)
669d3e0b
VD
800 .ndo_fcoe_ddp_setup = vlan_dev_fcoe_ddp_setup,
801 .ndo_fcoe_ddp_done = vlan_dev_fcoe_ddp_done,
802 .ndo_fcoe_enable = vlan_dev_fcoe_enable,
803 .ndo_fcoe_disable = vlan_dev_fcoe_disable,
4ea09c9c 804 .ndo_fcoe_ddp_target = vlan_dev_fcoe_ddp_target,
6d4cdf47 805#endif
0a89eb92
CL
806#ifdef NETDEV_FCOE_WWNN
807 .ndo_fcoe_get_wwn = vlan_dev_fcoe_get_wwn,
808#endif
6d4cdf47
BL
809#ifdef CONFIG_NET_POLL_CONTROLLER
810 .ndo_poll_controller = vlan_dev_poll_controller,
811 .ndo_netpoll_setup = vlan_dev_netpoll_setup,
812 .ndo_netpoll_cleanup = vlan_dev_netpoll_cleanup,
669d3e0b 813#endif
8a0427bb 814 .ndo_fix_features = vlan_dev_fix_features,
d38569ab 815 .ndo_get_lock_subclass = vlan_dev_get_lock_subclass,
2dbf6b50 816 .ndo_get_iflink = vlan_dev_get_iflink,
669d3e0b
VD
817};
818
a48e5faf
ED
819static void vlan_dev_free(struct net_device *dev)
820{
821 struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
822
823 free_percpu(vlan->vlan_pcpu_stats);
824 vlan->vlan_pcpu_stats = NULL;
a48e5faf
ED
825}
826
ef3eb3e5
PM
827void vlan_setup(struct net_device *dev)
828{
829 ether_setup(dev);
830
2e659c05 831 dev->priv_flags |= IFF_802_1Q_VLAN | IFF_NO_QUEUE;
e817af27 832 dev->priv_flags |= IFF_UNICAST_FLT;
02875878
ED
833 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
834 netif_keep_dst(dev);
ef3eb3e5 835
656299f7 836 dev->netdev_ops = &vlan_netdev_ops;
cf124db5
DM
837 dev->needs_free_netdev = true;
838 dev->priv_destructor = vlan_dev_free;
75b8846a 839 dev->ethtool_ops = &vlan_ethtool_ops;
ef3eb3e5 840
91572088
JW
841 dev->min_mtu = 0;
842 dev->max_mtu = ETH_MAX_MTU;
843
423049ab 844 eth_zero_addr(dev->broadcast);
ef3eb3e5 845}