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 <net/rtnetlink.h>
  34#include <net/xfrm.h>
  35
  36#define MACVLAN_HASH_SIZE       (1 << BITS_PER_BYTE)
  37
  38struct macvlan_port {
  39        struct net_device       *dev;
  40        struct hlist_head       vlan_hash[MACVLAN_HASH_SIZE];
  41        struct list_head        vlans;
  42        struct rcu_head         rcu;
  43        bool                    passthru;
  44        int                     count;
  45};
  46
  47static void macvlan_port_destroy(struct net_device *dev);
  48
  49#define macvlan_port_get_rcu(dev) \
  50        ((struct macvlan_port *) rcu_dereference(dev->rx_handler_data))
  51#define macvlan_port_get(dev) ((struct macvlan_port *) dev->rx_handler_data)
  52#define macvlan_port_exists(dev) (dev->priv_flags & IFF_MACVLAN_PORT)
  53
  54static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
  55                                               const unsigned char *addr)
  56{
  57        struct macvlan_dev *vlan;
  58
  59        hlist_for_each_entry_rcu(vlan, &port->vlan_hash[addr[5]], hlist) {
  60                if (ether_addr_equal_64bits(vlan->dev->dev_addr, addr))
  61                        return vlan;
  62        }
  63        return NULL;
  64}
  65
  66static void macvlan_hash_add(struct macvlan_dev *vlan)
  67{
  68        struct macvlan_port *port = vlan->port;
  69        const unsigned char *addr = vlan->dev->dev_addr;
  70
  71        hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]);
  72}
  73
  74static void macvlan_hash_del(struct macvlan_dev *vlan, bool sync)
  75{
  76        hlist_del_rcu(&vlan->hlist);
  77        if (sync)
  78                synchronize_rcu();
  79}
  80
  81static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
  82                                        const unsigned char *addr)
  83{
  84        macvlan_hash_del(vlan, true);
  85        /* Now that we are unhashed it is safe to change the device
  86         * address without confusing packet delivery.
  87         */
  88        memcpy(vlan->dev->dev_addr, addr, ETH_ALEN);
  89        macvlan_hash_add(vlan);
  90}
  91
  92static int macvlan_addr_busy(const struct macvlan_port *port,
  93                                const unsigned char *addr)
  94{
  95        /* Test to see if the specified multicast address is
  96         * currently in use by the underlying device or
  97         * another macvlan.
  98         */
  99        if (ether_addr_equal_64bits(port->dev->dev_addr, addr))
 100                return 1;
 101
 102        if (macvlan_hash_lookup(port, addr))
 103                return 1;
 104
 105        return 0;
 106}
 107
 108
 109static int macvlan_broadcast_one(struct sk_buff *skb,
 110                                 const struct macvlan_dev *vlan,
 111                                 const struct ethhdr *eth, bool local)
 112{
 113        struct net_device *dev = vlan->dev;
 114        if (!skb)
 115                return NET_RX_DROP;
 116
 117        if (local)
 118                return vlan->forward(dev, skb);
 119
 120        skb->dev = dev;
 121        if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast))
 122                skb->pkt_type = PACKET_BROADCAST;
 123        else
 124                skb->pkt_type = PACKET_MULTICAST;
 125
 126        return vlan->receive(skb);
 127}
 128
 129static u32 macvlan_hash_mix(const struct macvlan_dev *vlan)
 130{
 131        return (u32)(((unsigned long)vlan) >> L1_CACHE_SHIFT);
 132}
 133
 134
 135static unsigned int mc_hash(const struct macvlan_dev *vlan,
 136                            const unsigned char *addr)
 137{
 138        u32 val = __get_unaligned_cpu32(addr + 2);
 139
 140        val ^= macvlan_hash_mix(vlan);
 141        return hash_32(val, MACVLAN_MC_FILTER_BITS);
 142}
 143
 144static void macvlan_broadcast(struct sk_buff *skb,
 145                              const struct macvlan_port *port,
 146                              struct net_device *src,
 147                              enum macvlan_mode mode)
 148{
 149        const struct ethhdr *eth = eth_hdr(skb);
 150        const struct macvlan_dev *vlan;
 151        struct sk_buff *nskb;
 152        unsigned int i;
 153        int err;
 154        unsigned int hash;
 155
 156        if (skb->protocol == htons(ETH_P_PAUSE))
 157                return;
 158
 159        for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
 160                hlist_for_each_entry_rcu(vlan, &port->vlan_hash[i], hlist) {
 161                        if (vlan->dev == src || !(vlan->mode & mode))
 162                                continue;
 163
 164                        hash = mc_hash(vlan, eth->h_dest);
 165                        if (!test_bit(hash, vlan->mc_filter))
 166                                continue;
 167                        nskb = skb_clone(skb, GFP_ATOMIC);
 168                        err = macvlan_broadcast_one(nskb, vlan, eth,
 169                                         mode == MACVLAN_MODE_BRIDGE);
 170                        macvlan_count_rx(vlan, skb->len + ETH_HLEN,
 171                                         err == NET_RX_SUCCESS, 1);
 172                }
 173        }
 174}
 175
 176/* called under rcu_read_lock() from netif_receive_skb */
 177static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb)
 178{
 179        struct macvlan_port *port;
 180        struct sk_buff *skb = *pskb;
 181        const struct ethhdr *eth = eth_hdr(skb);
 182        const struct macvlan_dev *vlan;
 183        const struct macvlan_dev *src;
 184        struct net_device *dev;
 185        unsigned int len = 0;
 186        int ret = NET_RX_DROP;
 187
 188        port = macvlan_port_get_rcu(skb->dev);
 189        if (is_multicast_ether_addr(eth->h_dest)) {
 190                skb = ip_check_defrag(skb, IP_DEFRAG_MACVLAN);
 191                if (!skb)
 192                        return RX_HANDLER_CONSUMED;
 193                eth = eth_hdr(skb);
 194                src = macvlan_hash_lookup(port, eth->h_source);
 195                if (!src)
 196                        /* frame comes from an external address */
 197                        macvlan_broadcast(skb, port, NULL,
 198                                          MACVLAN_MODE_PRIVATE |
 199                                          MACVLAN_MODE_VEPA    |
 200                                          MACVLAN_MODE_PASSTHRU|
 201                                          MACVLAN_MODE_BRIDGE);
 202                else if (src->mode == MACVLAN_MODE_VEPA)
 203                        /* flood to everyone except source */
 204                        macvlan_broadcast(skb, port, src->dev,
 205                                          MACVLAN_MODE_VEPA |
 206                                          MACVLAN_MODE_BRIDGE);
 207                else if (src->mode == MACVLAN_MODE_BRIDGE)
 208                        /*
 209                         * flood only to VEPA ports, bridge ports
 210                         * already saw the frame on the way out.
 211                         */
 212                        macvlan_broadcast(skb, port, src->dev,
 213                                          MACVLAN_MODE_VEPA);
 214                else {
 215                        /* forward to original port. */
 216                        vlan = src;
 217                        ret = macvlan_broadcast_one(skb, vlan, eth, 0);
 218                        goto out;
 219                }
 220
 221                return RX_HANDLER_PASS;
 222        }
 223
 224        if (port->passthru)
 225                vlan = list_first_or_null_rcu(&port->vlans,
 226                                              struct macvlan_dev, list);
 227        else
 228                vlan = macvlan_hash_lookup(port, eth->h_dest);
 229        if (vlan == NULL)
 230                return RX_HANDLER_PASS;
 231
 232        dev = vlan->dev;
 233        if (unlikely(!(dev->flags & IFF_UP))) {
 234                kfree_skb(skb);
 235                return RX_HANDLER_CONSUMED;
 236        }
 237        len = skb->len + ETH_HLEN;
 238        skb = skb_share_check(skb, GFP_ATOMIC);
 239        if (!skb)
 240                goto out;
 241
 242        skb->dev = dev;
 243        skb->pkt_type = PACKET_HOST;
 244
 245        ret = vlan->receive(skb);
 246
 247out:
 248        macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, 0);
 249        return RX_HANDLER_CONSUMED;
 250}
 251
 252static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
 253{
 254        const struct macvlan_dev *vlan = netdev_priv(dev);
 255        const struct macvlan_port *port = vlan->port;
 256        const struct macvlan_dev *dest;
 257        __u8 ip_summed = skb->ip_summed;
 258
 259        if (vlan->mode == MACVLAN_MODE_BRIDGE) {
 260                const struct ethhdr *eth = (void *)skb->data;
 261                skb->ip_summed = CHECKSUM_UNNECESSARY;
 262
 263                /* send to other bridge ports directly */
 264                if (is_multicast_ether_addr(eth->h_dest)) {
 265                        macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE);
 266                        goto xmit_world;
 267                }
 268
 269                dest = macvlan_hash_lookup(port, eth->h_dest);
 270                if (dest && dest->mode == MACVLAN_MODE_BRIDGE) {
 271                        /* send to lowerdev first for its network taps */
 272                        dev_forward_skb(vlan->lowerdev, skb);
 273
 274                        return NET_XMIT_SUCCESS;
 275                }
 276        }
 277
 278xmit_world:
 279        skb->ip_summed = ip_summed;
 280        skb->dev = vlan->lowerdev;
 281        return dev_queue_xmit(skb);
 282}
 283
 284netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
 285                               struct net_device *dev)
 286{
 287        unsigned int len = skb->len;
 288        int ret;
 289        const struct macvlan_dev *vlan = netdev_priv(dev);
 290
 291        ret = macvlan_queue_xmit(skb, dev);
 292        if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
 293                struct macvlan_pcpu_stats *pcpu_stats;
 294
 295                pcpu_stats = this_cpu_ptr(vlan->pcpu_stats);
 296                u64_stats_update_begin(&pcpu_stats->syncp);
 297                pcpu_stats->tx_packets++;
 298                pcpu_stats->tx_bytes += len;
 299                u64_stats_update_end(&pcpu_stats->syncp);
 300        } else {
 301                this_cpu_inc(vlan->pcpu_stats->tx_dropped);
 302        }
 303        return ret;
 304}
 305EXPORT_SYMBOL_GPL(macvlan_start_xmit);
 306
 307static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
 308                               unsigned short type, const void *daddr,
 309                               const void *saddr, unsigned len)
 310{
 311        const struct macvlan_dev *vlan = netdev_priv(dev);
 312        struct net_device *lowerdev = vlan->lowerdev;
 313
 314        return dev_hard_header(skb, lowerdev, type, daddr,
 315                               saddr ? : dev->dev_addr, len);
 316}
 317
 318static const struct header_ops macvlan_hard_header_ops = {
 319        .create         = macvlan_hard_header,
 320        .rebuild        = eth_rebuild_header,
 321        .parse          = eth_header_parse,
 322        .cache          = eth_header_cache,
 323        .cache_update   = eth_header_cache_update,
 324};
 325
 326static int macvlan_open(struct net_device *dev)
 327{
 328        struct macvlan_dev *vlan = netdev_priv(dev);
 329        struct net_device *lowerdev = vlan->lowerdev;
 330        int err;
 331
 332        if (vlan->port->passthru) {
 333                if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC))
 334                        dev_set_promiscuity(lowerdev, 1);
 335                goto hash_add;
 336        }
 337
 338        err = -EBUSY;
 339        if (macvlan_addr_busy(vlan->port, dev->dev_addr))
 340                goto out;
 341
 342        err = dev_uc_add(lowerdev, dev->dev_addr);
 343        if (err < 0)
 344                goto out;
 345        if (dev->flags & IFF_ALLMULTI) {
 346                err = dev_set_allmulti(lowerdev, 1);
 347                if (err < 0)
 348                        goto del_unicast;
 349        }
 350
 351hash_add:
 352        macvlan_hash_add(vlan);
 353        return 0;
 354
 355del_unicast:
 356        dev_uc_del(lowerdev, dev->dev_addr);
 357out:
 358        return err;
 359}
 360
 361static int macvlan_stop(struct net_device *dev)
 362{
 363        struct macvlan_dev *vlan = netdev_priv(dev);
 364        struct net_device *lowerdev = vlan->lowerdev;
 365
 366        dev_uc_unsync(lowerdev, dev);
 367        dev_mc_unsync(lowerdev, dev);
 368
 369        if (vlan->port->passthru) {
 370                if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC))
 371                        dev_set_promiscuity(lowerdev, -1);
 372                goto hash_del;
 373        }
 374
 375        if (dev->flags & IFF_ALLMULTI)
 376                dev_set_allmulti(lowerdev, -1);
 377
 378        dev_uc_del(lowerdev, dev->dev_addr);
 379
 380hash_del:
 381        macvlan_hash_del(vlan, !dev->dismantle);
 382        return 0;
 383}
 384
 385static int macvlan_set_mac_address(struct net_device *dev, void *p)
 386{
 387        struct macvlan_dev *vlan = netdev_priv(dev);
 388        struct net_device *lowerdev = vlan->lowerdev;
 389        struct sockaddr *addr = p;
 390        int err;
 391
 392        if (!is_valid_ether_addr(addr->sa_data))
 393                return -EADDRNOTAVAIL;
 394
 395        if (!(dev->flags & IFF_UP)) {
 396                /* Just copy in the new address */
 397                memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
 398        } else {
 399                /* Rehash and update the device filters */
 400                if (macvlan_addr_busy(vlan->port, addr->sa_data))
 401                        return -EBUSY;
 402
 403                err = dev_uc_add(lowerdev, addr->sa_data);
 404                if (err)
 405                        return err;
 406
 407                dev_uc_del(lowerdev, dev->dev_addr);
 408
 409                macvlan_hash_change_addr(vlan, addr->sa_data);
 410        }
 411        return 0;
 412}
 413
 414static void macvlan_change_rx_flags(struct net_device *dev, int change)
 415{
 416        struct macvlan_dev *vlan = netdev_priv(dev);
 417        struct net_device *lowerdev = vlan->lowerdev;
 418
 419        if (change & IFF_ALLMULTI)
 420                dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
 421}
 422
 423static void macvlan_set_mac_lists(struct net_device *dev)
 424{
 425        struct macvlan_dev *vlan = netdev_priv(dev);
 426
 427        if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
 428                bitmap_fill(vlan->mc_filter, MACVLAN_MC_FILTER_SZ);
 429        } else {
 430                struct netdev_hw_addr *ha;
 431                DECLARE_BITMAP(filter, MACVLAN_MC_FILTER_SZ);
 432
 433                bitmap_zero(filter, MACVLAN_MC_FILTER_SZ);
 434                netdev_for_each_mc_addr(ha, dev) {
 435                        __set_bit(mc_hash(vlan, ha->addr), filter);
 436                }
 437
 438                __set_bit(mc_hash(vlan, dev->broadcast), filter);
 439
 440                bitmap_copy(vlan->mc_filter, filter, MACVLAN_MC_FILTER_SZ);
 441        }
 442        dev_uc_sync(vlan->lowerdev, dev);
 443        dev_mc_sync(vlan->lowerdev, dev);
 444}
 445
 446static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
 447{
 448        struct macvlan_dev *vlan = netdev_priv(dev);
 449
 450        if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
 451                return -EINVAL;
 452        dev->mtu = new_mtu;
 453        return 0;
 454}
 455
 456/*
 457 * macvlan network devices have devices nesting below it and are a special
 458 * "super class" of normal network devices; split their locks off into a
 459 * separate class since they always nest.
 460 */
 461static struct lock_class_key macvlan_netdev_xmit_lock_key;
 462static struct lock_class_key macvlan_netdev_addr_lock_key;
 463
 464#define MACVLAN_FEATURES \
 465        (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
 466         NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
 467         NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
 468         NETIF_F_HW_VLAN_FILTER)
 469
 470#define MACVLAN_STATE_MASK \
 471        ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
 472
 473static void macvlan_set_lockdep_class_one(struct net_device *dev,
 474                                          struct netdev_queue *txq,
 475                                          void *_unused)
 476{
 477        lockdep_set_class(&txq->_xmit_lock,
 478                          &macvlan_netdev_xmit_lock_key);
 479}
 480
 481static void macvlan_set_lockdep_class(struct net_device *dev)
 482{
 483        lockdep_set_class(&dev->addr_list_lock,
 484                          &macvlan_netdev_addr_lock_key);
 485        netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL);
 486}
 487
 488static int macvlan_init(struct net_device *dev)
 489{
 490        struct macvlan_dev *vlan = netdev_priv(dev);
 491        const struct net_device *lowerdev = vlan->lowerdev;
 492
 493        dev->state              = (dev->state & ~MACVLAN_STATE_MASK) |
 494                                  (lowerdev->state & MACVLAN_STATE_MASK);
 495        dev->features           = lowerdev->features & MACVLAN_FEATURES;
 496        dev->features           |= NETIF_F_LLTX;
 497        dev->gso_max_size       = lowerdev->gso_max_size;
 498        dev->iflink             = lowerdev->ifindex;
 499        dev->hard_header_len    = lowerdev->hard_header_len;
 500
 501        macvlan_set_lockdep_class(dev);
 502
 503        vlan->pcpu_stats = alloc_percpu(struct macvlan_pcpu_stats);
 504        if (!vlan->pcpu_stats)
 505                return -ENOMEM;
 506
 507        return 0;
 508}
 509
 510static void macvlan_uninit(struct net_device *dev)
 511{
 512        struct macvlan_dev *vlan = netdev_priv(dev);
 513        struct macvlan_port *port = vlan->port;
 514
 515        free_percpu(vlan->pcpu_stats);
 516
 517        port->count -= 1;
 518        if (!port->count)
 519                macvlan_port_destroy(port->dev);
 520}
 521
 522static struct rtnl_link_stats64 *macvlan_dev_get_stats64(struct net_device *dev,
 523                                                         struct rtnl_link_stats64 *stats)
 524{
 525        struct macvlan_dev *vlan = netdev_priv(dev);
 526
 527        if (vlan->pcpu_stats) {
 528                struct macvlan_pcpu_stats *p;
 529                u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
 530                u32 rx_errors = 0, tx_dropped = 0;
 531                unsigned int start;
 532                int i;
 533
 534                for_each_possible_cpu(i) {
 535                        p = per_cpu_ptr(vlan->pcpu_stats, i);
 536                        do {
 537                                start = u64_stats_fetch_begin_bh(&p->syncp);
 538                                rx_packets      = p->rx_packets;
 539                                rx_bytes        = p->rx_bytes;
 540                                rx_multicast    = p->rx_multicast;
 541                                tx_packets      = p->tx_packets;
 542                                tx_bytes        = p->tx_bytes;
 543                        } while (u64_stats_fetch_retry_bh(&p->syncp, start));
 544
 545                        stats->rx_packets       += rx_packets;
 546                        stats->rx_bytes         += rx_bytes;
 547                        stats->multicast        += rx_multicast;
 548                        stats->tx_packets       += tx_packets;
 549                        stats->tx_bytes         += tx_bytes;
 550                        /* rx_errors & tx_dropped are u32, updated
 551                         * without syncp protection.
 552                         */
 553                        rx_errors       += p->rx_errors;
 554                        tx_dropped      += p->tx_dropped;
 555                }
 556                stats->rx_errors        = rx_errors;
 557                stats->rx_dropped       = rx_errors;
 558                stats->tx_dropped       = tx_dropped;
 559        }
 560        return stats;
 561}
 562
 563static int macvlan_vlan_rx_add_vid(struct net_device *dev,
 564                                    unsigned short vid)
 565{
 566        struct macvlan_dev *vlan = netdev_priv(dev);
 567        struct net_device *lowerdev = vlan->lowerdev;
 568
 569        return vlan_vid_add(lowerdev, vid);
 570}
 571
 572static int macvlan_vlan_rx_kill_vid(struct net_device *dev,
 573                                     unsigned short vid)
 574{
 575        struct macvlan_dev *vlan = netdev_priv(dev);
 576        struct net_device *lowerdev = vlan->lowerdev;
 577
 578        vlan_vid_del(lowerdev, vid);
 579        return 0;
 580}
 581
 582static int macvlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
 583                           struct net_device *dev,
 584                           const unsigned char *addr,
 585                           u16 flags)
 586{
 587        struct macvlan_dev *vlan = netdev_priv(dev);
 588        int err = -EINVAL;
 589
 590        if (!vlan->port->passthru)
 591                return -EOPNOTSUPP;
 592
 593        if (is_unicast_ether_addr(addr))
 594                err = dev_uc_add_excl(dev, addr);
 595        else if (is_multicast_ether_addr(addr))
 596                err = dev_mc_add_excl(dev, addr);
 597
 598        return err;
 599}
 600
 601static int macvlan_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
 602                           struct net_device *dev,
 603                           const unsigned char *addr)
 604{
 605        struct macvlan_dev *vlan = netdev_priv(dev);
 606        int err = -EINVAL;
 607
 608        if (!vlan->port->passthru)
 609                return -EOPNOTSUPP;
 610
 611        if (is_unicast_ether_addr(addr))
 612                err = dev_uc_del(dev, addr);
 613        else if (is_multicast_ether_addr(addr))
 614                err = dev_mc_del(dev, addr);
 615
 616        return err;
 617}
 618
 619static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
 620                                        struct ethtool_drvinfo *drvinfo)
 621{
 622        strlcpy(drvinfo->driver, "macvlan", sizeof(drvinfo->driver));
 623        strlcpy(drvinfo->version, "0.1", sizeof(drvinfo->version));
 624}
 625
 626static int macvlan_ethtool_get_settings(struct net_device *dev,
 627                                        struct ethtool_cmd *cmd)
 628{
 629        const struct macvlan_dev *vlan = netdev_priv(dev);
 630
 631        return __ethtool_get_settings(vlan->lowerdev, cmd);
 632}
 633
 634static const struct ethtool_ops macvlan_ethtool_ops = {
 635        .get_link               = ethtool_op_get_link,
 636        .get_settings           = macvlan_ethtool_get_settings,
 637        .get_drvinfo            = macvlan_ethtool_get_drvinfo,
 638};
 639
 640static const struct net_device_ops macvlan_netdev_ops = {
 641        .ndo_init               = macvlan_init,
 642        .ndo_uninit             = macvlan_uninit,
 643        .ndo_open               = macvlan_open,
 644        .ndo_stop               = macvlan_stop,
 645        .ndo_start_xmit         = macvlan_start_xmit,
 646        .ndo_change_mtu         = macvlan_change_mtu,
 647        .ndo_change_rx_flags    = macvlan_change_rx_flags,
 648        .ndo_set_mac_address    = macvlan_set_mac_address,
 649        .ndo_set_rx_mode        = macvlan_set_mac_lists,
 650        .ndo_get_stats64        = macvlan_dev_get_stats64,
 651        .ndo_validate_addr      = eth_validate_addr,
 652        .ndo_vlan_rx_add_vid    = macvlan_vlan_rx_add_vid,
 653        .ndo_vlan_rx_kill_vid   = macvlan_vlan_rx_kill_vid,
 654        .ndo_fdb_add            = macvlan_fdb_add,
 655        .ndo_fdb_del            = macvlan_fdb_del,
 656        .ndo_fdb_dump           = ndo_dflt_fdb_dump,
 657};
 658
 659void macvlan_common_setup(struct net_device *dev)
 660{
 661        ether_setup(dev);
 662
 663        dev->priv_flags        &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
 664        dev->priv_flags        |= IFF_UNICAST_FLT;
 665        dev->netdev_ops         = &macvlan_netdev_ops;
 666        dev->destructor         = free_netdev;
 667        dev->header_ops         = &macvlan_hard_header_ops,
 668        dev->ethtool_ops        = &macvlan_ethtool_ops;
 669}
 670EXPORT_SYMBOL_GPL(macvlan_common_setup);
 671
 672static void macvlan_setup(struct net_device *dev)
 673{
 674        macvlan_common_setup(dev);
 675        dev->tx_queue_len       = 0;
 676}
 677
 678static int macvlan_port_create(struct net_device *dev)
 679{
 680        struct macvlan_port *port;
 681        unsigned int i;
 682        int err;
 683
 684        if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
 685                return -EINVAL;
 686
 687        port = kzalloc(sizeof(*port), GFP_KERNEL);
 688        if (port == NULL)
 689                return -ENOMEM;
 690
 691        port->passthru = false;
 692        port->dev = dev;
 693        INIT_LIST_HEAD(&port->vlans);
 694        for (i = 0; i < MACVLAN_HASH_SIZE; i++)
 695                INIT_HLIST_HEAD(&port->vlan_hash[i]);
 696
 697        err = netdev_rx_handler_register(dev, macvlan_handle_frame, port);
 698        if (err)
 699                kfree(port);
 700        else
 701                dev->priv_flags |= IFF_MACVLAN_PORT;
 702        return err;
 703}
 704
 705static void macvlan_port_destroy(struct net_device *dev)
 706{
 707        struct macvlan_port *port = macvlan_port_get(dev);
 708
 709        dev->priv_flags &= ~IFF_MACVLAN_PORT;
 710        netdev_rx_handler_unregister(dev);
 711        kfree_rcu(port, rcu);
 712}
 713
 714static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
 715{
 716        if (tb[IFLA_ADDRESS]) {
 717                if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
 718                        return -EINVAL;
 719                if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
 720                        return -EADDRNOTAVAIL;
 721        }
 722
 723        if (data && data[IFLA_MACVLAN_MODE]) {
 724                switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) {
 725                case MACVLAN_MODE_PRIVATE:
 726                case MACVLAN_MODE_VEPA:
 727                case MACVLAN_MODE_BRIDGE:
 728                case MACVLAN_MODE_PASSTHRU:
 729                        break;
 730                default:
 731                        return -EINVAL;
 732                }
 733        }
 734        return 0;
 735}
 736
 737int macvlan_common_newlink(struct net *src_net, struct net_device *dev,
 738                           struct nlattr *tb[], struct nlattr *data[],
 739                           int (*receive)(struct sk_buff *skb),
 740                           int (*forward)(struct net_device *dev,
 741                                          struct sk_buff *skb))
 742{
 743        struct macvlan_dev *vlan = netdev_priv(dev);
 744        struct macvlan_port *port;
 745        struct net_device *lowerdev;
 746        int err;
 747
 748        if (!tb[IFLA_LINK])
 749                return -EINVAL;
 750
 751        lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
 752        if (lowerdev == NULL)
 753                return -ENODEV;
 754
 755        /* When creating macvlans on top of other macvlans - use
 756         * the real device as the lowerdev.
 757         */
 758        if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) {
 759                struct macvlan_dev *lowervlan = netdev_priv(lowerdev);
 760                lowerdev = lowervlan->lowerdev;
 761        }
 762
 763        if (!tb[IFLA_MTU])
 764                dev->mtu = lowerdev->mtu;
 765        else if (dev->mtu > lowerdev->mtu)
 766                return -EINVAL;
 767
 768        if (!tb[IFLA_ADDRESS])
 769                eth_hw_addr_random(dev);
 770
 771        if (!macvlan_port_exists(lowerdev)) {
 772                err = macvlan_port_create(lowerdev);
 773                if (err < 0)
 774                        return err;
 775        }
 776        port = macvlan_port_get(lowerdev);
 777
 778        /* Only 1 macvlan device can be created in passthru mode */
 779        if (port->passthru)
 780                return -EINVAL;
 781
 782        vlan->lowerdev = lowerdev;
 783        vlan->dev      = dev;
 784        vlan->port     = port;
 785        vlan->receive  = receive;
 786        vlan->forward  = forward;
 787
 788        vlan->mode     = MACVLAN_MODE_VEPA;
 789        if (data && data[IFLA_MACVLAN_MODE])
 790                vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
 791
 792        if (data && data[IFLA_MACVLAN_FLAGS])
 793                vlan->flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
 794
 795        if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
 796                if (port->count)
 797                        return -EINVAL;
 798                port->passthru = true;
 799                memcpy(dev->dev_addr, lowerdev->dev_addr, ETH_ALEN);
 800        }
 801
 802        err = netdev_upper_dev_link(lowerdev, dev);
 803        if (err)
 804                goto destroy_port;
 805
 806        port->count += 1;
 807        err = register_netdevice(dev);
 808        if (err < 0)
 809                goto upper_dev_unlink;
 810
 811        list_add_tail_rcu(&vlan->list, &port->vlans);
 812        netif_stacked_transfer_operstate(lowerdev, dev);
 813
 814        return 0;
 815
 816upper_dev_unlink:
 817        netdev_upper_dev_unlink(lowerdev, dev);
 818destroy_port:
 819        port->count -= 1;
 820        if (!port->count)
 821                macvlan_port_destroy(lowerdev);
 822
 823        return err;
 824}
 825EXPORT_SYMBOL_GPL(macvlan_common_newlink);
 826
 827static int macvlan_newlink(struct net *src_net, struct net_device *dev,
 828                           struct nlattr *tb[], struct nlattr *data[])
 829{
 830        return macvlan_common_newlink(src_net, dev, tb, data,
 831                                      netif_rx,
 832                                      dev_forward_skb);
 833}
 834
 835void macvlan_dellink(struct net_device *dev, struct list_head *head)
 836{
 837        struct macvlan_dev *vlan = netdev_priv(dev);
 838
 839        list_del_rcu(&vlan->list);
 840        unregister_netdevice_queue(dev, head);
 841        netdev_upper_dev_unlink(vlan->lowerdev, dev);
 842}
 843EXPORT_SYMBOL_GPL(macvlan_dellink);
 844
 845static int macvlan_changelink(struct net_device *dev,
 846                struct nlattr *tb[], struct nlattr *data[])
 847{
 848        struct macvlan_dev *vlan = netdev_priv(dev);
 849        if (data && data[IFLA_MACVLAN_MODE])
 850                vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
 851        if (data && data[IFLA_MACVLAN_FLAGS]) {
 852                __u16 flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
 853                bool promisc = (flags ^ vlan->flags) & MACVLAN_FLAG_NOPROMISC;
 854
 855                if (promisc && (flags & MACVLAN_FLAG_NOPROMISC))
 856                        dev_set_promiscuity(vlan->lowerdev, -1);
 857                else if (promisc && !(flags & MACVLAN_FLAG_NOPROMISC))
 858                        dev_set_promiscuity(vlan->lowerdev, 1);
 859                vlan->flags = flags;
 860        }
 861        return 0;
 862}
 863
 864static size_t macvlan_get_size(const struct net_device *dev)
 865{
 866        return (0
 867                + nla_total_size(4) /* IFLA_MACVLAN_MODE */
 868                + nla_total_size(2) /* IFLA_MACVLAN_FLAGS */
 869                );
 870}
 871
 872static int macvlan_fill_info(struct sk_buff *skb,
 873                                const struct net_device *dev)
 874{
 875        struct macvlan_dev *vlan = netdev_priv(dev);
 876
 877        if (nla_put_u32(skb, IFLA_MACVLAN_MODE, vlan->mode))
 878                goto nla_put_failure;
 879        if (nla_put_u16(skb, IFLA_MACVLAN_FLAGS, vlan->flags))
 880                goto nla_put_failure;
 881        return 0;
 882
 883nla_put_failure:
 884        return -EMSGSIZE;
 885}
 886
 887static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = {
 888        [IFLA_MACVLAN_MODE]  = { .type = NLA_U32 },
 889        [IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 },
 890};
 891
 892int macvlan_link_register(struct rtnl_link_ops *ops)
 893{
 894        /* common fields */
 895        ops->priv_size          = sizeof(struct macvlan_dev);
 896        ops->validate           = macvlan_validate;
 897        ops->maxtype            = IFLA_MACVLAN_MAX;
 898        ops->policy             = macvlan_policy;
 899        ops->changelink         = macvlan_changelink;
 900        ops->get_size           = macvlan_get_size;
 901        ops->fill_info          = macvlan_fill_info;
 902
 903        return rtnl_link_register(ops);
 904};
 905EXPORT_SYMBOL_GPL(macvlan_link_register);
 906
 907static struct rtnl_link_ops macvlan_link_ops = {
 908        .kind           = "macvlan",
 909        .setup          = macvlan_setup,
 910        .newlink        = macvlan_newlink,
 911        .dellink        = macvlan_dellink,
 912};
 913
 914static int macvlan_device_event(struct notifier_block *unused,
 915                                unsigned long event, void *ptr)
 916{
 917        struct net_device *dev = ptr;
 918        struct macvlan_dev *vlan, *next;
 919        struct macvlan_port *port;
 920        LIST_HEAD(list_kill);
 921
 922        if (!macvlan_port_exists(dev))
 923                return NOTIFY_DONE;
 924
 925        port = macvlan_port_get(dev);
 926
 927        switch (event) {
 928        case NETDEV_CHANGE:
 929                list_for_each_entry(vlan, &port->vlans, list)
 930                        netif_stacked_transfer_operstate(vlan->lowerdev,
 931                                                         vlan->dev);
 932                break;
 933        case NETDEV_FEAT_CHANGE:
 934                list_for_each_entry(vlan, &port->vlans, list) {
 935                        vlan->dev->features = dev->features & MACVLAN_FEATURES;
 936                        vlan->dev->gso_max_size = dev->gso_max_size;
 937                        netdev_features_change(vlan->dev);
 938                }
 939                break;
 940        case NETDEV_UNREGISTER:
 941                /* twiddle thumbs on netns device moves */
 942                if (dev->reg_state != NETREG_UNREGISTERING)
 943                        break;
 944
 945                list_for_each_entry_safe(vlan, next, &port->vlans, list)
 946                        vlan->dev->rtnl_link_ops->dellink(vlan->dev, &list_kill);
 947                unregister_netdevice_many(&list_kill);
 948                list_del(&list_kill);
 949                break;
 950        case NETDEV_PRE_TYPE_CHANGE:
 951                /* Forbid underlaying device to change its type. */
 952                return NOTIFY_BAD;
 953        }
 954        return NOTIFY_DONE;
 955}
 956
 957static struct notifier_block macvlan_notifier_block __read_mostly = {
 958        .notifier_call  = macvlan_device_event,
 959};
 960
 961static int __init macvlan_init_module(void)
 962{
 963        int err;
 964
 965        register_netdevice_notifier(&macvlan_notifier_block);
 966
 967        err = macvlan_link_register(&macvlan_link_ops);
 968        if (err < 0)
 969                goto err1;
 970        return 0;
 971err1:
 972        unregister_netdevice_notifier(&macvlan_notifier_block);
 973        return err;
 974}
 975
 976static void __exit macvlan_cleanup_module(void)
 977{
 978        rtnl_link_unregister(&macvlan_link_ops);
 979        unregister_netdevice_notifier(&macvlan_notifier_block);
 980}
 981
 982module_init(macvlan_init_module);
 983module_exit(macvlan_cleanup_module);
 984
 985MODULE_LICENSE("GPL");
 986MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
 987MODULE_DESCRIPTION("Driver for MAC address based VLANs");
 988MODULE_ALIAS_RTNL_LINK("macvlan");
 989
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.