Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* -*- linux-c -*-
0003  * INET     802.1Q VLAN
0004  *      Ethernet-type device handling.
0005  *
0006  * Authors: Ben Greear <greearb@candelatech.com>
0007  *              Please send support related email to: netdev@vger.kernel.org
0008  *              VLAN Home Page: http://www.candelatech.com/~greear/vlan.html
0009  *
0010  * Fixes:       Mar 22 2001: Martin Bokaemper <mbokaemper@unispherenetworks.com>
0011  *                - reset skb->pkt_type on incoming packets when MAC was changed
0012  *                - see that changed MAC is saddr for outgoing packets
0013  *              Oct 20, 2001:  Ard van Breeman:
0014  *                - Fix MC-list, finally.
0015  *                - Flush MC-list on VLAN destroy.
0016  */
0017 
0018 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0019 
0020 #include <linux/module.h>
0021 #include <linux/slab.h>
0022 #include <linux/skbuff.h>
0023 #include <linux/netdevice.h>
0024 #include <linux/net_tstamp.h>
0025 #include <linux/etherdevice.h>
0026 #include <linux/ethtool.h>
0027 #include <linux/phy.h>
0028 #include <net/arp.h>
0029 
0030 #include "vlan.h"
0031 #include "vlanproc.h"
0032 #include <linux/if_vlan.h>
0033 #include <linux/netpoll.h>
0034 
0035 /*
0036  *  Create the VLAN header for an arbitrary protocol layer
0037  *
0038  *  saddr=NULL  means use device source address
0039  *  daddr=NULL  means leave destination address (eg unresolved arp)
0040  *
0041  *  This is called when the SKB is moving down the stack towards the
0042  *  physical devices.
0043  */
0044 static int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
0045                 unsigned short type,
0046                 const void *daddr, const void *saddr,
0047                 unsigned int len)
0048 {
0049     struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
0050     struct vlan_hdr *vhdr;
0051     unsigned int vhdrlen = 0;
0052     u16 vlan_tci = 0;
0053     int rc;
0054 
0055     if (!(vlan->flags & VLAN_FLAG_REORDER_HDR)) {
0056         vhdr = skb_push(skb, VLAN_HLEN);
0057 
0058         vlan_tci = vlan->vlan_id;
0059         vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb->priority);
0060         vhdr->h_vlan_TCI = htons(vlan_tci);
0061 
0062         /*
0063          *  Set the protocol type. For a packet of type ETH_P_802_3/2 we
0064          *  put the length in here instead.
0065          */
0066         if (type != ETH_P_802_3 && type != ETH_P_802_2)
0067             vhdr->h_vlan_encapsulated_proto = htons(type);
0068         else
0069             vhdr->h_vlan_encapsulated_proto = htons(len);
0070 
0071         skb->protocol = vlan->vlan_proto;
0072         type = ntohs(vlan->vlan_proto);
0073         vhdrlen = VLAN_HLEN;
0074     }
0075 
0076     /* Before delegating work to the lower layer, enter our MAC-address */
0077     if (saddr == NULL)
0078         saddr = dev->dev_addr;
0079 
0080     /* Now make the underlying real hard header */
0081     dev = vlan->real_dev;
0082     rc = dev_hard_header(skb, dev, type, daddr, saddr, len + vhdrlen);
0083     if (rc > 0)
0084         rc += vhdrlen;
0085     return rc;
0086 }
0087 
0088 static inline netdev_tx_t vlan_netpoll_send_skb(struct vlan_dev_priv *vlan, struct sk_buff *skb)
0089 {
0090 #ifdef CONFIG_NET_POLL_CONTROLLER
0091     return netpoll_send_skb(vlan->netpoll, skb);
0092 #else
0093     BUG();
0094     return NETDEV_TX_OK;
0095 #endif
0096 }
0097 
0098 static netdev_tx_t vlan_dev_hard_start_xmit(struct sk_buff *skb,
0099                         struct net_device *dev)
0100 {
0101     struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
0102     struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
0103     unsigned int len;
0104     int ret;
0105 
0106     /* Handle non-VLAN frames if they are sent to us, for example by DHCP.
0107      *
0108      * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING
0109      * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs...
0110      */
0111     if (veth->h_vlan_proto != vlan->vlan_proto ||
0112         vlan->flags & VLAN_FLAG_REORDER_HDR) {
0113         u16 vlan_tci;
0114         vlan_tci = vlan->vlan_id;
0115         vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb->priority);
0116         __vlan_hwaccel_put_tag(skb, vlan->vlan_proto, vlan_tci);
0117     }
0118 
0119     skb->dev = vlan->real_dev;
0120     len = skb->len;
0121     if (unlikely(netpoll_tx_running(dev)))
0122         return vlan_netpoll_send_skb(vlan, skb);
0123 
0124     ret = dev_queue_xmit(skb);
0125 
0126     if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
0127         struct vlan_pcpu_stats *stats;
0128 
0129         stats = this_cpu_ptr(vlan->vlan_pcpu_stats);
0130         u64_stats_update_begin(&stats->syncp);
0131         u64_stats_inc(&stats->tx_packets);
0132         u64_stats_add(&stats->tx_bytes, len);
0133         u64_stats_update_end(&stats->syncp);
0134     } else {
0135         this_cpu_inc(vlan->vlan_pcpu_stats->tx_dropped);
0136     }
0137 
0138     return ret;
0139 }
0140 
0141 static int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
0142 {
0143     struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
0144     unsigned int max_mtu = real_dev->mtu;
0145 
0146     if (netif_reduces_vlan_mtu(real_dev))
0147         max_mtu -= VLAN_HLEN;
0148     if (max_mtu < new_mtu)
0149         return -ERANGE;
0150 
0151     dev->mtu = new_mtu;
0152 
0153     return 0;
0154 }
0155 
0156 void vlan_dev_set_ingress_priority(const struct net_device *dev,
0157                    u32 skb_prio, u16 vlan_prio)
0158 {
0159     struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
0160 
0161     if (vlan->ingress_priority_map[vlan_prio & 0x7] && !skb_prio)
0162         vlan->nr_ingress_mappings--;
0163     else if (!vlan->ingress_priority_map[vlan_prio & 0x7] && skb_prio)
0164         vlan->nr_ingress_mappings++;
0165 
0166     vlan->ingress_priority_map[vlan_prio & 0x7] = skb_prio;
0167 }
0168 
0169 int vlan_dev_set_egress_priority(const struct net_device *dev,
0170                  u32 skb_prio, u16 vlan_prio)
0171 {
0172     struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
0173     struct vlan_priority_tci_mapping *mp = NULL;
0174     struct vlan_priority_tci_mapping *np;
0175     u32 vlan_qos = (vlan_prio << VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK;
0176 
0177     /* See if a priority mapping exists.. */
0178     mp = vlan->egress_priority_map[skb_prio & 0xF];
0179     while (mp) {
0180         if (mp->priority == skb_prio) {
0181             if (mp->vlan_qos && !vlan_qos)
0182                 vlan->nr_egress_mappings--;
0183             else if (!mp->vlan_qos && vlan_qos)
0184                 vlan->nr_egress_mappings++;
0185             mp->vlan_qos = vlan_qos;
0186             return 0;
0187         }
0188         mp = mp->next;
0189     }
0190 
0191     /* Create a new mapping then. */
0192     mp = vlan->egress_priority_map[skb_prio & 0xF];
0193     np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL);
0194     if (!np)
0195         return -ENOBUFS;
0196 
0197     np->next = mp;
0198     np->priority = skb_prio;
0199     np->vlan_qos = vlan_qos;
0200     /* Before inserting this element in hash table, make sure all its fields
0201      * are committed to memory.
0202      * coupled with smp_rmb() in vlan_dev_get_egress_qos_mask()
0203      */
0204     smp_wmb();
0205     vlan->egress_priority_map[skb_prio & 0xF] = np;
0206     if (vlan_qos)
0207         vlan->nr_egress_mappings++;
0208     return 0;
0209 }
0210 
0211 /* Flags are defined in the vlan_flags enum in
0212  * include/uapi/linux/if_vlan.h file.
0213  */
0214 int vlan_dev_change_flags(const struct net_device *dev, u32 flags, u32 mask)
0215 {
0216     struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
0217     u32 old_flags = vlan->flags;
0218 
0219     if (mask & ~(VLAN_FLAG_REORDER_HDR | VLAN_FLAG_GVRP |
0220              VLAN_FLAG_LOOSE_BINDING | VLAN_FLAG_MVRP |
0221              VLAN_FLAG_BRIDGE_BINDING))
0222         return -EINVAL;
0223 
0224     vlan->flags = (old_flags & ~mask) | (flags & mask);
0225 
0226     if (netif_running(dev) && (vlan->flags ^ old_flags) & VLAN_FLAG_GVRP) {
0227         if (vlan->flags & VLAN_FLAG_GVRP)
0228             vlan_gvrp_request_join(dev);
0229         else
0230             vlan_gvrp_request_leave(dev);
0231     }
0232 
0233     if (netif_running(dev) && (vlan->flags ^ old_flags) & VLAN_FLAG_MVRP) {
0234         if (vlan->flags & VLAN_FLAG_MVRP)
0235             vlan_mvrp_request_join(dev);
0236         else
0237             vlan_mvrp_request_leave(dev);
0238     }
0239     return 0;
0240 }
0241 
0242 void vlan_dev_get_realdev_name(const struct net_device *dev, char *result, size_t size)
0243 {
0244     strscpy_pad(result, vlan_dev_priv(dev)->real_dev->name, size);
0245 }
0246 
0247 bool vlan_dev_inherit_address(struct net_device *dev,
0248                   struct net_device *real_dev)
0249 {
0250     if (dev->addr_assign_type != NET_ADDR_STOLEN)
0251         return false;
0252 
0253     eth_hw_addr_set(dev, real_dev->dev_addr);
0254     call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
0255     return true;
0256 }
0257 
0258 static int vlan_dev_open(struct net_device *dev)
0259 {
0260     struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
0261     struct net_device *real_dev = vlan->real_dev;
0262     int err;
0263 
0264     if (!(real_dev->flags & IFF_UP) &&
0265         !(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
0266         return -ENETDOWN;
0267 
0268     if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr) &&
0269         !vlan_dev_inherit_address(dev, real_dev)) {
0270         err = dev_uc_add(real_dev, dev->dev_addr);
0271         if (err < 0)
0272             goto out;
0273     }
0274 
0275     if (dev->flags & IFF_ALLMULTI) {
0276         err = dev_set_allmulti(real_dev, 1);
0277         if (err < 0)
0278             goto del_unicast;
0279     }
0280     if (dev->flags & IFF_PROMISC) {
0281         err = dev_set_promiscuity(real_dev, 1);
0282         if (err < 0)
0283             goto clear_allmulti;
0284     }
0285 
0286     ether_addr_copy(vlan->real_dev_addr, real_dev->dev_addr);
0287 
0288     if (vlan->flags & VLAN_FLAG_GVRP)
0289         vlan_gvrp_request_join(dev);
0290 
0291     if (vlan->flags & VLAN_FLAG_MVRP)
0292         vlan_mvrp_request_join(dev);
0293 
0294     if (netif_carrier_ok(real_dev) &&
0295         !(vlan->flags & VLAN_FLAG_BRIDGE_BINDING))
0296         netif_carrier_on(dev);
0297     return 0;
0298 
0299 clear_allmulti:
0300     if (dev->flags & IFF_ALLMULTI)
0301         dev_set_allmulti(real_dev, -1);
0302 del_unicast:
0303     if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr))
0304         dev_uc_del(real_dev, dev->dev_addr);
0305 out:
0306     netif_carrier_off(dev);
0307     return err;
0308 }
0309 
0310 static int vlan_dev_stop(struct net_device *dev)
0311 {
0312     struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
0313     struct net_device *real_dev = vlan->real_dev;
0314 
0315     dev_mc_unsync(real_dev, dev);
0316     dev_uc_unsync(real_dev, dev);
0317     if (dev->flags & IFF_ALLMULTI)
0318         dev_set_allmulti(real_dev, -1);
0319     if (dev->flags & IFF_PROMISC)
0320         dev_set_promiscuity(real_dev, -1);
0321 
0322     if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr))
0323         dev_uc_del(real_dev, dev->dev_addr);
0324 
0325     if (!(vlan->flags & VLAN_FLAG_BRIDGE_BINDING))
0326         netif_carrier_off(dev);
0327     return 0;
0328 }
0329 
0330 static int vlan_dev_set_mac_address(struct net_device *dev, void *p)
0331 {
0332     struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
0333     struct sockaddr *addr = p;
0334     int err;
0335 
0336     if (!is_valid_ether_addr(addr->sa_data))
0337         return -EADDRNOTAVAIL;
0338 
0339     if (!(dev->flags & IFF_UP))
0340         goto out;
0341 
0342     if (!ether_addr_equal(addr->sa_data, real_dev->dev_addr)) {
0343         err = dev_uc_add(real_dev, addr->sa_data);
0344         if (err < 0)
0345             return err;
0346     }
0347 
0348     if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr))
0349         dev_uc_del(real_dev, dev->dev_addr);
0350 
0351 out:
0352     eth_hw_addr_set(dev, addr->sa_data);
0353     return 0;
0354 }
0355 
0356 static int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
0357 {
0358     struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
0359     const struct net_device_ops *ops = real_dev->netdev_ops;
0360     struct ifreq ifrr;
0361     int err = -EOPNOTSUPP;
0362 
0363     strscpy_pad(ifrr.ifr_name, real_dev->name, IFNAMSIZ);
0364     ifrr.ifr_ifru = ifr->ifr_ifru;
0365 
0366     switch (cmd) {
0367     case SIOCSHWTSTAMP:
0368         if (!net_eq(dev_net(dev), &init_net))
0369             break;
0370         fallthrough;
0371     case SIOCGMIIPHY:
0372     case SIOCGMIIREG:
0373     case SIOCSMIIREG:
0374     case SIOCGHWTSTAMP:
0375         if (netif_device_present(real_dev) && ops->ndo_eth_ioctl)
0376             err = ops->ndo_eth_ioctl(real_dev, &ifrr, cmd);
0377         break;
0378     }
0379 
0380     if (!err)
0381         ifr->ifr_ifru = ifrr.ifr_ifru;
0382 
0383     return err;
0384 }
0385 
0386 static int vlan_dev_neigh_setup(struct net_device *dev, struct neigh_parms *pa)
0387 {
0388     struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
0389     const struct net_device_ops *ops = real_dev->netdev_ops;
0390     int err = 0;
0391 
0392     if (netif_device_present(real_dev) && ops->ndo_neigh_setup)
0393         err = ops->ndo_neigh_setup(real_dev, pa);
0394 
0395     return err;
0396 }
0397 
0398 #if IS_ENABLED(CONFIG_FCOE)
0399 static int vlan_dev_fcoe_ddp_setup(struct net_device *dev, u16 xid,
0400                    struct scatterlist *sgl, unsigned int sgc)
0401 {
0402     struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
0403     const struct net_device_ops *ops = real_dev->netdev_ops;
0404     int rc = 0;
0405 
0406     if (ops->ndo_fcoe_ddp_setup)
0407         rc = ops->ndo_fcoe_ddp_setup(real_dev, xid, sgl, sgc);
0408 
0409     return rc;
0410 }
0411 
0412 static int vlan_dev_fcoe_ddp_done(struct net_device *dev, u16 xid)
0413 {
0414     struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
0415     const struct net_device_ops *ops = real_dev->netdev_ops;
0416     int len = 0;
0417 
0418     if (ops->ndo_fcoe_ddp_done)
0419         len = ops->ndo_fcoe_ddp_done(real_dev, xid);
0420 
0421     return len;
0422 }
0423 
0424 static int vlan_dev_fcoe_enable(struct net_device *dev)
0425 {
0426     struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
0427     const struct net_device_ops *ops = real_dev->netdev_ops;
0428     int rc = -EINVAL;
0429 
0430     if (ops->ndo_fcoe_enable)
0431         rc = ops->ndo_fcoe_enable(real_dev);
0432     return rc;
0433 }
0434 
0435 static int vlan_dev_fcoe_disable(struct net_device *dev)
0436 {
0437     struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
0438     const struct net_device_ops *ops = real_dev->netdev_ops;
0439     int rc = -EINVAL;
0440 
0441     if (ops->ndo_fcoe_disable)
0442         rc = ops->ndo_fcoe_disable(real_dev);
0443     return rc;
0444 }
0445 
0446 static int vlan_dev_fcoe_ddp_target(struct net_device *dev, u16 xid,
0447                     struct scatterlist *sgl, unsigned int sgc)
0448 {
0449     struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
0450     const struct net_device_ops *ops = real_dev->netdev_ops;
0451     int rc = 0;
0452 
0453     if (ops->ndo_fcoe_ddp_target)
0454         rc = ops->ndo_fcoe_ddp_target(real_dev, xid, sgl, sgc);
0455 
0456     return rc;
0457 }
0458 #endif
0459 
0460 #ifdef NETDEV_FCOE_WWNN
0461 static int vlan_dev_fcoe_get_wwn(struct net_device *dev, u64 *wwn, int type)
0462 {
0463     struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
0464     const struct net_device_ops *ops = real_dev->netdev_ops;
0465     int rc = -EINVAL;
0466 
0467     if (ops->ndo_fcoe_get_wwn)
0468         rc = ops->ndo_fcoe_get_wwn(real_dev, wwn, type);
0469     return rc;
0470 }
0471 #endif
0472 
0473 static void vlan_dev_change_rx_flags(struct net_device *dev, int change)
0474 {
0475     struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
0476 
0477     if (dev->flags & IFF_UP) {
0478         if (change & IFF_ALLMULTI)
0479             dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
0480         if (change & IFF_PROMISC)
0481             dev_set_promiscuity(real_dev, dev->flags & IFF_PROMISC ? 1 : -1);
0482     }
0483 }
0484 
0485 static void vlan_dev_set_rx_mode(struct net_device *vlan_dev)
0486 {
0487     dev_mc_sync(vlan_dev_priv(vlan_dev)->real_dev, vlan_dev);
0488     dev_uc_sync(vlan_dev_priv(vlan_dev)->real_dev, vlan_dev);
0489 }
0490 
0491 /*
0492  * vlan network devices have devices nesting below it, and are a special
0493  * "super class" of normal network devices; split their locks off into a
0494  * separate class since they always nest.
0495  */
0496 static struct lock_class_key vlan_netdev_xmit_lock_key;
0497 static struct lock_class_key vlan_netdev_addr_lock_key;
0498 
0499 static void vlan_dev_set_lockdep_one(struct net_device *dev,
0500                      struct netdev_queue *txq,
0501                      void *unused)
0502 {
0503     lockdep_set_class(&txq->_xmit_lock, &vlan_netdev_xmit_lock_key);
0504 }
0505 
0506 static void vlan_dev_set_lockdep_class(struct net_device *dev)
0507 {
0508     lockdep_set_class(&dev->addr_list_lock,
0509               &vlan_netdev_addr_lock_key);
0510     netdev_for_each_tx_queue(dev, vlan_dev_set_lockdep_one, NULL);
0511 }
0512 
0513 static __be16 vlan_parse_protocol(const struct sk_buff *skb)
0514 {
0515     struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
0516 
0517     return __vlan_get_protocol(skb, veth->h_vlan_proto, NULL);
0518 }
0519 
0520 static const struct header_ops vlan_header_ops = {
0521     .create  = vlan_dev_hard_header,
0522     .parse   = eth_header_parse,
0523     .parse_protocol = vlan_parse_protocol,
0524 };
0525 
0526 static int vlan_passthru_hard_header(struct sk_buff *skb, struct net_device *dev,
0527                      unsigned short type,
0528                      const void *daddr, const void *saddr,
0529                      unsigned int len)
0530 {
0531     struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
0532     struct net_device *real_dev = vlan->real_dev;
0533 
0534     if (saddr == NULL)
0535         saddr = dev->dev_addr;
0536 
0537     return dev_hard_header(skb, real_dev, type, daddr, saddr, len);
0538 }
0539 
0540 static const struct header_ops vlan_passthru_header_ops = {
0541     .create  = vlan_passthru_hard_header,
0542     .parse   = eth_header_parse,
0543     .parse_protocol = vlan_parse_protocol,
0544 };
0545 
0546 static struct device_type vlan_type = {
0547     .name   = "vlan",
0548 };
0549 
0550 static const struct net_device_ops vlan_netdev_ops;
0551 
0552 static int vlan_dev_init(struct net_device *dev)
0553 {
0554     struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
0555     struct net_device *real_dev = vlan->real_dev;
0556 
0557     netif_carrier_off(dev);
0558 
0559     /* IFF_BROADCAST|IFF_MULTICAST; ??? */
0560     dev->flags  = real_dev->flags & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
0561                       IFF_MASTER | IFF_SLAVE);
0562     dev->state  = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
0563                       (1<<__LINK_STATE_DORMANT))) |
0564               (1<<__LINK_STATE_PRESENT);
0565 
0566     if (vlan->flags & VLAN_FLAG_BRIDGE_BINDING)
0567         dev->state |= (1 << __LINK_STATE_NOCARRIER);
0568 
0569     dev->hw_features = NETIF_F_HW_CSUM | NETIF_F_SG |
0570                NETIF_F_FRAGLIST | NETIF_F_GSO_SOFTWARE |
0571                NETIF_F_GSO_ENCAP_ALL |
0572                NETIF_F_HIGHDMA | NETIF_F_SCTP_CRC |
0573                NETIF_F_ALL_FCOE;
0574 
0575     dev->features |= dev->hw_features | NETIF_F_LLTX;
0576     netif_inherit_tso_max(dev, real_dev);
0577     if (dev->features & NETIF_F_VLAN_FEATURES)
0578         netdev_warn(real_dev, "VLAN features are set incorrectly.  Q-in-Q configurations may not work correctly.\n");
0579 
0580     dev->vlan_features = real_dev->vlan_features & ~NETIF_F_ALL_FCOE;
0581     dev->hw_enc_features = vlan_tnl_features(real_dev);
0582     dev->mpls_features = real_dev->mpls_features;
0583 
0584     /* ipv6 shared card related stuff */
0585     dev->dev_id = real_dev->dev_id;
0586 
0587     if (is_zero_ether_addr(dev->dev_addr)) {
0588         eth_hw_addr_set(dev, real_dev->dev_addr);
0589         dev->addr_assign_type = NET_ADDR_STOLEN;
0590     }
0591     if (is_zero_ether_addr(dev->broadcast))
0592         memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
0593 
0594 #if IS_ENABLED(CONFIG_FCOE)
0595     dev->fcoe_ddp_xid = real_dev->fcoe_ddp_xid;
0596 #endif
0597 
0598     dev->needed_headroom = real_dev->needed_headroom;
0599     if (vlan_hw_offload_capable(real_dev->features, vlan->vlan_proto)) {
0600         dev->header_ops      = &vlan_passthru_header_ops;
0601         dev->hard_header_len = real_dev->hard_header_len;
0602     } else {
0603         dev->header_ops      = &vlan_header_ops;
0604         dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN;
0605     }
0606 
0607     dev->netdev_ops = &vlan_netdev_ops;
0608 
0609     SET_NETDEV_DEVTYPE(dev, &vlan_type);
0610 
0611     vlan_dev_set_lockdep_class(dev);
0612 
0613     vlan->vlan_pcpu_stats = netdev_alloc_pcpu_stats(struct vlan_pcpu_stats);
0614     if (!vlan->vlan_pcpu_stats)
0615         return -ENOMEM;
0616 
0617     /* Get vlan's reference to real_dev */
0618     netdev_hold(real_dev, &vlan->dev_tracker, GFP_KERNEL);
0619 
0620     return 0;
0621 }
0622 
0623 /* Note: this function might be called multiple times for the same device. */
0624 void vlan_dev_free_egress_priority(const struct net_device *dev)
0625 {
0626     struct vlan_priority_tci_mapping *pm;
0627     struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
0628     int i;
0629 
0630     for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) {
0631         while ((pm = vlan->egress_priority_map[i]) != NULL) {
0632             vlan->egress_priority_map[i] = pm->next;
0633             kfree(pm);
0634         }
0635     }
0636 }
0637 
0638 static void vlan_dev_uninit(struct net_device *dev)
0639 {
0640     vlan_dev_free_egress_priority(dev);
0641 }
0642 
0643 static netdev_features_t vlan_dev_fix_features(struct net_device *dev,
0644     netdev_features_t features)
0645 {
0646     struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
0647     netdev_features_t old_features = features;
0648     netdev_features_t lower_features;
0649 
0650     lower_features = netdev_intersect_features((real_dev->vlan_features |
0651                             NETIF_F_RXCSUM),
0652                            real_dev->features);
0653 
0654     /* Add HW_CSUM setting to preserve user ability to control
0655      * checksum offload on the vlan device.
0656      */
0657     if (lower_features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))
0658         lower_features |= NETIF_F_HW_CSUM;
0659     features = netdev_intersect_features(features, lower_features);
0660     features |= old_features & (NETIF_F_SOFT_FEATURES | NETIF_F_GSO_SOFTWARE);
0661     features |= NETIF_F_LLTX;
0662 
0663     return features;
0664 }
0665 
0666 static int vlan_ethtool_get_link_ksettings(struct net_device *dev,
0667                        struct ethtool_link_ksettings *cmd)
0668 {
0669     const struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
0670 
0671     return __ethtool_get_link_ksettings(vlan->real_dev, cmd);
0672 }
0673 
0674 static void vlan_ethtool_get_drvinfo(struct net_device *dev,
0675                      struct ethtool_drvinfo *info)
0676 {
0677     strlcpy(info->driver, vlan_fullname, sizeof(info->driver));
0678     strlcpy(info->version, vlan_version, sizeof(info->version));
0679     strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
0680 }
0681 
0682 static int vlan_ethtool_get_ts_info(struct net_device *dev,
0683                     struct ethtool_ts_info *info)
0684 {
0685     const struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
0686     const struct ethtool_ops *ops = vlan->real_dev->ethtool_ops;
0687     struct phy_device *phydev = vlan->real_dev->phydev;
0688 
0689     if (phy_has_tsinfo(phydev)) {
0690         return phy_ts_info(phydev, info);
0691     } else if (ops->get_ts_info) {
0692         return ops->get_ts_info(vlan->real_dev, info);
0693     } else {
0694         info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE |
0695             SOF_TIMESTAMPING_SOFTWARE;
0696         info->phc_index = -1;
0697     }
0698 
0699     return 0;
0700 }
0701 
0702 static void vlan_dev_get_stats64(struct net_device *dev,
0703                  struct rtnl_link_stats64 *stats)
0704 {
0705     struct vlan_pcpu_stats *p;
0706     u32 rx_errors = 0, tx_dropped = 0;
0707     int i;
0708 
0709     for_each_possible_cpu(i) {
0710         u64 rxpackets, rxbytes, rxmulticast, txpackets, txbytes;
0711         unsigned int start;
0712 
0713         p = per_cpu_ptr(vlan_dev_priv(dev)->vlan_pcpu_stats, i);
0714         do {
0715             start = u64_stats_fetch_begin_irq(&p->syncp);
0716             rxpackets   = u64_stats_read(&p->rx_packets);
0717             rxbytes     = u64_stats_read(&p->rx_bytes);
0718             rxmulticast = u64_stats_read(&p->rx_multicast);
0719             txpackets   = u64_stats_read(&p->tx_packets);
0720             txbytes     = u64_stats_read(&p->tx_bytes);
0721         } while (u64_stats_fetch_retry_irq(&p->syncp, start));
0722 
0723         stats->rx_packets   += rxpackets;
0724         stats->rx_bytes     += rxbytes;
0725         stats->multicast    += rxmulticast;
0726         stats->tx_packets   += txpackets;
0727         stats->tx_bytes     += txbytes;
0728         /* rx_errors & tx_dropped are u32 */
0729         rx_errors   += READ_ONCE(p->rx_errors);
0730         tx_dropped  += READ_ONCE(p->tx_dropped);
0731     }
0732     stats->rx_errors  = rx_errors;
0733     stats->tx_dropped = tx_dropped;
0734 }
0735 
0736 #ifdef CONFIG_NET_POLL_CONTROLLER
0737 static void vlan_dev_poll_controller(struct net_device *dev)
0738 {
0739     return;
0740 }
0741 
0742 static int vlan_dev_netpoll_setup(struct net_device *dev, struct netpoll_info *npinfo)
0743 {
0744     struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
0745     struct net_device *real_dev = vlan->real_dev;
0746     struct netpoll *netpoll;
0747     int err = 0;
0748 
0749     netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
0750     err = -ENOMEM;
0751     if (!netpoll)
0752         goto out;
0753 
0754     err = __netpoll_setup(netpoll, real_dev);
0755     if (err) {
0756         kfree(netpoll);
0757         goto out;
0758     }
0759 
0760     vlan->netpoll = netpoll;
0761 
0762 out:
0763     return err;
0764 }
0765 
0766 static void vlan_dev_netpoll_cleanup(struct net_device *dev)
0767 {
0768     struct vlan_dev_priv *vlan= vlan_dev_priv(dev);
0769     struct netpoll *netpoll = vlan->netpoll;
0770 
0771     if (!netpoll)
0772         return;
0773 
0774     vlan->netpoll = NULL;
0775     __netpoll_free(netpoll);
0776 }
0777 #endif /* CONFIG_NET_POLL_CONTROLLER */
0778 
0779 static int vlan_dev_get_iflink(const struct net_device *dev)
0780 {
0781     struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
0782 
0783     return real_dev->ifindex;
0784 }
0785 
0786 static int vlan_dev_fill_forward_path(struct net_device_path_ctx *ctx,
0787                       struct net_device_path *path)
0788 {
0789     struct vlan_dev_priv *vlan = vlan_dev_priv(ctx->dev);
0790 
0791     path->type = DEV_PATH_VLAN;
0792     path->encap.id = vlan->vlan_id;
0793     path->encap.proto = vlan->vlan_proto;
0794     path->dev = ctx->dev;
0795     ctx->dev = vlan->real_dev;
0796     if (ctx->num_vlans >= ARRAY_SIZE(ctx->vlan))
0797         return -ENOSPC;
0798 
0799     ctx->vlan[ctx->num_vlans].id = vlan->vlan_id;
0800     ctx->vlan[ctx->num_vlans].proto = vlan->vlan_proto;
0801     ctx->num_vlans++;
0802 
0803     return 0;
0804 }
0805 
0806 static const struct ethtool_ops vlan_ethtool_ops = {
0807     .get_link_ksettings = vlan_ethtool_get_link_ksettings,
0808     .get_drvinfo            = vlan_ethtool_get_drvinfo,
0809     .get_link       = ethtool_op_get_link,
0810     .get_ts_info        = vlan_ethtool_get_ts_info,
0811 };
0812 
0813 static const struct net_device_ops vlan_netdev_ops = {
0814     .ndo_change_mtu     = vlan_dev_change_mtu,
0815     .ndo_init       = vlan_dev_init,
0816     .ndo_uninit     = vlan_dev_uninit,
0817     .ndo_open       = vlan_dev_open,
0818     .ndo_stop       = vlan_dev_stop,
0819     .ndo_start_xmit =  vlan_dev_hard_start_xmit,
0820     .ndo_validate_addr  = eth_validate_addr,
0821     .ndo_set_mac_address    = vlan_dev_set_mac_address,
0822     .ndo_set_rx_mode    = vlan_dev_set_rx_mode,
0823     .ndo_change_rx_flags    = vlan_dev_change_rx_flags,
0824     .ndo_eth_ioctl      = vlan_dev_ioctl,
0825     .ndo_neigh_setup    = vlan_dev_neigh_setup,
0826     .ndo_get_stats64    = vlan_dev_get_stats64,
0827 #if IS_ENABLED(CONFIG_FCOE)
0828     .ndo_fcoe_ddp_setup = vlan_dev_fcoe_ddp_setup,
0829     .ndo_fcoe_ddp_done  = vlan_dev_fcoe_ddp_done,
0830     .ndo_fcoe_enable    = vlan_dev_fcoe_enable,
0831     .ndo_fcoe_disable   = vlan_dev_fcoe_disable,
0832     .ndo_fcoe_ddp_target    = vlan_dev_fcoe_ddp_target,
0833 #endif
0834 #ifdef NETDEV_FCOE_WWNN
0835     .ndo_fcoe_get_wwn   = vlan_dev_fcoe_get_wwn,
0836 #endif
0837 #ifdef CONFIG_NET_POLL_CONTROLLER
0838     .ndo_poll_controller    = vlan_dev_poll_controller,
0839     .ndo_netpoll_setup  = vlan_dev_netpoll_setup,
0840     .ndo_netpoll_cleanup    = vlan_dev_netpoll_cleanup,
0841 #endif
0842     .ndo_fix_features   = vlan_dev_fix_features,
0843     .ndo_get_iflink     = vlan_dev_get_iflink,
0844     .ndo_fill_forward_path  = vlan_dev_fill_forward_path,
0845 };
0846 
0847 static void vlan_dev_free(struct net_device *dev)
0848 {
0849     struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
0850 
0851     free_percpu(vlan->vlan_pcpu_stats);
0852     vlan->vlan_pcpu_stats = NULL;
0853 
0854     /* Get rid of the vlan's reference to real_dev */
0855     netdev_put(vlan->real_dev, &vlan->dev_tracker);
0856 }
0857 
0858 void vlan_setup(struct net_device *dev)
0859 {
0860     ether_setup(dev);
0861 
0862     dev->priv_flags     |= IFF_802_1Q_VLAN | IFF_NO_QUEUE;
0863     dev->priv_flags     |= IFF_UNICAST_FLT;
0864     dev->priv_flags     &= ~IFF_TX_SKB_SHARING;
0865     netif_keep_dst(dev);
0866 
0867     dev->netdev_ops     = &vlan_netdev_ops;
0868     dev->needs_free_netdev  = true;
0869     dev->priv_destructor    = vlan_dev_free;
0870     dev->ethtool_ops    = &vlan_ethtool_ops;
0871 
0872     dev->min_mtu        = 0;
0873     dev->max_mtu        = ETH_MAX_MTU;
0874 
0875     eth_zero_addr(dev->broadcast);
0876 }