blob: 3986a170d0326a099ee8351585e599b446ffd8c3 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/* -*- linux-c -*-
2 * INET 802.1Q VLAN
3 * Ethernet-type device handling.
4 *
5 * Authors: Ben Greear <greearb@candelatech.com>
6 * Please send support related email to: netdev@vger.kernel.org
7 * VLAN Home Page: http://www.candelatech.com/~greear/vlan.html
8 *
9 * Fixes: Mar 22 2001: Martin Bokaemper <mbokaemper@unispherenetworks.com>
10 * - reset skb->pkt_type on incoming packets when MAC was changed
11 * - see that changed MAC is saddr for outgoing packets
12 * Oct 20, 2001: Ard van Breeman:
13 * - Fix MC-list, finally.
14 * - Flush MC-list on VLAN destroy.
15 *
16 *
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version
20 * 2 of the License, or (at your option) any later version.
21 */
22
23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25#include <linux/module.h>
26#include <linux/slab.h>
27#include <linux/skbuff.h>
28#include <linux/netdevice.h>
29#include <linux/etherdevice.h>
30#include <linux/ethtool.h>
31#include <net/arp.h>
32
33#include "vlan.h"
34#include "vlanproc.h"
35#include <linux/if_vlan.h>
36#include <linux/netpoll.h>
37
38/*
39 * Rebuild the Ethernet MAC header. This is called after an ARP
40 * (or in future other address resolution) has completed on this
41 * sk_buff. We now let ARP fill in the other fields.
42 *
43 * This routine CANNOT use cached dst->neigh!
44 * Really, it is used only when dst->neigh is wrong.
45 *
46 * TODO: This needs a checkup, I'm ignorant here. --BLG
47 */
48static int vlan_dev_rebuild_header(struct sk_buff *skb)
49{
50 struct net_device *dev = skb->dev;
51 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
52
53 switch (veth->h_vlan_encapsulated_proto) {
54#ifdef CONFIG_INET
55 case htons(ETH_P_IP):
56
57 /* TODO: Confirm this will work with VLAN headers... */
58 return arp_find(veth->h_dest, skb);
59#endif
60 default:
61 pr_debug("%s: unable to resolve type %X addresses\n",
62 dev->name, ntohs(veth->h_vlan_encapsulated_proto));
63
64 memcpy(veth->h_source, dev->dev_addr, ETH_ALEN);
65 break;
66 }
67
68 return 0;
69}
70
71static inline u16
72vlan_dev_get_egress_qos_mask(struct net_device *dev, struct sk_buff *skb)
73{
74 struct vlan_priority_tci_mapping *mp;
75
76 smp_rmb(); /* coupled with smp_wmb() in vlan_dev_set_egress_priority() */
77
78 mp = vlan_dev_priv(dev)->egress_priority_map[(skb->priority & 0xF)];
79 while (mp) {
80 if (mp->priority == skb->priority) {
81 return mp->vlan_qos; /* This should already be shifted
82 * to mask correctly with the
83 * VLAN's TCI */
84 }
85 mp = mp->next;
86 }
87 return 0;
88}
89
90/*
91 * Create the VLAN header for an arbitrary protocol layer
92 *
93 * saddr=NULL means use device source address
94 * daddr=NULL means leave destination address (eg unresolved arp)
95 *
96 * This is called when the SKB is moving down the stack towards the
97 * physical devices.
98 */
99static int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
100 unsigned short type,
101 const void *daddr, const void *saddr,
102 unsigned int len)
103{
104 struct vlan_hdr *vhdr;
105 unsigned int vhdrlen = 0;
106 u16 vlan_tci = 0;
107 int rc;
108
109 if (!(vlan_dev_priv(dev)->flags & VLAN_FLAG_REORDER_HDR)) {
110 vhdr = (struct vlan_hdr *) skb_push(skb, VLAN_HLEN);
111
112 vlan_tci = vlan_dev_priv(dev)->vlan_id;
113 vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb);
114 vhdr->h_vlan_TCI = htons(vlan_tci);
115
116 /*
117 * Set the protocol type. For a packet of type ETH_P_802_3/2 we
118 * put the length in here instead.
119 */
120 if (type != ETH_P_802_3 && type != ETH_P_802_2)
121 vhdr->h_vlan_encapsulated_proto = htons(type);
122 else
123 vhdr->h_vlan_encapsulated_proto = htons(len);
124
125 skb->protocol = htons(ETH_P_8021Q);
126 type = ETH_P_8021Q;
127 vhdrlen = VLAN_HLEN;
128 }
129
130 /* Before delegating work to the lower layer, enter our MAC-address */
131 if (saddr == NULL)
132 saddr = dev->dev_addr;
133
134 /* Now make the underlying real hard header */
135 dev = vlan_dev_priv(dev)->real_dev;
136 net_run_track(PRT_VLAN," vlan_dev_hard_header may not call,connect sunquan!!!\n");
137 rc = dev_hard_header(skb, dev, type, daddr, saddr, len + vhdrlen);
138 if (rc > 0)
139 rc += vhdrlen;
140 return rc;
141}
142
143static netdev_tx_t vlan_dev_hard_start_xmit(struct sk_buff *skb,
144 struct net_device *dev)
145{
146 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
147 unsigned int len;
148 int ret;
149
150 /* Handle non-VLAN frames if they are sent to us, for example by DHCP.
151 *
152 * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING
153 * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs...
154 */
155 if (veth->h_vlan_proto != htons(ETH_P_8021Q) ||
156 vlan_dev_priv(dev)->flags & VLAN_FLAG_REORDER_HDR) {
157 u16 vlan_tci;
158 vlan_tci = vlan_dev_priv(dev)->vlan_id;
159 vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb);
160 skb = __vlan_hwaccel_put_tag(skb, vlan_tci);
161 }
162
163 skb->dev = vlan_dev_priv(dev)->real_dev;
164 len = skb->len;
165 if (netpoll_tx_running(dev))
166 return skb->dev->netdev_ops->ndo_start_xmit(skb, skb->dev);
167 ret = dev_queue_xmit(skb);
168
169 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
170 struct vlan_pcpu_stats *stats;
171
172 stats = this_cpu_ptr(vlan_dev_priv(dev)->vlan_pcpu_stats);
173 u64_stats_update_begin(&stats->syncp);
174 stats->tx_packets++;
175 stats->tx_bytes += len;
176 u64_stats_update_end(&stats->syncp);
177 } else {
178 this_cpu_inc(vlan_dev_priv(dev)->vlan_pcpu_stats->tx_dropped);
179 }
180
181 return ret;
182}
183
184static int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
185{
186 /* TODO: gotta make sure the underlying layer can handle it,
187 * maybe an IFF_VLAN_CAPABLE flag for devices?
188 */
189 if (vlan_dev_priv(dev)->real_dev->mtu < new_mtu)
190 return -ERANGE;
191
192 dev->mtu = new_mtu;
193
194 return 0;
195}
196
197void vlan_dev_set_ingress_priority(const struct net_device *dev,
198 u32 skb_prio, u16 vlan_prio)
199{
200 struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
201
202 if (vlan->ingress_priority_map[vlan_prio & 0x7] && !skb_prio)
203 vlan->nr_ingress_mappings--;
204 else if (!vlan->ingress_priority_map[vlan_prio & 0x7] && skb_prio)
205 vlan->nr_ingress_mappings++;
206
207 vlan->ingress_priority_map[vlan_prio & 0x7] = skb_prio;
208}
209
210int vlan_dev_set_egress_priority(const struct net_device *dev,
211 u32 skb_prio, u16 vlan_prio)
212{
213 struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
214 struct vlan_priority_tci_mapping *mp = NULL;
215 struct vlan_priority_tci_mapping *np;
216 u32 vlan_qos = (vlan_prio << VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK;
217
218 /* See if a priority mapping exists.. */
219 mp = vlan->egress_priority_map[skb_prio & 0xF];
220 while (mp) {
221 if (mp->priority == skb_prio) {
222 if (mp->vlan_qos && !vlan_qos)
223 vlan->nr_egress_mappings--;
224 else if (!mp->vlan_qos && vlan_qos)
225 vlan->nr_egress_mappings++;
226 mp->vlan_qos = vlan_qos;
227 return 0;
228 }
229 mp = mp->next;
230 }
231
232 /* Create a new mapping then. */
233 mp = vlan->egress_priority_map[skb_prio & 0xF];
234 np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL);
235 if (!np)
236 return -ENOBUFS;
237
238 np->next = mp;
239 np->priority = skb_prio;
240 np->vlan_qos = vlan_qos;
241 /* Before inserting this element in hash table, make sure all its fields
242 * are committed to memory.
243 * coupled with smp_rmb() in vlan_dev_get_egress_qos_mask()
244 */
245 smp_wmb();
246 vlan->egress_priority_map[skb_prio & 0xF] = np;
247 if (vlan_qos)
248 vlan->nr_egress_mappings++;
249 return 0;
250}
251
252/* Flags are defined in the vlan_flags enum in include/linux/if_vlan.h file. */
253int vlan_dev_change_flags(const struct net_device *dev, u32 flags, u32 mask)
254{
255 struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
256 u32 old_flags = vlan->flags;
257
258 if (mask & ~(VLAN_FLAG_REORDER_HDR | VLAN_FLAG_GVRP |
259 VLAN_FLAG_LOOSE_BINDING))
260 return -EINVAL;
261
262 vlan->flags = (old_flags & ~mask) | (flags & mask);
263
264 if (netif_running(dev) && (vlan->flags ^ old_flags) & VLAN_FLAG_GVRP) {
265 if (vlan->flags & VLAN_FLAG_GVRP)
266 vlan_gvrp_request_join(dev);
267 else
268 vlan_gvrp_request_leave(dev);
269 }
270 return 0;
271}
272
273void vlan_dev_get_realdev_name(const struct net_device *dev, char *result)
274{
275 strncpy(result, vlan_dev_priv(dev)->real_dev->name, 23);
276}
277
278static int vlan_dev_open(struct net_device *dev)
279{
280 struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
281 struct net_device *real_dev = vlan->real_dev;
282 int err;
283
284 if (!(real_dev->flags & IFF_UP) &&
285 !(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
286 return -ENETDOWN;
287
288 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr)) {
289 err = dev_uc_add(real_dev, dev->dev_addr);
290 if (err < 0)
291 goto out;
292 }
293
294 if (dev->flags & IFF_ALLMULTI) {
295 err = dev_set_allmulti(real_dev, 1);
296 if (err < 0)
297 goto del_unicast;
298 }
299 if (dev->flags & IFF_PROMISC) {
300 err = dev_set_promiscuity(real_dev, 1);
301 if (err < 0)
302 goto clear_allmulti;
303 }
304
305 memcpy(vlan->real_dev_addr, real_dev->dev_addr, ETH_ALEN);
306
307 if (vlan->flags & VLAN_FLAG_GVRP)
308 vlan_gvrp_request_join(dev);
309
310 if (netif_carrier_ok(real_dev))
311 netif_carrier_on(dev);
312 return 0;
313
314clear_allmulti:
315 if (dev->flags & IFF_ALLMULTI)
316 dev_set_allmulti(real_dev, -1);
317del_unicast:
318 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
319 dev_uc_del(real_dev, dev->dev_addr);
320out:
321 netif_carrier_off(dev);
322 return err;
323}
324
325static int vlan_dev_stop(struct net_device *dev)
326{
327 struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
328 struct net_device *real_dev = vlan->real_dev;
329
330 dev_mc_unsync(real_dev, dev);
331 dev_uc_unsync(real_dev, dev);
332 if (dev->flags & IFF_ALLMULTI)
333 dev_set_allmulti(real_dev, -1);
334 if (dev->flags & IFF_PROMISC)
335 dev_set_promiscuity(real_dev, -1);
336
337 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
338 dev_uc_del(real_dev, dev->dev_addr);
339
340 netif_carrier_off(dev);
341 return 0;
342}
343
344static int vlan_dev_set_mac_address(struct net_device *dev, void *p)
345{
346 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
347 struct sockaddr *addr = p;
348 int err;
349
350 if (!is_valid_ether_addr(addr->sa_data))
351 return -EADDRNOTAVAIL;
352
353 if (!(dev->flags & IFF_UP))
354 goto out;
355
356 if (compare_ether_addr(addr->sa_data, real_dev->dev_addr)) {
357 err = dev_uc_add(real_dev, addr->sa_data);
358 if (err < 0)
359 return err;
360 }
361
362 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
363 dev_uc_del(real_dev, dev->dev_addr);
364
365out:
366 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
367 return 0;
368}
369
370static int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
371{
372 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
373 const struct net_device_ops *ops = real_dev->netdev_ops;
374 struct ifreq ifrr;
375 int err = -EOPNOTSUPP;
376
377 strncpy(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 if (netif_device_present(real_dev) && ops->ndo_do_ioctl)
385 err = ops->ndo_do_ioctl(real_dev, &ifrr, cmd);
386 break;
387 }
388
389 if (!err)
390 ifr->ifr_ifru = ifrr.ifr_ifru;
391
392 return err;
393}
394
395static int vlan_dev_neigh_setup(struct net_device *dev, struct neigh_parms *pa)
396{
397 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
398 const struct net_device_ops *ops = real_dev->netdev_ops;
399 int err = 0;
400
401 if (netif_device_present(real_dev) && ops->ndo_neigh_setup)
402 err = ops->ndo_neigh_setup(real_dev, pa);
403
404 return err;
405}
406
407#if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
408static int vlan_dev_fcoe_ddp_setup(struct net_device *dev, u16 xid,
409 struct scatterlist *sgl, unsigned int sgc)
410{
411 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
412 const struct net_device_ops *ops = real_dev->netdev_ops;
413 int rc = 0;
414
415 if (ops->ndo_fcoe_ddp_setup)
416 rc = ops->ndo_fcoe_ddp_setup(real_dev, xid, sgl, sgc);
417
418 return rc;
419}
420
421static int vlan_dev_fcoe_ddp_done(struct net_device *dev, u16 xid)
422{
423 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
424 const struct net_device_ops *ops = real_dev->netdev_ops;
425 int len = 0;
426
427 if (ops->ndo_fcoe_ddp_done)
428 len = ops->ndo_fcoe_ddp_done(real_dev, xid);
429
430 return len;
431}
432
433static int vlan_dev_fcoe_enable(struct net_device *dev)
434{
435 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
436 const struct net_device_ops *ops = real_dev->netdev_ops;
437 int rc = -EINVAL;
438
439 if (ops->ndo_fcoe_enable)
440 rc = ops->ndo_fcoe_enable(real_dev);
441 return rc;
442}
443
444static int vlan_dev_fcoe_disable(struct net_device *dev)
445{
446 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
447 const struct net_device_ops *ops = real_dev->netdev_ops;
448 int rc = -EINVAL;
449
450 if (ops->ndo_fcoe_disable)
451 rc = ops->ndo_fcoe_disable(real_dev);
452 return rc;
453}
454
455static int vlan_dev_fcoe_get_wwn(struct net_device *dev, u64 *wwn, int type)
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 = -EINVAL;
460
461 if (ops->ndo_fcoe_get_wwn)
462 rc = ops->ndo_fcoe_get_wwn(real_dev, wwn, type);
463 return rc;
464}
465
466static int vlan_dev_fcoe_ddp_target(struct net_device *dev, u16 xid,
467 struct scatterlist *sgl, unsigned int sgc)
468{
469 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
470 const struct net_device_ops *ops = real_dev->netdev_ops;
471 int rc = 0;
472
473 if (ops->ndo_fcoe_ddp_target)
474 rc = ops->ndo_fcoe_ddp_target(real_dev, xid, sgl, sgc);
475
476 return rc;
477}
478#endif
479
480static void vlan_dev_change_rx_flags(struct net_device *dev, int change)
481{
482 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
483
484 if (dev->flags & IFF_UP) {
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
492static void vlan_dev_set_rx_mode(struct net_device *vlan_dev)
493{
494 dev_mc_sync(vlan_dev_priv(vlan_dev)->real_dev, vlan_dev);
495 dev_uc_sync(vlan_dev_priv(vlan_dev)->real_dev, vlan_dev);
496}
497
498/*
499 * vlan network devices have devices nesting below it, and are a special
500 * "super class" of normal network devices; split their locks off into a
501 * separate class since they always nest.
502 */
503static struct lock_class_key vlan_netdev_xmit_lock_key;
504static struct lock_class_key vlan_netdev_addr_lock_key;
505
506static void vlan_dev_set_lockdep_one(struct net_device *dev,
507 struct netdev_queue *txq,
508 void *_subclass)
509{
510 lockdep_set_class_and_subclass(&txq->_xmit_lock,
511 &vlan_netdev_xmit_lock_key,
512 *(int *)_subclass);
513}
514
515static void vlan_dev_set_lockdep_class(struct net_device *dev, int subclass)
516{
517 lockdep_set_class_and_subclass(&dev->addr_list_lock,
518 &vlan_netdev_addr_lock_key,
519 subclass);
520 netdev_for_each_tx_queue(dev, vlan_dev_set_lockdep_one, &subclass);
521}
522
523static const struct header_ops vlan_header_ops = {
524 .create = vlan_dev_hard_header,
525 .rebuild = vlan_dev_rebuild_header,
526 .parse = eth_header_parse,
527};
528
529static int vlan_passthru_hard_header(struct sk_buff *skb, struct net_device *dev,
530 unsigned short type,
531 const void *daddr, const void *saddr,
532 unsigned int len)
533{
534 struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
535 struct net_device *real_dev = vlan->real_dev;
536
537 if (saddr == NULL)
538 saddr = dev->dev_addr;
539
540 return dev_hard_header(skb, real_dev, type, daddr, saddr, len);
541}
542
543static const struct header_ops vlan_passthru_header_ops = {
544 .create = vlan_passthru_hard_header,
545 .rebuild = dev_rebuild_header,
546 .parse = eth_header_parse,
547};
548
549static const struct net_device_ops vlan_netdev_ops;
550
551static int vlan_dev_init(struct net_device *dev)
552{
553 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
554 int subclass = 0;
555
556 netif_carrier_off(dev);
557
558 /* IFF_BROADCAST|IFF_MULTICAST; ??? */
559 dev->flags = real_dev->flags & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
560 IFF_MASTER | IFF_SLAVE);
561 dev->iflink = real_dev->ifindex;
562 dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
563 (1<<__LINK_STATE_DORMANT))) |
564 (1<<__LINK_STATE_PRESENT);
565
566 dev->hw_features = NETIF_F_ALL_CSUM | NETIF_F_SG |
567 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO |
568 NETIF_F_HIGHDMA | NETIF_F_SCTP_CSUM |
569 NETIF_F_ALL_FCOE;
570
571 dev->features |= real_dev->vlan_features | NETIF_F_LLTX;
572 dev->gso_max_size = real_dev->gso_max_size;
573
574 /* ipv6 shared card related stuff */
575 dev->dev_id = real_dev->dev_id;
576
577 if (is_zero_ether_addr(dev->dev_addr))
578 memcpy(dev->dev_addr, real_dev->dev_addr, dev->addr_len);
579 if (is_zero_ether_addr(dev->broadcast))
580 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
581
582#if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
583 dev->fcoe_ddp_xid = real_dev->fcoe_ddp_xid;
584#endif
585
586 dev->needed_headroom = real_dev->needed_headroom;
587 if (real_dev->features & NETIF_F_HW_VLAN_TX) {
588 dev->header_ops = &vlan_passthru_header_ops;
589 dev->hard_header_len = real_dev->hard_header_len;
590 } else {
591 dev->header_ops = &vlan_header_ops;
592 dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN;
593 }
594
595 dev->netdev_ops = &vlan_netdev_ops;
596
597 if (is_vlan_dev(real_dev))
598 subclass = 1;
599
600 vlan_dev_set_lockdep_class(dev, subclass);
601
602 vlan_dev_priv(dev)->vlan_pcpu_stats = alloc_percpu(struct vlan_pcpu_stats);
603 if (!vlan_dev_priv(dev)->vlan_pcpu_stats)
604 return -ENOMEM;
605
606 return 0;
607}
608
609static void vlan_dev_uninit(struct net_device *dev)
610{
611 struct vlan_priority_tci_mapping *pm;
612 struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
613 int i;
614
615 free_percpu(vlan->vlan_pcpu_stats);
616 vlan->vlan_pcpu_stats = NULL;
617 for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) {
618 while ((pm = vlan->egress_priority_map[i]) != NULL) {
619 vlan->egress_priority_map[i] = pm->next;
620 kfree(pm);
621 }
622 }
623}
624
625static netdev_features_t vlan_dev_fix_features(struct net_device *dev,
626 netdev_features_t features)
627{
628 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
629 netdev_features_t old_features = features;
630
631 features &= real_dev->vlan_features;
632 features |= NETIF_F_RXCSUM;
633 features &= real_dev->features;
634
635 features |= old_features & NETIF_F_SOFT_FEATURES;
636 features |= NETIF_F_LLTX;
637
638 return features;
639}
640
641static int vlan_ethtool_get_settings(struct net_device *dev,
642 struct ethtool_cmd *cmd)
643{
644 const struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
645
646 return __ethtool_get_settings(vlan->real_dev, cmd);
647}
648
649static void vlan_ethtool_get_drvinfo(struct net_device *dev,
650 struct ethtool_drvinfo *info)
651{
652 strcpy(info->driver, vlan_fullname);
653 strcpy(info->version, vlan_version);
654 strcpy(info->fw_version, "N/A");
655}
656
657static struct rtnl_link_stats64 *vlan_dev_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
658{
659
660 if (vlan_dev_priv(dev)->vlan_pcpu_stats) {
661 struct vlan_pcpu_stats *p;
662 u32 rx_errors = 0, tx_dropped = 0;
663 int i;
664
665 for_each_possible_cpu(i) {
666 u64 rxpackets, rxbytes, rxmulticast, txpackets, txbytes;
667 unsigned int start;
668
669 p = per_cpu_ptr(vlan_dev_priv(dev)->vlan_pcpu_stats, i);
670 do {
671 start = u64_stats_fetch_begin_bh(&p->syncp);
672 rxpackets = p->rx_packets;
673 rxbytes = p->rx_bytes;
674 rxmulticast = p->rx_multicast;
675 txpackets = p->tx_packets;
676 txbytes = p->tx_bytes;
677 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
678
679 stats->rx_packets += rxpackets;
680 stats->rx_bytes += rxbytes;
681 stats->multicast += rxmulticast;
682 stats->tx_packets += txpackets;
683 stats->tx_bytes += txbytes;
684 /* rx_errors & tx_dropped are u32 */
685 rx_errors += p->rx_errors;
686 tx_dropped += p->tx_dropped;
687 }
688 stats->rx_errors = rx_errors;
689 stats->tx_dropped = tx_dropped;
690 }
691 return stats;
692}
693
694#ifdef CONFIG_NET_POLL_CONTROLLER
695static void vlan_dev_poll_controller(struct net_device *dev)
696{
697 return;
698}
699
700static int vlan_dev_netpoll_setup(struct net_device *dev, struct netpoll_info *npinfo)
701{
702 struct vlan_dev_priv *info = vlan_dev_priv(dev);
703 struct net_device *real_dev = info->real_dev;
704 struct netpoll *netpoll;
705 int err = 0;
706
707 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
708 err = -ENOMEM;
709 if (!netpoll)
710 goto out;
711
712 netpoll->dev = real_dev;
713 strlcpy(netpoll->dev_name, real_dev->name, IFNAMSIZ);
714
715 err = __netpoll_setup(netpoll);
716 if (err) {
717 kfree(netpoll);
718 goto out;
719 }
720
721 info->netpoll = netpoll;
722
723out:
724 return err;
725}
726
727static void vlan_dev_netpoll_cleanup(struct net_device *dev)
728{
729 struct vlan_dev_priv *info = vlan_dev_priv(dev);
730 struct netpoll *netpoll = info->netpoll;
731
732 if (!netpoll)
733 return;
734
735 info->netpoll = NULL;
736
737 /* Wait for transmitting packets to finish before freeing. */
738 synchronize_rcu_bh();
739
740 __netpoll_cleanup(netpoll);
741 kfree(netpoll);
742}
743#endif /* CONFIG_NET_POLL_CONTROLLER */
744
745static const struct ethtool_ops vlan_ethtool_ops = {
746 .get_settings = vlan_ethtool_get_settings,
747 .get_drvinfo = vlan_ethtool_get_drvinfo,
748 .get_link = ethtool_op_get_link,
749};
750
751static const struct net_device_ops vlan_netdev_ops = {
752 .ndo_change_mtu = vlan_dev_change_mtu,
753 .ndo_init = vlan_dev_init,
754 .ndo_uninit = vlan_dev_uninit,
755 .ndo_open = vlan_dev_open,
756 .ndo_stop = vlan_dev_stop,
757 .ndo_start_xmit = vlan_dev_hard_start_xmit,
758 .ndo_validate_addr = eth_validate_addr,
759 .ndo_set_mac_address = vlan_dev_set_mac_address,
760 .ndo_set_rx_mode = vlan_dev_set_rx_mode,
761 .ndo_change_rx_flags = vlan_dev_change_rx_flags,
762 .ndo_do_ioctl = vlan_dev_ioctl,
763 .ndo_neigh_setup = vlan_dev_neigh_setup,
764 .ndo_get_stats64 = vlan_dev_get_stats64,
765#if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
766 .ndo_fcoe_ddp_setup = vlan_dev_fcoe_ddp_setup,
767 .ndo_fcoe_ddp_done = vlan_dev_fcoe_ddp_done,
768 .ndo_fcoe_enable = vlan_dev_fcoe_enable,
769 .ndo_fcoe_disable = vlan_dev_fcoe_disable,
770 .ndo_fcoe_get_wwn = vlan_dev_fcoe_get_wwn,
771 .ndo_fcoe_ddp_target = vlan_dev_fcoe_ddp_target,
772#endif
773#ifdef CONFIG_NET_POLL_CONTROLLER
774 .ndo_poll_controller = vlan_dev_poll_controller,
775 .ndo_netpoll_setup = vlan_dev_netpoll_setup,
776 .ndo_netpoll_cleanup = vlan_dev_netpoll_cleanup,
777#endif
778 .ndo_fix_features = vlan_dev_fix_features,
779};
780
781void vlan_setup(struct net_device *dev)
782{
783 ether_setup(dev);
784
785 dev->priv_flags |= IFF_802_1Q_VLAN;
786 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
787 dev->tx_queue_len = 0;
788
789 dev->netdev_ops = &vlan_netdev_ops;
790 dev->destructor = free_netdev;
791 dev->ethtool_ops = &vlan_ethtool_ops;
792
793 memset(dev->broadcast, 0, ETH_ALEN);
794}