linux/drivers/net/macvlan.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2007 Patrick McHardy <kaber@trash.net>
   3 *
   4 * This program is free software; you can redistribute it and/or
   5 * modify it under the terms of the GNU General Public License as
   6 * published by the Free Software Foundation; either version 2 of
   7 * the License, or (at your option) any later version.
   8 *
   9 * The code this is based on carried the following copyright notice:
  10 * ---
  11 * (C) Copyright 2001-2006
  12 * Alex Zeffertt, Cambridge Broadband Ltd, ajz@cambridgebroadband.com
  13 * Re-worked by Ben Greear <greearb@candelatech.com>
  14 * ---
  15 */
  16#include <linux/kernel.h>
  17#include <linux/types.h>
  18#include <linux/module.h>
  19#include <linux/init.h>
  20#include <linux/errno.h>
  21#include <linux/slab.h>
  22#include <linux/string.h>
  23#include <linux/rculist.h>
  24#include <linux/notifier.h>
  25#include <linux/netdevice.h>
  26#include <linux/etherdevice.h>
  27#include <linux/ethtool.h>
  28#include <linux/if_arp.h>
  29#include <linux/if_vlan.h>
  30#include <linux/if_link.h>
  31#include <linux/if_macvlan.h>
  32#include <linux/hash.h>
  33#include <linux/workqueue.h>
  34#include <net/rtnetlink.h>
  35#include <net/xfrm.h>
  36
  37#define MACVLAN_HASH_SIZE       (1 << BITS_PER_BYTE)
  38#define MACVLAN_BC_QUEUE_LEN    1000
  39
  40struct macvlan_port {
  41        struct net_device       *dev;
  42        struct hlist_head       vlan_hash[MACVLAN_HASH_SIZE];
  43        struct list_head        vlans;
  44        struct rcu_head         rcu;
  45        struct sk_buff_head     bc_queue;
  46        struct work_struct      bc_work;
  47        bool                    passthru;
  48        int                     count;
  49};
  50
  51struct macvlan_skb_cb {
  52        const struct macvlan_dev *src;
  53};
  54
  55#define MACVLAN_SKB_CB(__skb) ((struct macvlan_skb_cb *)&((__skb)->cb[0]))
  56
  57static void macvlan_port_destroy(struct net_device *dev);
  58
  59static struct macvlan_port *macvlan_port_get_rcu(const struct net_device *dev)
  60{
  61        return rcu_dereference(dev->rx_handler_data);
  62}
  63
  64static struct macvlan_port *macvlan_port_get_rtnl(const struct net_device *dev)
  65{
  66        return rtnl_dereference(dev->rx_handler_data);
  67}
  68
  69#define macvlan_port_exists(dev) (dev->priv_flags & IFF_MACVLAN_PORT)
  70
  71static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
  72                                               const unsigned char *addr)
  73{
  74        struct macvlan_dev *vlan;
  75
  76        hlist_for_each_entry_rcu(vlan, &port->vlan_hash[addr[5]], hlist) {
  77                if (ether_addr_equal_64bits(vlan->dev->dev_addr, addr))
  78                        return vlan;
  79        }
  80        return NULL;
  81}
  82
  83static void macvlan_hash_add(struct macvlan_dev *vlan)
  84{
  85        struct macvlan_port *port = vlan->port;
  86        const unsigned char *addr = vlan->dev->dev_addr;
  87
  88        hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]);
  89}
  90
  91static void macvlan_hash_del(struct macvlan_dev *vlan, bool sync)
  92{
  93        hlist_del_rcu(&vlan->hlist);
  94        if (sync)
  95                synchronize_rcu();
  96}
  97
  98static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
  99                                        const unsigned char *addr)
 100{
 101        macvlan_hash_del(vlan, true);
 102        /* Now that we are unhashed it is safe to change the device
 103         * address without confusing packet delivery.
 104         */
 105        memcpy(vlan->dev->dev_addr, addr, ETH_ALEN);
 106        macvlan_hash_add(vlan);
 107}
 108
 109static int macvlan_addr_busy(const struct macvlan_port *port,
 110                                const unsigned char *addr)
 111{
 112        /* Test to see if the specified multicast address is
 113         * currently in use by the underlying device or
 114         * another macvlan.
 115         */
 116        if (ether_addr_equal_64bits(port->dev->dev_addr, addr))
 117                return 1;
 118
 119        if (macvlan_hash_lookup(port, addr))
 120                return 1;
 121
 122        return 0;
 123}
 124
 125
 126static int macvlan_broadcast_one(struct sk_buff *skb,
 127                                 const struct macvlan_dev *vlan,
 128                                 const struct ethhdr *eth, bool local)
 129{
 130        struct net_device *dev = vlan->dev;
 131        if (!skb)
 132                return NET_RX_DROP;
 133
 134        if (local)
 135                return __dev_forward_skb(dev, skb);
 136
 137        skb->dev = dev;
 138        if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast))
 139                skb->pkt_type = PACKET_BROADCAST;
 140        else
 141                skb->pkt_type = PACKET_MULTICAST;
 142
 143        return 0;
 144}
 145
 146static u32 macvlan_hash_mix(const struct macvlan_dev *vlan)
 147{
 148        return (u32)(((unsigned long)vlan) >> L1_CACHE_SHIFT);
 149}
 150
 151
 152static unsigned int mc_hash(const struct macvlan_dev *vlan,
 153                            const unsigned char *addr)
 154{
 155        u32 val = __get_unaligned_cpu32(addr + 2);
 156
 157        val ^= macvlan_hash_mix(vlan);
 158        return hash_32(val, MACVLAN_MC_FILTER_BITS);
 159}
 160
 161static void macvlan_broadcast(struct sk_buff *skb,
 162                              const struct macvlan_port *port,
 163                              struct net_device *src,
 164                              enum macvlan_mode mode)
 165{
 166        const struct ethhdr *eth = eth_hdr(skb);
 167        const struct macvlan_dev *vlan;
 168        struct sk_buff *nskb;
 169        unsigned int i;
 170        int err;
 171        unsigned int hash;
 172
 173        if (skb->protocol == htons(ETH_P_PAUSE))
 174                return;
 175
 176        for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
 177                hlist_for_each_entry_rcu(vlan, &port->vlan_hash[i], hlist) {
 178                        if (vlan->dev == src || !(vlan->mode & mode))
 179                                continue;
 180
 181                        hash = mc_hash(vlan, eth->h_dest);
 182                        if (!test_bit(hash, vlan->mc_filter))
 183                                continue;
 184                        nskb = skb_clone(skb, GFP_ATOMIC);
 185                        err = macvlan_broadcast_one(
 186                                nskb, vlan, eth,
 187                                mode == MACVLAN_MODE_BRIDGE) ?:
 188                              netif_rx_ni(nskb);
 189                        macvlan_count_rx(vlan, skb->len + ETH_HLEN,
 190                                         err == NET_RX_SUCCESS, true);
 191                }
 192        }
 193}
 194
 195static void macvlan_process_broadcast(struct work_struct *w)
 196{
 197        struct macvlan_port *port = container_of(w, struct macvlan_port,
 198                                                 bc_work);
 199        struct sk_buff *skb;
 200        struct sk_buff_head list;
 201
 202        __skb_queue_head_init(&list);
 203
 204        spin_lock_bh(&port->bc_queue.lock);
 205        skb_queue_splice_tail_init(&port->bc_queue, &list);
 206        spin_unlock_bh(&port->bc_queue.lock);
 207
 208        while ((skb = __skb_dequeue(&list))) {
 209                const struct macvlan_dev *src = MACVLAN_SKB_CB(skb)->src;
 210
 211                rcu_read_lock();
 212
 213                if (!src)
 214                        /* frame comes from an external address */
 215                        macvlan_broadcast(skb, port, NULL,
 216                                          MACVLAN_MODE_PRIVATE |
 217                                          MACVLAN_MODE_VEPA    |
 218                                          MACVLAN_MODE_PASSTHRU|
 219                                          MACVLAN_MODE_BRIDGE);
 220                else if (src->mode == MACVLAN_MODE_VEPA)
 221                        /* flood to everyone except source */
 222                        macvlan_broadcast(skb, port, src->dev,
 223                                          MACVLAN_MODE_VEPA |
 224                                          MACVLAN_MODE_BRIDGE);
 225                else
 226                        /*
 227                         * flood only to VEPA ports, bridge ports
 228                         * already saw the frame on the way out.
 229                         */
 230                        macvlan_broadcast(skb, port, src->dev,
 231                                          MACVLAN_MODE_VEPA);
 232
 233                rcu_read_unlock();
 234
 235                kfree_skb(skb);
 236        }
 237}
 238
 239static void macvlan_broadcast_enqueue(struct macvlan_port *port,
 240                                      struct sk_buff *skb)
 241{
 242        struct sk_buff *nskb;
 243        int err = -ENOMEM;
 244
 245        nskb = skb_clone(skb, GFP_ATOMIC);
 246        if (!nskb)
 247                goto err;
 248
 249        spin_lock(&port->bc_queue.lock);
 250        if (skb_queue_len(&port->bc_queue) < MACVLAN_BC_QUEUE_LEN) {
 251                __skb_queue_tail(&port->bc_queue, nskb);
 252                err = 0;
 253        }
 254        spin_unlock(&port->bc_queue.lock);
 255
 256        if (err)
 257                goto free_nskb;
 258
 259        schedule_work(&port->bc_work);
 260        return;
 261
 262free_nskb:
 263        kfree_skb(nskb);
 264err:
 265        atomic_long_inc(&skb->dev->rx_dropped);
 266}
 267
 268/* called under rcu_read_lock() from netif_receive_skb */
 269static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb)
 270{
 271        struct macvlan_port *port;
 272        struct sk_buff *skb = *pskb;
 273        const struct ethhdr *eth = eth_hdr(skb);
 274        const struct macvlan_dev *vlan;
 275        const struct macvlan_dev *src;
 276        struct net_device *dev;
 277        unsigned int len = 0;
 278        int ret;
 279        rx_handler_result_t handle_res;
 280
 281        port = macvlan_port_get_rcu(skb->dev);
 282        if (is_multicast_ether_addr(eth->h_dest)) {
 283                skb = ip_check_defrag(skb, IP_DEFRAG_MACVLAN);
 284                if (!skb)
 285                        return RX_HANDLER_CONSUMED;
 286                *pskb = skb;
 287                eth = eth_hdr(skb);
 288                src = macvlan_hash_lookup(port, eth->h_source);
 289                if (src && src->mode != MACVLAN_MODE_VEPA &&
 290                    src->mode != MACVLAN_MODE_BRIDGE) {
 291                        /* forward to original port. */
 292                        vlan = src;
 293                        ret = macvlan_broadcast_one(skb, vlan, eth, 0) ?:
 294                              netif_rx(skb);
 295                        handle_res = RX_HANDLER_CONSUMED;
 296                        goto out;
 297                }
 298
 299                MACVLAN_SKB_CB(skb)->src = src;
 300                macvlan_broadcast_enqueue(port, skb);
 301
 302                return RX_HANDLER_PASS;
 303        }
 304
 305        if (port->passthru)
 306                vlan = list_first_or_null_rcu(&port->vlans,
 307                                              struct macvlan_dev, list);
 308        else
 309                vlan = macvlan_hash_lookup(port, eth->h_dest);
 310        if (vlan == NULL)
 311                return RX_HANDLER_PASS;
 312
 313        dev = vlan->dev;
 314        if (unlikely(!(dev->flags & IFF_UP))) {
 315                kfree_skb(skb);
 316                return RX_HANDLER_CONSUMED;
 317        }
 318        len = skb->len + ETH_HLEN;
 319        skb = skb_share_check(skb, GFP_ATOMIC);
 320        if (!skb) {
 321                ret = NET_RX_DROP;
 322                handle_res = RX_HANDLER_CONSUMED;
 323                goto out;
 324        }
 325
 326        *pskb = skb;
 327        skb->dev = dev;
 328        skb->pkt_type = PACKET_HOST;
 329
 330        ret = NET_RX_SUCCESS;
 331        handle_res = RX_HANDLER_ANOTHER;
 332out:
 333        macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, false);
 334        return handle_res;
 335}
 336
 337static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
 338{
 339        const struct macvlan_dev *vlan = netdev_priv(dev);
 340        const struct macvlan_port *port = vlan->port;
 341        const struct macvlan_dev *dest;
 342
 343        if (vlan->mode == MACVLAN_MODE_BRIDGE) {
 344                const struct ethhdr *eth = (void *)skb->data;
 345
 346                /* send to other bridge ports directly */
 347                if (is_multicast_ether_addr(eth->h_dest)) {
 348                        macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE);
 349                        goto xmit_world;
 350                }
 351
 352                dest = macvlan_hash_lookup(port, eth->h_dest);
 353                if (dest && dest->mode == MACVLAN_MODE_BRIDGE) {
 354                        /* send to lowerdev first for its network taps */
 355                        dev_forward_skb(vlan->lowerdev, skb);
 356
 357                        return NET_XMIT_SUCCESS;
 358                }
 359        }
 360
 361xmit_world:
 362        skb->dev = vlan->lowerdev;
 363        return dev_queue_xmit(skb);
 364}
 365
 366netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
 367                               struct net_device *dev)
 368{
 369        unsigned int len = skb->len;
 370        int ret;
 371        const struct macvlan_dev *vlan = netdev_priv(dev);
 372
 373        if (vlan->fwd_priv) {
 374                skb->dev = vlan->lowerdev;
 375                ret = dev_queue_xmit_accel(skb, vlan->fwd_priv);
 376        } else {
 377                ret = macvlan_queue_xmit(skb, dev);
 378        }
 379
 380        if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
 381                struct macvlan_pcpu_stats *pcpu_stats;
 382
 383                pcpu_stats = this_cpu_ptr(vlan->pcpu_stats);
 384                u64_stats_update_begin(&pcpu_stats->syncp);
 385                pcpu_stats->tx_packets++;
 386                pcpu_stats->tx_bytes += len;
 387                u64_stats_update_end(&pcpu_stats->syncp);
 388        } else {
 389                this_cpu_inc(vlan->pcpu_stats->tx_dropped);
 390        }
 391        return ret;
 392}
 393EXPORT_SYMBOL_GPL(macvlan_start_xmit);
 394
 395static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
 396                               unsigned short type, const void *daddr,
 397                               const void *saddr, unsigned len)
 398{
 399        const struct macvlan_dev *vlan = netdev_priv(dev);
 400        struct net_device *lowerdev = vlan->lowerdev;
 401
 402        return dev_hard_header(skb, lowerdev, type, daddr,
 403                               saddr ? : dev->dev_addr, len);
 404}
 405
 406static const struct header_ops macvlan_hard_header_ops = {
 407        .create         = macvlan_hard_header,
 408        .rebuild        = eth_rebuild_header,
 409        .parse          = eth_header_parse,
 410        .cache          = eth_header_cache,
 411        .cache_update   = eth_header_cache_update,
 412};
 413
 414static int macvlan_open(struct net_device *dev)
 415{
 416        struct macvlan_dev *vlan = netdev_priv(dev);
 417        struct net_device *lowerdev = vlan->lowerdev;
 418        int err;
 419
 420        if (vlan->port->passthru) {
 421                if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC))
 422                        dev_set_promiscuity(lowerdev, 1);
 423                goto hash_add;
 424        }
 425
 426        if (lowerdev->features & NETIF_F_HW_L2FW_DOFFLOAD) {
 427                vlan->fwd_priv =
 428                      get_ndo_ext(lowerdev->netdev_ops, ndo_dfwd_add_station)(lowerdev, dev);
 429
 430                /* If we get a NULL pointer back, or if we get an error
 431                 * then we should just fall through to the non accelerated path
 432                 */
 433                if (IS_ERR_OR_NULL(vlan->fwd_priv)) {
 434                        vlan->fwd_priv = NULL;
 435                } else
 436                        return 0;
 437        }
 438
 439        err = -EBUSY;
 440        if (macvlan_addr_busy(vlan->port, dev->dev_addr))
 441                goto out;
 442
 443        err = dev_uc_add(lowerdev, dev->dev_addr);
 444        if (err < 0)
 445                goto out;
 446        if (dev->flags & IFF_ALLMULTI) {
 447                err = dev_set_allmulti(lowerdev, 1);
 448                if (err < 0)
 449                        goto del_unicast;
 450        }
 451
 452hash_add:
 453        macvlan_hash_add(vlan);
 454        return 0;
 455
 456del_unicast:
 457        dev_uc_del(lowerdev, dev->dev_addr);
 458out:
 459        if (vlan->fwd_priv) {
 460                get_ndo_ext(lowerdev->netdev_ops, ndo_dfwd_del_station)(lowerdev,
 461                                                                        vlan->fwd_priv);
 462                vlan->fwd_priv = NULL;
 463        }
 464        return err;
 465}
 466
 467static int macvlan_stop(struct net_device *dev)
 468{
 469        struct macvlan_dev *vlan = netdev_priv(dev);
 470        struct net_device *lowerdev = vlan->lowerdev;
 471
 472        if (vlan->fwd_priv) {
 473                get_ndo_ext(lowerdev->netdev_ops, ndo_dfwd_del_station)(lowerdev,
 474                                                                        vlan->fwd_priv);
 475                vlan->fwd_priv = NULL;
 476                return 0;
 477        }
 478
 479        dev_uc_unsync(lowerdev, dev);
 480        dev_mc_unsync(lowerdev, dev);
 481
 482        if (vlan->port->passthru) {
 483                if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC))
 484                        dev_set_promiscuity(lowerdev, -1);
 485                goto hash_del;
 486        }
 487
 488        if (dev->flags & IFF_ALLMULTI)
 489                dev_set_allmulti(lowerdev, -1);
 490
 491        dev_uc_del(lowerdev, dev->dev_addr);
 492
 493hash_del:
 494        macvlan_hash_del(vlan, !dev->dismantle);
 495        return 0;
 496}
 497
 498static int macvlan_set_mac_address(struct net_device *dev, void *p)
 499{
 500        struct macvlan_dev *vlan = netdev_priv(dev);
 501        struct net_device *lowerdev = vlan->lowerdev;
 502        struct sockaddr *addr = p;
 503        int err;
 504
 505        if (!is_valid_ether_addr(addr->sa_data))
 506                return -EADDRNOTAVAIL;
 507
 508        if (!(dev->flags & IFF_UP)) {
 509                /* Just copy in the new address */
 510                memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
 511        } else {
 512                /* Rehash and update the device filters */
 513                if (macvlan_addr_busy(vlan->port, addr->sa_data))
 514                        return -EBUSY;
 515
 516                err = dev_uc_add(lowerdev, addr->sa_data);
 517                if (err)
 518                        return err;
 519
 520                dev_uc_del(lowerdev, dev->dev_addr);
 521
 522                macvlan_hash_change_addr(vlan, addr->sa_data);
 523        }
 524        return 0;
 525}
 526
 527static void macvlan_change_rx_flags(struct net_device *dev, int change)
 528{
 529        struct macvlan_dev *vlan = netdev_priv(dev);
 530        struct net_device *lowerdev = vlan->lowerdev;
 531
 532        if (change & IFF_ALLMULTI)
 533                dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
 534}
 535
 536static void macvlan_set_mac_lists(struct net_device *dev)
 537{
 538        struct macvlan_dev *vlan = netdev_priv(dev);
 539
 540        if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
 541                bitmap_fill(vlan->mc_filter, MACVLAN_MC_FILTER_SZ);
 542        } else {
 543                struct netdev_hw_addr *ha;
 544                DECLARE_BITMAP(filter, MACVLAN_MC_FILTER_SZ);
 545
 546                bitmap_zero(filter, MACVLAN_MC_FILTER_SZ);
 547                netdev_for_each_mc_addr(ha, dev) {
 548                        __set_bit(mc_hash(vlan, ha->addr), filter);
 549                }
 550
 551                __set_bit(mc_hash(vlan, dev->broadcast), filter);
 552
 553                bitmap_copy(vlan->mc_filter, filter, MACVLAN_MC_FILTER_SZ);
 554        }
 555        dev_uc_sync(vlan->lowerdev, dev);
 556        dev_mc_sync(vlan->lowerdev, dev);
 557}
 558
 559static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
 560{
 561        struct macvlan_dev *vlan = netdev_priv(dev);
 562
 563        if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
 564                return -EINVAL;
 565        dev->mtu = new_mtu;
 566        return 0;
 567}
 568
 569/*
 570 * macvlan network devices have devices nesting below it and are a special
 571 * "super class" of normal network devices; split their locks off into a
 572 * separate class since they always nest.
 573 */
 574static struct lock_class_key macvlan_netdev_xmit_lock_key;
 575static struct lock_class_key macvlan_netdev_addr_lock_key;
 576
 577#define ALWAYS_ON_OFFLOADS \
 578        (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE)
 579
 580#define ALWAYS_ON_FEATURES (ALWAYS_ON_OFFLOADS | NETIF_F_LLTX)
 581
 582#define MACVLAN_FEATURES \
 583        (NETIF_F_SG | NETIF_F_CSUM_MASK | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
 584         NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_LRO | NETIF_F_GSO_ROBUST | \
 585         NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
 586         NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
 587
 588#define MACVLAN_STATE_MASK \
 589        ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
 590
 591static void macvlan_set_lockdep_class_one(struct net_device *dev,
 592                                          struct netdev_queue *txq,
 593                                          void *_unused)
 594{
 595        lockdep_set_class(&txq->_xmit_lock,
 596                          &macvlan_netdev_xmit_lock_key);
 597}
 598
 599static void macvlan_set_lockdep_class(struct net_device *dev)
 600{
 601        lockdep_set_class(&dev->addr_list_lock,
 602                          &macvlan_netdev_addr_lock_key);
 603        netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL);
 604}
 605
 606static int macvlan_init(struct net_device *dev)
 607{
 608        struct macvlan_dev *vlan = netdev_priv(dev);
 609        const struct net_device *lowerdev = vlan->lowerdev;
 610
 611        dev->state              = (dev->state & ~MACVLAN_STATE_MASK) |
 612                                  (lowerdev->state & MACVLAN_STATE_MASK);
 613        dev->features           = lowerdev->features & MACVLAN_FEATURES;
 614        dev->features           |= ALWAYS_ON_FEATURES;
 615        dev->hw_features        |= NETIF_F_LRO;
 616        dev->vlan_features      = lowerdev->vlan_features & MACVLAN_FEATURES;
 617        dev->vlan_features      |= ALWAYS_ON_OFFLOADS;
 618        dev->gso_max_size       = lowerdev->gso_max_size;
 619        dev->hard_header_len    = lowerdev->hard_header_len;
 620
 621        macvlan_set_lockdep_class(dev);
 622
 623        vlan->pcpu_stats = netdev_alloc_pcpu_stats(struct macvlan_pcpu_stats);
 624        if (!vlan->pcpu_stats)
 625                return -ENOMEM;
 626
 627        return 0;
 628}
 629
 630static void macvlan_uninit(struct net_device *dev)
 631{
 632        struct macvlan_dev *vlan = netdev_priv(dev);
 633        struct macvlan_port *port = vlan->port;
 634
 635        free_percpu(vlan->pcpu_stats);
 636
 637        port->count -= 1;
 638        if (!port->count)
 639                macvlan_port_destroy(port->dev);
 640}
 641
 642static void macvlan_dev_get_stats64(struct net_device *dev,
 643                                    struct rtnl_link_stats64 *stats)
 644{
 645        struct macvlan_dev *vlan = netdev_priv(dev);
 646
 647        if (vlan->pcpu_stats) {
 648                struct macvlan_pcpu_stats *p;
 649                u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
 650                u32 rx_errors = 0, tx_dropped = 0;
 651                unsigned int start;
 652                int i;
 653
 654                for_each_possible_cpu(i) {
 655                        p = per_cpu_ptr(vlan->pcpu_stats, i);
 656                        do {
 657                                start = u64_stats_fetch_begin_irq(&p->syncp);
 658                                rx_packets      = p->rx_packets;
 659                                rx_bytes        = p->rx_bytes;
 660                                rx_multicast    = p->rx_multicast;
 661                                tx_packets      = p->tx_packets;
 662                                tx_bytes        = p->tx_bytes;
 663                        } while (u64_stats_fetch_retry_irq(&p->syncp, start));
 664
 665                        stats->rx_packets       += rx_packets;
 666                        stats->rx_bytes         += rx_bytes;
 667                        stats->multicast        += rx_multicast;
 668                        stats->tx_packets       += tx_packets;
 669                        stats->tx_bytes         += tx_bytes;
 670                        /* rx_errors & tx_dropped are u32, updated
 671                         * without syncp protection.
 672                         */
 673                        rx_errors       += p->rx_errors;
 674                        tx_dropped      += p->tx_dropped;
 675                }
 676                stats->rx_errors        = rx_errors;
 677                stats->rx_dropped       = rx_errors;
 678                stats->tx_dropped       = tx_dropped;
 679        }
 680}
 681
 682static int macvlan_vlan_rx_add_vid(struct net_device *dev,
 683                                   __be16 proto, u16 vid)
 684{
 685        struct macvlan_dev *vlan = netdev_priv(dev);
 686        struct net_device *lowerdev = vlan->lowerdev;
 687
 688        return vlan_vid_add(lowerdev, proto, vid);
 689}
 690
 691static int macvlan_vlan_rx_kill_vid(struct net_device *dev,
 692                                    __be16 proto, u16 vid)
 693{
 694        struct macvlan_dev *vlan = netdev_priv(dev);
 695        struct net_device *lowerdev = vlan->lowerdev;
 696
 697        vlan_vid_del(lowerdev, proto, vid);
 698        return 0;
 699}
 700
 701static int macvlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
 702                           struct net_device *dev,
 703                           const unsigned char *addr, u16 vid,
 704                           u16 flags)
 705{
 706        struct macvlan_dev *vlan = netdev_priv(dev);
 707        int err = -EINVAL;
 708
 709        /* Support unicast filter only on passthru devices.
 710         * Multicast filter should be allowed on all devices.
 711         */
 712        if (!vlan->port->passthru && is_unicast_ether_addr(addr))
 713                return -EOPNOTSUPP;
 714
 715        if (is_unicast_ether_addr(addr))
 716                err = dev_uc_add_excl(dev, addr);
 717        else if (is_multicast_ether_addr(addr))
 718                err = dev_mc_add_excl(dev, addr);
 719
 720        return err;
 721}
 722
 723static int macvlan_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
 724                           struct net_device *dev,
 725                           const unsigned char *addr, u16 vid)
 726{
 727        struct macvlan_dev *vlan = netdev_priv(dev);
 728        int err = -EINVAL;
 729
 730        /* Support unicast filter only on passthru devices.
 731         * Multicast filter should be allowed on all devices.
 732         */
 733        if (!vlan->port->passthru && is_unicast_ether_addr(addr))
 734                return -EOPNOTSUPP;
 735
 736        if (is_unicast_ether_addr(addr))
 737                err = dev_uc_del(dev, addr);
 738        else if (is_multicast_ether_addr(addr))
 739                err = dev_mc_del(dev, addr);
 740
 741        return err;
 742}
 743
 744static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
 745                                        struct ethtool_drvinfo *drvinfo)
 746{
 747        strlcpy(drvinfo->driver, "macvlan", sizeof(drvinfo->driver));
 748        strlcpy(drvinfo->version, "0.1", sizeof(drvinfo->version));
 749}
 750
 751static int macvlan_ethtool_get_link_ksettings(struct net_device *dev,
 752                                              struct ethtool_link_ksettings *cmd)
 753{
 754        const struct macvlan_dev *vlan = netdev_priv(dev);
 755
 756        return __ethtool_get_link_ksettings(vlan->lowerdev, cmd);
 757}
 758
 759static netdev_features_t macvlan_fix_features(struct net_device *dev,
 760                                              netdev_features_t features)
 761{
 762        struct macvlan_dev *vlan = netdev_priv(dev);
 763        netdev_features_t lowerdev_features = vlan->lowerdev->features;
 764        netdev_features_t mask;
 765
 766        features |= NETIF_F_ALL_FOR_ALL;
 767        features &= (vlan->set_features | ~MACVLAN_FEATURES);
 768        mask = features;
 769
 770        lowerdev_features &= (features | ~NETIF_F_LRO);
 771        features = netdev_increment_features(lowerdev_features, features, mask);
 772        features |= ALWAYS_ON_FEATURES;
 773        features &= ~NETIF_F_NETNS_LOCAL;
 774
 775        return features;
 776}
 777
 778static int macvlan_dev_get_iflink(const struct net_device *dev)
 779{
 780        struct macvlan_dev *vlan = netdev_priv(dev);
 781
 782        return vlan->lowerdev->ifindex;
 783}
 784
 785static const struct ethtool_ops macvlan_ethtool_ops = {
 786        .get_link               = ethtool_op_get_link,
 787        .get_link_ksettings     = macvlan_ethtool_get_link_ksettings,
 788        .get_drvinfo            = macvlan_ethtool_get_drvinfo,
 789};
 790
 791static const struct net_device_ops macvlan_netdev_ops = {
 792        .ndo_size               = sizeof(struct net_device_ops),
 793        .ndo_init               = macvlan_init,
 794        .ndo_uninit             = macvlan_uninit,
 795        .ndo_open               = macvlan_open,
 796        .ndo_stop               = macvlan_stop,
 797        .ndo_start_xmit         = macvlan_start_xmit,
 798        .ndo_change_mtu_rh74    = macvlan_change_mtu,
 799        .ndo_fix_features       = macvlan_fix_features,
 800        .ndo_change_rx_flags    = macvlan_change_rx_flags,
 801        .ndo_set_mac_address    = macvlan_set_mac_address,
 802        .ndo_set_rx_mode        = macvlan_set_mac_lists,
 803        .ndo_get_stats64        = macvlan_dev_get_stats64,
 804        .ndo_validate_addr      = eth_validate_addr,
 805        .ndo_vlan_rx_add_vid    = macvlan_vlan_rx_add_vid,
 806        .ndo_vlan_rx_kill_vid   = macvlan_vlan_rx_kill_vid,
 807        .ndo_fdb_add            = macvlan_fdb_add,
 808        .ndo_fdb_del            = macvlan_fdb_del,
 809        .extended.ndo_fdb_dump  = ndo_dflt_fdb_dump,
 810        .ndo_get_iflink         = macvlan_dev_get_iflink,
 811};
 812
 813void macvlan_common_setup(struct net_device *dev)
 814{
 815        ether_setup(dev);
 816
 817        dev->priv_flags        &= ~IFF_TX_SKB_SHARING;
 818        netif_keep_dst(dev);
 819        dev->priv_flags        |= IFF_UNICAST_FLT;
 820        dev->netdev_ops         = &macvlan_netdev_ops;
 821        dev->extended->needs_free_netdev        = true;
 822        dev->header_ops         = &macvlan_hard_header_ops;
 823        dev->ethtool_ops        = &macvlan_ethtool_ops;
 824}
 825EXPORT_SYMBOL_GPL(macvlan_common_setup);
 826
 827static void macvlan_setup(struct net_device *dev)
 828{
 829        macvlan_common_setup(dev);
 830        dev->priv_flags |= IFF_NO_QUEUE;
 831}
 832
 833static int macvlan_port_create(struct net_device *dev)
 834{
 835        struct macvlan_port *port;
 836        unsigned int i;
 837        int err;
 838
 839        if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
 840                return -EINVAL;
 841
 842        port = kzalloc(sizeof(*port), GFP_KERNEL);
 843        if (port == NULL)
 844                return -ENOMEM;
 845
 846        port->passthru = false;
 847        port->dev = dev;
 848        INIT_LIST_HEAD(&port->vlans);
 849        for (i = 0; i < MACVLAN_HASH_SIZE; i++)
 850                INIT_HLIST_HEAD(&port->vlan_hash[i]);
 851
 852        skb_queue_head_init(&port->bc_queue);
 853        INIT_WORK(&port->bc_work, macvlan_process_broadcast);
 854
 855        err = netdev_rx_handler_register(dev, macvlan_handle_frame, port);
 856        if (err)
 857                kfree(port);
 858        else
 859                dev->priv_flags |= IFF_MACVLAN_PORT;
 860        return err;
 861}
 862
 863static void macvlan_port_destroy(struct net_device *dev)
 864{
 865        struct macvlan_port *port = macvlan_port_get_rtnl(dev);
 866
 867        dev->priv_flags &= ~IFF_MACVLAN_PORT;
 868        netdev_rx_handler_unregister(dev);
 869
 870        /* After this point, no packet can schedule bc_work anymore,
 871         * but we need to cancel it and purge left skbs if any.
 872         */
 873        cancel_work_sync(&port->bc_work);
 874        __skb_queue_purge(&port->bc_queue);
 875
 876        kfree_rcu(port, rcu);
 877}
 878
 879static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
 880{
 881        if (tb[IFLA_ADDRESS]) {
 882                if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
 883                        return -EINVAL;
 884                if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
 885                        return -EADDRNOTAVAIL;
 886        }
 887
 888        if (data && data[IFLA_MACVLAN_FLAGS] &&
 889            nla_get_u16(data[IFLA_MACVLAN_FLAGS]) & ~MACVLAN_FLAG_NOPROMISC)
 890                return -EINVAL;
 891
 892        if (data && data[IFLA_MACVLAN_MODE]) {
 893                switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) {
 894                case MACVLAN_MODE_PRIVATE:
 895                case MACVLAN_MODE_VEPA:
 896                case MACVLAN_MODE_BRIDGE:
 897                case MACVLAN_MODE_PASSTHRU:
 898                        break;
 899                default:
 900                        return -EINVAL;
 901                }
 902        }
 903        return 0;
 904}
 905
 906int macvlan_common_newlink(struct net *src_net, struct net_device *dev,
 907                           struct nlattr *tb[], struct nlattr *data[])
 908{
 909        struct macvlan_dev *vlan = netdev_priv(dev);
 910        struct macvlan_port *port;
 911        struct net_device *lowerdev;
 912        int err;
 913
 914        if (!tb[IFLA_LINK])
 915                return -EINVAL;
 916
 917        lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
 918        if (lowerdev == NULL)
 919                return -ENODEV;
 920
 921        /* When creating macvlans on top of other macvlans - use
 922         * the real device as the lowerdev.
 923         */
 924        if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) {
 925                struct macvlan_dev *lowervlan = netdev_priv(lowerdev);
 926                lowerdev = lowervlan->lowerdev;
 927        }
 928
 929        if (!tb[IFLA_MTU])
 930                dev->mtu = lowerdev->mtu;
 931        else if (dev->mtu > lowerdev->mtu)
 932                return -EINVAL;
 933
 934        if (!tb[IFLA_ADDRESS])
 935                eth_hw_addr_random(dev);
 936
 937        if (!macvlan_port_exists(lowerdev)) {
 938                err = macvlan_port_create(lowerdev);
 939                if (err < 0)
 940                        return err;
 941        }
 942        port = macvlan_port_get_rtnl(lowerdev);
 943
 944        /* Only 1 macvlan device can be created in passthru mode */
 945        if (port->passthru)
 946                return -EINVAL;
 947
 948        vlan->lowerdev = lowerdev;
 949        vlan->dev      = dev;
 950        vlan->port     = port;
 951        vlan->set_features = MACVLAN_FEATURES;
 952
 953        vlan->mode     = MACVLAN_MODE_VEPA;
 954        if (data && data[IFLA_MACVLAN_MODE])
 955                vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
 956
 957        if (data && data[IFLA_MACVLAN_FLAGS])
 958                vlan->flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
 959
 960        if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
 961                if (port->count)
 962                        return -EINVAL;
 963                port->passthru = true;
 964                memcpy(dev->dev_addr, lowerdev->dev_addr, ETH_ALEN);
 965        }
 966
 967        port->count += 1;
 968        err = register_netdevice(dev);
 969        if (err < 0)
 970                goto destroy_port;
 971
 972        err = netdev_upper_dev_link(lowerdev, dev);
 973        if (err)
 974                goto unregister_netdev;
 975
 976        dev->priv_flags |= IFF_MACVLAN;
 977        list_add_tail_rcu(&vlan->list, &port->vlans);
 978        netif_stacked_transfer_operstate(lowerdev, dev);
 979
 980        return 0;
 981
 982unregister_netdev:
 983        unregister_netdevice(dev);
 984destroy_port:
 985        port->count -= 1;
 986        if (!port->count)
 987                macvlan_port_destroy(lowerdev);
 988
 989        return err;
 990}
 991EXPORT_SYMBOL_GPL(macvlan_common_newlink);
 992
 993static int macvlan_newlink(struct net *src_net, struct net_device *dev,
 994                           struct nlattr *tb[], struct nlattr *data[])
 995{
 996        return macvlan_common_newlink(src_net, dev, tb, data);
 997}
 998
 999void macvlan_dellink(struct net_device *dev, struct list_head *head)
