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