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