1000{
1001        struct macvlan_dev *vlan = netdev_priv(dev);
1002
1003        list_del_rcu(&vlan->list);
1004        unregister_netdevice_queue(dev, head);
1005        netdev_upper_dev_unlink(vlan->lowerdev, dev);
1006}
1007EXPORT_SYMBOL_GPL(macvlan_dellink);
1008
1009static int macvlan_changelink(struct net_device *dev,
1010                struct nlattr *tb[], struct nlattr *data[])
1011{
1012        struct macvlan_dev *vlan = netdev_priv(dev);
1013
1014        if (data && data[IFLA_MACVLAN_FLAGS]) {
1015                __u16 flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
1016                bool promisc = (flags ^ vlan->flags) & MACVLAN_FLAG_NOPROMISC;
1017                if (vlan->port->passthru && promisc) {
1018                        int err;
1019
1020                        if (flags & MACVLAN_FLAG_NOPROMISC)
1021                                err = dev_set_promiscuity(vlan->lowerdev, -1);
1022                        else
1023                                err = dev_set_promiscuity(vlan->lowerdev, 1);
1024                        if (err < 0)
1025                                return err;
1026                }
1027                vlan->flags = flags;
1028        }
1029        if (data && data[IFLA_MACVLAN_MODE])
1030                vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
1031        return 0;
1032}
1033
1034static size_t macvlan_get_size(const struct net_device *dev)
1035{
1036        return (0
1037                + nla_total_size(4) /* IFLA_MACVLAN_MODE */
1038                + nla_total_size(2) /* IFLA_MACVLAN_FLAGS */
1039                );
1040}
1041
1042static int macvlan_fill_info(struct sk_buff *skb,
1043                                const struct net_device *dev)
1044{
1045        struct macvlan_dev *vlan = netdev_priv(dev);
1046
1047        if (nla_put_u32(skb, IFLA_MACVLAN_MODE, vlan->mode))
1048                goto nla_put_failure;
1049        if (nla_put_u16(skb, IFLA_MACVLAN_FLAGS, vlan->flags))
1050                goto nla_put_failure;
1051        return 0;
1052
1053nla_put_failure:
1054        return -EMSGSIZE;
1055}
1056
1057static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = {
1058        [IFLA_MACVLAN_MODE]  = { .type = NLA_U32 },
1059        [IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 },
1060};
1061
1062int macvlan_link_register(struct rtnl_link_ops *ops)
1063{
1064        /* common fields */
1065        ops->priv_size          = sizeof(struct macvlan_dev);
1066        ops->validate           = macvlan_validate;
1067        ops->maxtype            = IFLA_MACVLAN_MAX;
1068        ops->policy             = macvlan_policy;
1069        ops->changelink         = macvlan_changelink;
1070        ops->get_size           = macvlan_get_size;
1071        ops->fill_info          = macvlan_fill_info;
1072
1073        return rtnl_link_register(ops);
1074};
1075EXPORT_SYMBOL_GPL(macvlan_link_register);
1076
1077static struct net *macvlan_get_link_net(const struct net_device *dev)
1078{
1079        return dev_net(macvlan_dev_real_dev(dev));
1080}
1081
1082static struct rtnl_link_ops macvlan_link_ops = {
1083        .kind           = "macvlan",
1084        .setup          = macvlan_setup,
1085        .newlink        = macvlan_newlink,
1086        .dellink        = macvlan_dellink,
1087        .get_link_net   = macvlan_get_link_net,
1088};
1089
1090static int macvlan_device_event(struct notifier_block *unused,
1091                                unsigned long event, void *ptr)
1092{
1093        struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1094        struct macvlan_dev *vlan, *next;
1095        struct macvlan_port *port;
1096        LIST_HEAD(list_kill);
1097
1098        if (!macvlan_port_exists(dev))
1099                return NOTIFY_DONE;
1100
1101        port = macvlan_port_get_rtnl(dev);
1102
1103        switch (event) {
1104        case NETDEV_CHANGE:
1105                list_for_each_entry(vlan, &port->vlans, list)
1106                        netif_stacked_transfer_operstate(vlan->lowerdev,
1107                                                         vlan->dev);
1108                break;
1109        case NETDEV_FEAT_CHANGE:
1110                list_for_each_entry(vlan, &port->vlans, list) {
1111                        vlan->dev->gso_max_size = dev->gso_max_size;
1112                        netdev_update_features(vlan->dev);
1113                }
1114                break;
1115        case NETDEV_UNREGISTER:
1116                /* twiddle thumbs on netns device moves */
1117                if (dev->reg_state != NETREG_UNREGISTERING)
1118                        break;
1119
1120                list_for_each_entry_safe(vlan, next, &port->vlans, list)
1121                        vlan->dev->rtnl_link_ops->dellink(vlan->dev, &list_kill);
1122                unregister_netdevice_many(&list_kill);
1123                list_del(&list_kill);
1124                break;
1125        case NETDEV_PRE_TYPE_CHANGE:
1126                /* Forbid underlaying device to change its type. */
1127                return NOTIFY_BAD;
1128
1129        case NETDEV_NOTIFY_PEERS:
1130        case NETDEV_BONDING_FAILOVER:
1131        case NETDEV_RESEND_IGMP:
1132                /* Propagate to all vlans */
1133                list_for_each_entry(vlan, &port->vlans, list)
1134                        call_netdevice_notifiers(event, vlan->dev);
1135        }
1136        return NOTIFY_DONE;
1137}
1138
1139static struct notifier_block macvlan_notifier_block __read_mostly = {
1140        .notifier_call  = macvlan_device_event,
1141};
1142
1143static int __init macvlan_init_module(void)
1144{
1145        int err;
1146
1147        register_netdevice_notifier_rh(&macvlan_notifier_block);
1148
1149        err = macvlan_link_register(&macvlan_link_ops);
1150        if (err < 0)
1151                goto err1;
1152        return 0;
1153err1:
1154        unregister_netdevice_notifier_rh(&macvlan_notifier_block);
1155        return err;
1156}
1157
1158static void __exit macvlan_cleanup_module(void)
1159{
1160        rtnl_link_unregister(&macvlan_link_ops);
1161        unregister_netdevice_notifier_rh(&macvlan_notifier_block);
1162}
1163
1164module_init(macvlan_init_module);
1165module_exit(macvlan_cleanup_module);
1166
1167MODULE_LICENSE("GPL");
1168MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
1169MODULE_DESCRIPTION("Driver for MAC address based VLANs");
1170MODULE_ALIAS_RTNL_LINK("macvlan");
1171