1b863ceb7SPatrick McHardy /* 2b863ceb7SPatrick McHardy * Copyright (c) 2007 Patrick McHardy <kaber@trash.net> 3b863ceb7SPatrick McHardy * 4b863ceb7SPatrick McHardy * This program is free software; you can redistribute it and/or 5b863ceb7SPatrick McHardy * modify it under the terms of the GNU General Public License as 6b863ceb7SPatrick McHardy * published by the Free Software Foundation; either version 2 of 7b863ceb7SPatrick McHardy * the License, or (at your option) any later version. 8b863ceb7SPatrick McHardy * 9b863ceb7SPatrick McHardy * The code this is based on carried the following copyright notice: 10b863ceb7SPatrick McHardy * --- 11b863ceb7SPatrick McHardy * (C) Copyright 2001-2006 12b863ceb7SPatrick McHardy * Alex Zeffertt, Cambridge Broadband Ltd, ajz@cambridgebroadband.com 13b863ceb7SPatrick McHardy * Re-worked by Ben Greear <greearb@candelatech.com> 14b863ceb7SPatrick McHardy * --- 15b863ceb7SPatrick McHardy */ 16b863ceb7SPatrick McHardy #include <linux/kernel.h> 17b863ceb7SPatrick McHardy #include <linux/types.h> 18b863ceb7SPatrick McHardy #include <linux/module.h> 19b863ceb7SPatrick McHardy #include <linux/init.h> 20b863ceb7SPatrick McHardy #include <linux/errno.h> 21b863ceb7SPatrick McHardy #include <linux/slab.h> 22b863ceb7SPatrick McHardy #include <linux/string.h> 2382524746SFranck Bui-Huu #include <linux/rculist.h> 24b863ceb7SPatrick McHardy #include <linux/notifier.h> 25b863ceb7SPatrick McHardy #include <linux/netdevice.h> 26b863ceb7SPatrick McHardy #include <linux/etherdevice.h> 27b863ceb7SPatrick McHardy #include <linux/ethtool.h> 28b863ceb7SPatrick McHardy #include <linux/if_arp.h> 29b863ceb7SPatrick McHardy #include <linux/if_link.h> 30b863ceb7SPatrick McHardy #include <linux/if_macvlan.h> 31b863ceb7SPatrick McHardy #include <net/rtnetlink.h> 32618e1b74SArnd Bergmann #include <net/xfrm.h> 33b863ceb7SPatrick McHardy 34b863ceb7SPatrick McHardy #define MACVLAN_HASH_SIZE (1 << BITS_PER_BYTE) 35b863ceb7SPatrick McHardy 36b863ceb7SPatrick McHardy struct macvlan_port { 37b863ceb7SPatrick McHardy struct net_device *dev; 38b863ceb7SPatrick McHardy struct hlist_head vlan_hash[MACVLAN_HASH_SIZE]; 39b863ceb7SPatrick McHardy struct list_head vlans; 40b863ceb7SPatrick McHardy }; 41b863ceb7SPatrick McHardy 42b863ceb7SPatrick McHardy static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port, 43b863ceb7SPatrick McHardy const unsigned char *addr) 44b863ceb7SPatrick McHardy { 45b863ceb7SPatrick McHardy struct macvlan_dev *vlan; 46b863ceb7SPatrick McHardy struct hlist_node *n; 47b863ceb7SPatrick McHardy 48b863ceb7SPatrick McHardy hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[addr[5]], hlist) { 49ac06713dSEric Dumazet if (!compare_ether_addr_64bits(vlan->dev->dev_addr, addr)) 50b863ceb7SPatrick McHardy return vlan; 51b863ceb7SPatrick McHardy } 52b863ceb7SPatrick McHardy return NULL; 53b863ceb7SPatrick McHardy } 54b863ceb7SPatrick McHardy 55f9ac30f0SEric Biederman static void macvlan_hash_add(struct macvlan_dev *vlan) 56f9ac30f0SEric Biederman { 57f9ac30f0SEric Biederman struct macvlan_port *port = vlan->port; 58f9ac30f0SEric Biederman const unsigned char *addr = vlan->dev->dev_addr; 59f9ac30f0SEric Biederman 60f9ac30f0SEric Biederman hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]); 61f9ac30f0SEric Biederman } 62f9ac30f0SEric Biederman 63f9ac30f0SEric Biederman static void macvlan_hash_del(struct macvlan_dev *vlan) 64f9ac30f0SEric Biederman { 65f9ac30f0SEric Biederman hlist_del_rcu(&vlan->hlist); 66f9ac30f0SEric Biederman synchronize_rcu(); 67f9ac30f0SEric Biederman } 68f9ac30f0SEric Biederman 69f9ac30f0SEric Biederman static void macvlan_hash_change_addr(struct macvlan_dev *vlan, 70f9ac30f0SEric Biederman const unsigned char *addr) 71f9ac30f0SEric Biederman { 72f9ac30f0SEric Biederman macvlan_hash_del(vlan); 73f9ac30f0SEric Biederman /* Now that we are unhashed it is safe to change the device 74f9ac30f0SEric Biederman * address without confusing packet delivery. 75f9ac30f0SEric Biederman */ 76f9ac30f0SEric Biederman memcpy(vlan->dev->dev_addr, addr, ETH_ALEN); 77f9ac30f0SEric Biederman macvlan_hash_add(vlan); 78f9ac30f0SEric Biederman } 79f9ac30f0SEric Biederman 80f9ac30f0SEric Biederman static int macvlan_addr_busy(const struct macvlan_port *port, 81f9ac30f0SEric Biederman const unsigned char *addr) 82f9ac30f0SEric Biederman { 83f9ac30f0SEric Biederman /* Test to see if the specified multicast address is 84f9ac30f0SEric Biederman * currently in use by the underlying device or 85f9ac30f0SEric Biederman * another macvlan. 86f9ac30f0SEric Biederman */ 87ac06713dSEric Dumazet if (!compare_ether_addr_64bits(port->dev->dev_addr, addr)) 88f9ac30f0SEric Biederman return 1; 89f9ac30f0SEric Biederman 90f9ac30f0SEric Biederman if (macvlan_hash_lookup(port, addr)) 91f9ac30f0SEric Biederman return 1; 92f9ac30f0SEric Biederman 93f9ac30f0SEric Biederman return 0; 94f9ac30f0SEric Biederman } 95f9ac30f0SEric Biederman 96a1e514c5SArnd Bergmann 97fc0663d6SArnd Bergmann static int macvlan_broadcast_one(struct sk_buff *skb, 98fc0663d6SArnd Bergmann const struct macvlan_dev *vlan, 99618e1b74SArnd Bergmann const struct ethhdr *eth, bool local) 100a1e514c5SArnd Bergmann { 101fc0663d6SArnd Bergmann struct net_device *dev = vlan->dev; 102a1e514c5SArnd Bergmann if (!skb) 103a1e514c5SArnd Bergmann return NET_RX_DROP; 104a1e514c5SArnd Bergmann 105618e1b74SArnd Bergmann if (local) 106fc0663d6SArnd Bergmann return vlan->forward(dev, skb); 107618e1b74SArnd Bergmann 108a1e514c5SArnd Bergmann skb->dev = dev; 109a1e514c5SArnd Bergmann if (!compare_ether_addr_64bits(eth->h_dest, 110a1e514c5SArnd Bergmann dev->broadcast)) 111a1e514c5SArnd Bergmann skb->pkt_type = PACKET_BROADCAST; 112a1e514c5SArnd Bergmann else 113a1e514c5SArnd Bergmann skb->pkt_type = PACKET_MULTICAST; 114a1e514c5SArnd Bergmann 115fc0663d6SArnd Bergmann return vlan->receive(skb); 116a1e514c5SArnd Bergmann } 117a1e514c5SArnd Bergmann 118b863ceb7SPatrick McHardy static void macvlan_broadcast(struct sk_buff *skb, 119618e1b74SArnd Bergmann const struct macvlan_port *port, 120618e1b74SArnd Bergmann struct net_device *src, 121618e1b74SArnd Bergmann enum macvlan_mode mode) 122b863ceb7SPatrick McHardy { 123b863ceb7SPatrick McHardy const struct ethhdr *eth = eth_hdr(skb); 124b863ceb7SPatrick McHardy const struct macvlan_dev *vlan; 125b863ceb7SPatrick McHardy struct hlist_node *n; 126b863ceb7SPatrick McHardy struct sk_buff *nskb; 127b863ceb7SPatrick McHardy unsigned int i; 128a1e514c5SArnd Bergmann int err; 129b863ceb7SPatrick McHardy 130efbbced3SPatrick McHardy if (skb->protocol == htons(ETH_P_PAUSE)) 131efbbced3SPatrick McHardy return; 132efbbced3SPatrick McHardy 133b863ceb7SPatrick McHardy for (i = 0; i < MACVLAN_HASH_SIZE; i++) { 134b863ceb7SPatrick McHardy hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[i], hlist) { 135618e1b74SArnd Bergmann if (vlan->dev == src || !(vlan->mode & mode)) 136618e1b74SArnd Bergmann continue; 137618e1b74SArnd Bergmann 138b863ceb7SPatrick McHardy nskb = skb_clone(skb, GFP_ATOMIC); 139fc0663d6SArnd Bergmann err = macvlan_broadcast_one(nskb, vlan, eth, 140618e1b74SArnd Bergmann mode == MACVLAN_MODE_BRIDGE); 141a1e514c5SArnd Bergmann macvlan_count_rx(vlan, skb->len + ETH_HLEN, 142a1e514c5SArnd Bergmann err == NET_RX_SUCCESS, 1); 143b863ceb7SPatrick McHardy } 144b863ceb7SPatrick McHardy } 145b863ceb7SPatrick McHardy } 146b863ceb7SPatrick McHardy 147b863ceb7SPatrick McHardy /* called under rcu_read_lock() from netif_receive_skb */ 148a14462f1SJiri Pirko static struct sk_buff *macvlan_handle_frame(struct macvlan_port *port, 149a14462f1SJiri Pirko struct sk_buff *skb) 150b863ceb7SPatrick McHardy { 151b863ceb7SPatrick McHardy const struct ethhdr *eth = eth_hdr(skb); 152b863ceb7SPatrick McHardy const struct macvlan_dev *vlan; 153618e1b74SArnd Bergmann const struct macvlan_dev *src; 154b863ceb7SPatrick McHardy struct net_device *dev; 155a1e514c5SArnd Bergmann unsigned int len; 156b863ceb7SPatrick McHardy 157b863ceb7SPatrick McHardy if (is_multicast_ether_addr(eth->h_dest)) { 158618e1b74SArnd Bergmann src = macvlan_hash_lookup(port, eth->h_source); 159618e1b74SArnd Bergmann if (!src) 160618e1b74SArnd Bergmann /* frame comes from an external address */ 161618e1b74SArnd Bergmann macvlan_broadcast(skb, port, NULL, 162618e1b74SArnd Bergmann MACVLAN_MODE_PRIVATE | 163618e1b74SArnd Bergmann MACVLAN_MODE_VEPA | 164618e1b74SArnd Bergmann MACVLAN_MODE_BRIDGE); 165618e1b74SArnd Bergmann else if (src->mode == MACVLAN_MODE_VEPA) 166618e1b74SArnd Bergmann /* flood to everyone except source */ 167618e1b74SArnd Bergmann macvlan_broadcast(skb, port, src->dev, 168618e1b74SArnd Bergmann MACVLAN_MODE_VEPA | 169618e1b74SArnd Bergmann MACVLAN_MODE_BRIDGE); 170618e1b74SArnd Bergmann else if (src->mode == MACVLAN_MODE_BRIDGE) 171618e1b74SArnd Bergmann /* 172618e1b74SArnd Bergmann * flood only to VEPA ports, bridge ports 173618e1b74SArnd Bergmann * already saw the frame on the way out. 174618e1b74SArnd Bergmann */ 175618e1b74SArnd Bergmann macvlan_broadcast(skb, port, src->dev, 176618e1b74SArnd Bergmann MACVLAN_MODE_VEPA); 177b863ceb7SPatrick McHardy return skb; 178b863ceb7SPatrick McHardy } 179b863ceb7SPatrick McHardy 180b863ceb7SPatrick McHardy vlan = macvlan_hash_lookup(port, eth->h_dest); 181b863ceb7SPatrick McHardy if (vlan == NULL) 182b863ceb7SPatrick McHardy return skb; 183b863ceb7SPatrick McHardy 184b863ceb7SPatrick McHardy dev = vlan->dev; 185b863ceb7SPatrick McHardy if (unlikely(!(dev->flags & IFF_UP))) { 186b863ceb7SPatrick McHardy kfree_skb(skb); 187b863ceb7SPatrick McHardy return NULL; 188b863ceb7SPatrick McHardy } 189a1e514c5SArnd Bergmann len = skb->len + ETH_HLEN; 190b863ceb7SPatrick McHardy skb = skb_share_check(skb, GFP_ATOMIC); 191a1e514c5SArnd Bergmann macvlan_count_rx(vlan, len, skb != NULL, 0); 192a1e514c5SArnd Bergmann if (!skb) 193b863ceb7SPatrick McHardy return NULL; 194b863ceb7SPatrick McHardy 195b863ceb7SPatrick McHardy skb->dev = dev; 196b863ceb7SPatrick McHardy skb->pkt_type = PACKET_HOST; 197b863ceb7SPatrick McHardy 198fc0663d6SArnd Bergmann vlan->receive(skb); 199b863ceb7SPatrick McHardy return NULL; 200b863ceb7SPatrick McHardy } 201b863ceb7SPatrick McHardy 202618e1b74SArnd Bergmann static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev) 203618e1b74SArnd Bergmann { 204618e1b74SArnd Bergmann const struct macvlan_dev *vlan = netdev_priv(dev); 205618e1b74SArnd Bergmann const struct macvlan_port *port = vlan->port; 206618e1b74SArnd Bergmann const struct macvlan_dev *dest; 207618e1b74SArnd Bergmann 208618e1b74SArnd Bergmann if (vlan->mode == MACVLAN_MODE_BRIDGE) { 209618e1b74SArnd Bergmann const struct ethhdr *eth = (void *)skb->data; 210618e1b74SArnd Bergmann 211618e1b74SArnd Bergmann /* send to other bridge ports directly */ 212618e1b74SArnd Bergmann if (is_multicast_ether_addr(eth->h_dest)) { 213618e1b74SArnd Bergmann macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE); 214618e1b74SArnd Bergmann goto xmit_world; 215618e1b74SArnd Bergmann } 216618e1b74SArnd Bergmann 217618e1b74SArnd Bergmann dest = macvlan_hash_lookup(port, eth->h_dest); 218618e1b74SArnd Bergmann if (dest && dest->mode == MACVLAN_MODE_BRIDGE) { 219618e1b74SArnd Bergmann unsigned int length = skb->len + ETH_HLEN; 220fc0663d6SArnd Bergmann int ret = dest->forward(dest->dev, skb); 221618e1b74SArnd Bergmann macvlan_count_rx(dest, length, 222618e1b74SArnd Bergmann ret == NET_RX_SUCCESS, 0); 223618e1b74SArnd Bergmann 224618e1b74SArnd Bergmann return NET_XMIT_SUCCESS; 225618e1b74SArnd Bergmann } 226618e1b74SArnd Bergmann } 227618e1b74SArnd Bergmann 228618e1b74SArnd Bergmann xmit_world: 2298a83a00bSArnd Bergmann skb_set_dev(skb, vlan->lowerdev); 230618e1b74SArnd Bergmann return dev_queue_xmit(skb); 231618e1b74SArnd Bergmann } 232618e1b74SArnd Bergmann 233fc0663d6SArnd Bergmann netdev_tx_t macvlan_start_xmit(struct sk_buff *skb, 234424efe9cSStephen Hemminger struct net_device *dev) 235b863ceb7SPatrick McHardy { 2362c114553SEric Dumazet int i = skb_get_queue_mapping(skb); 2372c114553SEric Dumazet struct netdev_queue *txq = netdev_get_tx_queue(dev, i); 238b863ceb7SPatrick McHardy unsigned int len = skb->len; 239b863ceb7SPatrick McHardy int ret; 240b863ceb7SPatrick McHardy 241618e1b74SArnd Bergmann ret = macvlan_queue_xmit(skb, dev); 242*2d6c9ffcSEric Dumazet if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) { 2432c114553SEric Dumazet txq->tx_packets++; 2442c114553SEric Dumazet txq->tx_bytes += len; 2452c114553SEric Dumazet } else 2462c114553SEric Dumazet txq->tx_dropped++; 2472c114553SEric Dumazet 248cbbef5e1SPatrick McHardy return ret; 249b863ceb7SPatrick McHardy } 250fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_start_xmit); 251b863ceb7SPatrick McHardy 252b863ceb7SPatrick McHardy static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev, 2533b04dddeSStephen Hemminger unsigned short type, const void *daddr, 2543b04dddeSStephen Hemminger const void *saddr, unsigned len) 255b863ceb7SPatrick McHardy { 256b863ceb7SPatrick McHardy const struct macvlan_dev *vlan = netdev_priv(dev); 257b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 258b863ceb7SPatrick McHardy 2590c4e8581SStephen Hemminger return dev_hard_header(skb, lowerdev, type, daddr, 260b863ceb7SPatrick McHardy saddr ? : dev->dev_addr, len); 261b863ceb7SPatrick McHardy } 262b863ceb7SPatrick McHardy 2633b04dddeSStephen Hemminger static const struct header_ops macvlan_hard_header_ops = { 2643b04dddeSStephen Hemminger .create = macvlan_hard_header, 2653b04dddeSStephen Hemminger .rebuild = eth_rebuild_header, 2663b04dddeSStephen Hemminger .parse = eth_header_parse, 2673b04dddeSStephen Hemminger .cache = eth_header_cache, 2683b04dddeSStephen Hemminger .cache_update = eth_header_cache_update, 2693b04dddeSStephen Hemminger }; 2703b04dddeSStephen Hemminger 271b863ceb7SPatrick McHardy static int macvlan_open(struct net_device *dev) 272b863ceb7SPatrick McHardy { 273b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 274b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 275b863ceb7SPatrick McHardy int err; 276b863ceb7SPatrick McHardy 277f9ac30f0SEric Biederman err = -EBUSY; 278f9ac30f0SEric Biederman if (macvlan_addr_busy(vlan->port, dev->dev_addr)) 279f9ac30f0SEric Biederman goto out; 280f9ac30f0SEric Biederman 281a748ee24SJiri Pirko err = dev_uc_add(lowerdev, dev->dev_addr); 282b863ceb7SPatrick McHardy if (err < 0) 283b89fb7daSWang Chen goto out; 284b89fb7daSWang Chen if (dev->flags & IFF_ALLMULTI) { 285b89fb7daSWang Chen err = dev_set_allmulti(lowerdev, 1); 286b89fb7daSWang Chen if (err < 0) 287b89fb7daSWang Chen goto del_unicast; 288b89fb7daSWang Chen } 289f9ac30f0SEric Biederman macvlan_hash_add(vlan); 290b863ceb7SPatrick McHardy return 0; 291b89fb7daSWang Chen 292b89fb7daSWang Chen del_unicast: 293a748ee24SJiri Pirko dev_uc_del(lowerdev, dev->dev_addr); 294b89fb7daSWang Chen out: 295b89fb7daSWang Chen return err; 296b863ceb7SPatrick McHardy } 297b863ceb7SPatrick McHardy 298b863ceb7SPatrick McHardy static int macvlan_stop(struct net_device *dev) 299b863ceb7SPatrick McHardy { 300b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 301b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 302b863ceb7SPatrick McHardy 303b863ceb7SPatrick McHardy dev_mc_unsync(lowerdev, dev); 304b863ceb7SPatrick McHardy if (dev->flags & IFF_ALLMULTI) 305b863ceb7SPatrick McHardy dev_set_allmulti(lowerdev, -1); 306b863ceb7SPatrick McHardy 307a748ee24SJiri Pirko dev_uc_del(lowerdev, dev->dev_addr); 308b863ceb7SPatrick McHardy 309f9ac30f0SEric Biederman macvlan_hash_del(vlan); 310b863ceb7SPatrick McHardy return 0; 311b863ceb7SPatrick McHardy } 312b863ceb7SPatrick McHardy 313ad5d20a6SPatrick McHardy static int macvlan_set_mac_address(struct net_device *dev, void *p) 314ad5d20a6SPatrick McHardy { 315ad5d20a6SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 316ad5d20a6SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 317ad5d20a6SPatrick McHardy struct sockaddr *addr = p; 318ad5d20a6SPatrick McHardy int err; 319ad5d20a6SPatrick McHardy 320ad5d20a6SPatrick McHardy if (!is_valid_ether_addr(addr->sa_data)) 321ad5d20a6SPatrick McHardy return -EADDRNOTAVAIL; 322ad5d20a6SPatrick McHardy 323f9ac30f0SEric Biederman if (!(dev->flags & IFF_UP)) { 324f9ac30f0SEric Biederman /* Just copy in the new address */ 325f9ac30f0SEric Biederman memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN); 326f9ac30f0SEric Biederman } else { 327f9ac30f0SEric Biederman /* Rehash and update the device filters */ 328f9ac30f0SEric Biederman if (macvlan_addr_busy(vlan->port, addr->sa_data)) 329f9ac30f0SEric Biederman return -EBUSY; 330ad5d20a6SPatrick McHardy 331a748ee24SJiri Pirko err = dev_uc_add(lowerdev, addr->sa_data); 332ccffad25SJiri Pirko if (err) 333ad5d20a6SPatrick McHardy return err; 334f9ac30f0SEric Biederman 335a748ee24SJiri Pirko dev_uc_del(lowerdev, dev->dev_addr); 336ad5d20a6SPatrick McHardy 337f9ac30f0SEric Biederman macvlan_hash_change_addr(vlan, addr->sa_data); 338f9ac30f0SEric Biederman } 339ad5d20a6SPatrick McHardy return 0; 340ad5d20a6SPatrick McHardy } 341ad5d20a6SPatrick McHardy 342b863ceb7SPatrick McHardy static void macvlan_change_rx_flags(struct net_device *dev, int change) 343b863ceb7SPatrick McHardy { 344b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 345b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 346b863ceb7SPatrick McHardy 347b863ceb7SPatrick McHardy if (change & IFF_ALLMULTI) 348b863ceb7SPatrick McHardy dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1); 349b863ceb7SPatrick McHardy } 350b863ceb7SPatrick McHardy 351b863ceb7SPatrick McHardy static void macvlan_set_multicast_list(struct net_device *dev) 352b863ceb7SPatrick McHardy { 353b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 354b863ceb7SPatrick McHardy 355b863ceb7SPatrick McHardy dev_mc_sync(vlan->lowerdev, dev); 356b863ceb7SPatrick McHardy } 357b863ceb7SPatrick McHardy 358b863ceb7SPatrick McHardy static int macvlan_change_mtu(struct net_device *dev, int new_mtu) 359b863ceb7SPatrick McHardy { 360b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 361b863ceb7SPatrick McHardy 362b863ceb7SPatrick McHardy if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu) 363b863ceb7SPatrick McHardy return -EINVAL; 364b863ceb7SPatrick McHardy dev->mtu = new_mtu; 365b863ceb7SPatrick McHardy return 0; 366b863ceb7SPatrick McHardy } 367b863ceb7SPatrick McHardy 368b863ceb7SPatrick McHardy /* 369b863ceb7SPatrick McHardy * macvlan network devices have devices nesting below it and are a special 370b863ceb7SPatrick McHardy * "super class" of normal network devices; split their locks off into a 371b863ceb7SPatrick McHardy * separate class since they always nest. 372b863ceb7SPatrick McHardy */ 373b863ceb7SPatrick McHardy static struct lock_class_key macvlan_netdev_xmit_lock_key; 374cf508b12SDavid S. Miller static struct lock_class_key macvlan_netdev_addr_lock_key; 375b863ceb7SPatrick McHardy 376b863ceb7SPatrick McHardy #define MACVLAN_FEATURES \ 377b863ceb7SPatrick McHardy (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \ 378b863ceb7SPatrick McHardy NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \ 3796eb3a855SPatrick Mullaney NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO) 380b863ceb7SPatrick McHardy 381b863ceb7SPatrick McHardy #define MACVLAN_STATE_MASK \ 382b863ceb7SPatrick McHardy ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT)) 383b863ceb7SPatrick McHardy 384e8a0464cSDavid S. Miller static void macvlan_set_lockdep_class_one(struct net_device *dev, 385e8a0464cSDavid S. Miller struct netdev_queue *txq, 386e8a0464cSDavid S. Miller void *_unused) 387c773e847SDavid S. Miller { 388c773e847SDavid S. Miller lockdep_set_class(&txq->_xmit_lock, 389c773e847SDavid S. Miller &macvlan_netdev_xmit_lock_key); 390c773e847SDavid S. Miller } 391c773e847SDavid S. Miller 392c773e847SDavid S. Miller static void macvlan_set_lockdep_class(struct net_device *dev) 393c773e847SDavid S. Miller { 394cf508b12SDavid S. Miller lockdep_set_class(&dev->addr_list_lock, 395cf508b12SDavid S. Miller &macvlan_netdev_addr_lock_key); 396e8a0464cSDavid S. Miller netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL); 397c773e847SDavid S. Miller } 398c773e847SDavid S. Miller 399b863ceb7SPatrick McHardy static int macvlan_init(struct net_device *dev) 400b863ceb7SPatrick McHardy { 401b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 402b863ceb7SPatrick McHardy const struct net_device *lowerdev = vlan->lowerdev; 403b863ceb7SPatrick McHardy 404b863ceb7SPatrick McHardy dev->state = (dev->state & ~MACVLAN_STATE_MASK) | 405b863ceb7SPatrick McHardy (lowerdev->state & MACVLAN_STATE_MASK); 406b863ceb7SPatrick McHardy dev->features = lowerdev->features & MACVLAN_FEATURES; 4078c2acc53SPatrick McHardy dev->gso_max_size = lowerdev->gso_max_size; 408b863ceb7SPatrick McHardy dev->iflink = lowerdev->ifindex; 409ef5c8996Ssg.tweak@gmail.com dev->hard_header_len = lowerdev->hard_header_len; 410b863ceb7SPatrick McHardy 411c773e847SDavid S. Miller macvlan_set_lockdep_class(dev); 412c773e847SDavid S. Miller 413fccaf710SEric Dumazet vlan->rx_stats = alloc_percpu(struct macvlan_rx_stats); 414fccaf710SEric Dumazet if (!vlan->rx_stats) 415fccaf710SEric Dumazet return -ENOMEM; 416fccaf710SEric Dumazet 417b863ceb7SPatrick McHardy return 0; 418b863ceb7SPatrick McHardy } 419b863ceb7SPatrick McHardy 420fccaf710SEric Dumazet static void macvlan_uninit(struct net_device *dev) 421fccaf710SEric Dumazet { 422fccaf710SEric Dumazet struct macvlan_dev *vlan = netdev_priv(dev); 423fccaf710SEric Dumazet 424fccaf710SEric Dumazet free_percpu(vlan->rx_stats); 425fccaf710SEric Dumazet } 426fccaf710SEric Dumazet 427fccaf710SEric Dumazet static struct net_device_stats *macvlan_dev_get_stats(struct net_device *dev) 428fccaf710SEric Dumazet { 429fccaf710SEric Dumazet struct net_device_stats *stats = &dev->stats; 430fccaf710SEric Dumazet struct macvlan_dev *vlan = netdev_priv(dev); 431fccaf710SEric Dumazet 432fccaf710SEric Dumazet dev_txq_stats_fold(dev, stats); 433fccaf710SEric Dumazet 434fccaf710SEric Dumazet if (vlan->rx_stats) { 435fccaf710SEric Dumazet struct macvlan_rx_stats *p, rx = {0}; 436fccaf710SEric Dumazet int i; 437fccaf710SEric Dumazet 438fccaf710SEric Dumazet for_each_possible_cpu(i) { 439fccaf710SEric Dumazet p = per_cpu_ptr(vlan->rx_stats, i); 440fccaf710SEric Dumazet rx.rx_packets += p->rx_packets; 441fccaf710SEric Dumazet rx.rx_bytes += p->rx_bytes; 442fccaf710SEric Dumazet rx.rx_errors += p->rx_errors; 443fccaf710SEric Dumazet rx.multicast += p->multicast; 444fccaf710SEric Dumazet } 445fccaf710SEric Dumazet stats->rx_packets = rx.rx_packets; 446fccaf710SEric Dumazet stats->rx_bytes = rx.rx_bytes; 447fccaf710SEric Dumazet stats->rx_errors = rx.rx_errors; 448fccaf710SEric Dumazet stats->rx_dropped = rx.rx_errors; 449fccaf710SEric Dumazet stats->multicast = rx.multicast; 450fccaf710SEric Dumazet } 451fccaf710SEric Dumazet return stats; 452fccaf710SEric Dumazet } 453fccaf710SEric Dumazet 454b863ceb7SPatrick McHardy static void macvlan_ethtool_get_drvinfo(struct net_device *dev, 455b863ceb7SPatrick McHardy struct ethtool_drvinfo *drvinfo) 456b863ceb7SPatrick McHardy { 457b863ceb7SPatrick McHardy snprintf(drvinfo->driver, 32, "macvlan"); 458b863ceb7SPatrick McHardy snprintf(drvinfo->version, 32, "0.1"); 459b863ceb7SPatrick McHardy } 460b863ceb7SPatrick McHardy 461b863ceb7SPatrick McHardy static u32 macvlan_ethtool_get_rx_csum(struct net_device *dev) 462b863ceb7SPatrick McHardy { 463b863ceb7SPatrick McHardy const struct macvlan_dev *vlan = netdev_priv(dev); 464b1b67dd4SPatrick McHardy return dev_ethtool_get_rx_csum(vlan->lowerdev); 465b863ceb7SPatrick McHardy } 466b863ceb7SPatrick McHardy 4679edb8bb6SStephen Hemminger static int macvlan_ethtool_get_settings(struct net_device *dev, 4689edb8bb6SStephen Hemminger struct ethtool_cmd *cmd) 4699edb8bb6SStephen Hemminger { 4709edb8bb6SStephen Hemminger const struct macvlan_dev *vlan = netdev_priv(dev); 471b1b67dd4SPatrick McHardy return dev_ethtool_get_settings(vlan->lowerdev, cmd); 4729edb8bb6SStephen Hemminger } 4739edb8bb6SStephen Hemminger 4749edb8bb6SStephen Hemminger static u32 macvlan_ethtool_get_flags(struct net_device *dev) 4759edb8bb6SStephen Hemminger { 4769edb8bb6SStephen Hemminger const struct macvlan_dev *vlan = netdev_priv(dev); 477b1b67dd4SPatrick McHardy return dev_ethtool_get_flags(vlan->lowerdev); 4789edb8bb6SStephen Hemminger } 4799edb8bb6SStephen Hemminger 480b863ceb7SPatrick McHardy static const struct ethtool_ops macvlan_ethtool_ops = { 481b863ceb7SPatrick McHardy .get_link = ethtool_op_get_link, 4829edb8bb6SStephen Hemminger .get_settings = macvlan_ethtool_get_settings, 483b863ceb7SPatrick McHardy .get_rx_csum = macvlan_ethtool_get_rx_csum, 484b863ceb7SPatrick McHardy .get_drvinfo = macvlan_ethtool_get_drvinfo, 4859edb8bb6SStephen Hemminger .get_flags = macvlan_ethtool_get_flags, 486b863ceb7SPatrick McHardy }; 487b863ceb7SPatrick McHardy 48854a30c97SStephen Hemminger static const struct net_device_ops macvlan_netdev_ops = { 48954a30c97SStephen Hemminger .ndo_init = macvlan_init, 490fccaf710SEric Dumazet .ndo_uninit = macvlan_uninit, 49154a30c97SStephen Hemminger .ndo_open = macvlan_open, 49254a30c97SStephen Hemminger .ndo_stop = macvlan_stop, 49300829823SStephen Hemminger .ndo_start_xmit = macvlan_start_xmit, 49454a30c97SStephen Hemminger .ndo_change_mtu = macvlan_change_mtu, 49554a30c97SStephen Hemminger .ndo_change_rx_flags = macvlan_change_rx_flags, 49654a30c97SStephen Hemminger .ndo_set_mac_address = macvlan_set_mac_address, 49754a30c97SStephen Hemminger .ndo_set_multicast_list = macvlan_set_multicast_list, 498fccaf710SEric Dumazet .ndo_get_stats = macvlan_dev_get_stats, 49954a30c97SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 50054a30c97SStephen Hemminger }; 50154a30c97SStephen Hemminger 502b863ceb7SPatrick McHardy static void macvlan_setup(struct net_device *dev) 503b863ceb7SPatrick McHardy { 504b863ceb7SPatrick McHardy ether_setup(dev); 505b863ceb7SPatrick McHardy 50693f154b5SEric Dumazet dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; 50754a30c97SStephen Hemminger dev->netdev_ops = &macvlan_netdev_ops; 508b863ceb7SPatrick McHardy dev->destructor = free_netdev; 5093b04dddeSStephen Hemminger dev->header_ops = &macvlan_hard_header_ops, 510b863ceb7SPatrick McHardy dev->ethtool_ops = &macvlan_ethtool_ops; 511b863ceb7SPatrick McHardy dev->tx_queue_len = 0; 512b863ceb7SPatrick McHardy } 513b863ceb7SPatrick McHardy 514b863ceb7SPatrick McHardy static int macvlan_port_create(struct net_device *dev) 515b863ceb7SPatrick McHardy { 516b863ceb7SPatrick McHardy struct macvlan_port *port; 517b863ceb7SPatrick McHardy unsigned int i; 518b863ceb7SPatrick McHardy 519b863ceb7SPatrick McHardy if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK) 520b863ceb7SPatrick McHardy return -EINVAL; 521b863ceb7SPatrick McHardy 522b863ceb7SPatrick McHardy port = kzalloc(sizeof(*port), GFP_KERNEL); 523b863ceb7SPatrick McHardy if (port == NULL) 524b863ceb7SPatrick McHardy return -ENOMEM; 525b863ceb7SPatrick McHardy 526b863ceb7SPatrick McHardy port->dev = dev; 527b863ceb7SPatrick McHardy INIT_LIST_HEAD(&port->vlans); 528b863ceb7SPatrick McHardy for (i = 0; i < MACVLAN_HASH_SIZE; i++) 529b863ceb7SPatrick McHardy INIT_HLIST_HEAD(&port->vlan_hash[i]); 530b863ceb7SPatrick McHardy rcu_assign_pointer(dev->macvlan_port, port); 531b863ceb7SPatrick McHardy return 0; 532b863ceb7SPatrick McHardy } 533b863ceb7SPatrick McHardy 534b863ceb7SPatrick McHardy static void macvlan_port_destroy(struct net_device *dev) 535b863ceb7SPatrick McHardy { 536b863ceb7SPatrick McHardy struct macvlan_port *port = dev->macvlan_port; 537b863ceb7SPatrick McHardy 538b863ceb7SPatrick McHardy rcu_assign_pointer(dev->macvlan_port, NULL); 539b863ceb7SPatrick McHardy synchronize_rcu(); 540b863ceb7SPatrick McHardy kfree(port); 541b863ceb7SPatrick McHardy } 542b863ceb7SPatrick McHardy 543b863ceb7SPatrick McHardy static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[]) 544b863ceb7SPatrick McHardy { 545b863ceb7SPatrick McHardy if (tb[IFLA_ADDRESS]) { 546b863ceb7SPatrick McHardy if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) 547b863ceb7SPatrick McHardy return -EINVAL; 548b863ceb7SPatrick McHardy if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) 549b863ceb7SPatrick McHardy return -EADDRNOTAVAIL; 550b863ceb7SPatrick McHardy } 55127c0b1a8SArnd Bergmann 55227c0b1a8SArnd Bergmann if (data && data[IFLA_MACVLAN_MODE]) { 55327c0b1a8SArnd Bergmann switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) { 55427c0b1a8SArnd Bergmann case MACVLAN_MODE_PRIVATE: 55527c0b1a8SArnd Bergmann case MACVLAN_MODE_VEPA: 55627c0b1a8SArnd Bergmann case MACVLAN_MODE_BRIDGE: 55727c0b1a8SArnd Bergmann break; 55827c0b1a8SArnd Bergmann default: 55927c0b1a8SArnd Bergmann return -EINVAL; 56027c0b1a8SArnd Bergmann } 56127c0b1a8SArnd Bergmann } 562b863ceb7SPatrick McHardy return 0; 563b863ceb7SPatrick McHardy } 564b863ceb7SPatrick McHardy 5652c114553SEric Dumazet static int macvlan_get_tx_queues(struct net *net, 5662c114553SEric Dumazet struct nlattr *tb[], 5672c114553SEric Dumazet unsigned int *num_tx_queues, 5682c114553SEric Dumazet unsigned int *real_num_tx_queues) 5692c114553SEric Dumazet { 5702c114553SEric Dumazet struct net_device *real_dev; 5712c114553SEric Dumazet 5722c114553SEric Dumazet if (!tb[IFLA_LINK]) 5732c114553SEric Dumazet return -EINVAL; 5742c114553SEric Dumazet 5752c114553SEric Dumazet real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK])); 5762c114553SEric Dumazet if (!real_dev) 5772c114553SEric Dumazet return -ENODEV; 5782c114553SEric Dumazet 5792c114553SEric Dumazet *num_tx_queues = real_dev->num_tx_queues; 5802c114553SEric Dumazet *real_num_tx_queues = real_dev->real_num_tx_queues; 5812c114553SEric Dumazet return 0; 5822c114553SEric Dumazet } 5832c114553SEric Dumazet 584fc0663d6SArnd Bergmann int macvlan_common_newlink(struct net *src_net, struct net_device *dev, 585fc0663d6SArnd Bergmann struct nlattr *tb[], struct nlattr *data[], 586fc0663d6SArnd Bergmann int (*receive)(struct sk_buff *skb), 587fc0663d6SArnd Bergmann int (*forward)(struct net_device *dev, 588fc0663d6SArnd Bergmann struct sk_buff *skb)) 589b863ceb7SPatrick McHardy { 590b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 591b863ceb7SPatrick McHardy struct macvlan_port *port; 592b863ceb7SPatrick McHardy struct net_device *lowerdev; 593b863ceb7SPatrick McHardy int err; 594b863ceb7SPatrick McHardy 595b863ceb7SPatrick McHardy if (!tb[IFLA_LINK]) 596b863ceb7SPatrick McHardy return -EINVAL; 597b863ceb7SPatrick McHardy 59881adee47SEric W. Biederman lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK])); 599b863ceb7SPatrick McHardy if (lowerdev == NULL) 600b863ceb7SPatrick McHardy return -ENODEV; 601b863ceb7SPatrick McHardy 602b0832a29SEric Biederman /* When creating macvlans on top of other macvlans - use 603b0832a29SEric Biederman * the real device as the lowerdev. 604a6ca5f1dSPatrick McHardy */ 605b0832a29SEric Biederman if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) { 606b0832a29SEric Biederman struct macvlan_dev *lowervlan = netdev_priv(lowerdev); 607b0832a29SEric Biederman lowerdev = lowervlan->lowerdev; 608b0832a29SEric Biederman } 609a6ca5f1dSPatrick McHardy 610b863ceb7SPatrick McHardy if (!tb[IFLA_MTU]) 611b863ceb7SPatrick McHardy dev->mtu = lowerdev->mtu; 612b863ceb7SPatrick McHardy else if (dev->mtu > lowerdev->mtu) 613b863ceb7SPatrick McHardy return -EINVAL; 614b863ceb7SPatrick McHardy 615b863ceb7SPatrick McHardy if (!tb[IFLA_ADDRESS]) 616b863ceb7SPatrick McHardy random_ether_addr(dev->dev_addr); 617b863ceb7SPatrick McHardy 618b863ceb7SPatrick McHardy if (lowerdev->macvlan_port == NULL) { 619b863ceb7SPatrick McHardy err = macvlan_port_create(lowerdev); 620b863ceb7SPatrick McHardy if (err < 0) 621b863ceb7SPatrick McHardy return err; 622b863ceb7SPatrick McHardy } 623b863ceb7SPatrick McHardy port = lowerdev->macvlan_port; 624b863ceb7SPatrick McHardy 625b863ceb7SPatrick McHardy vlan->lowerdev = lowerdev; 626b863ceb7SPatrick McHardy vlan->dev = dev; 627b863ceb7SPatrick McHardy vlan->port = port; 628fc0663d6SArnd Bergmann vlan->receive = receive; 629fc0663d6SArnd Bergmann vlan->forward = forward; 630b863ceb7SPatrick McHardy 63127c0b1a8SArnd Bergmann vlan->mode = MACVLAN_MODE_VEPA; 63227c0b1a8SArnd Bergmann if (data && data[IFLA_MACVLAN_MODE]) 63327c0b1a8SArnd Bergmann vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]); 63427c0b1a8SArnd Bergmann 635b863ceb7SPatrick McHardy err = register_netdevice(dev); 636b863ceb7SPatrick McHardy if (err < 0) 637b863ceb7SPatrick McHardy return err; 638b863ceb7SPatrick McHardy 639b863ceb7SPatrick McHardy list_add_tail(&vlan->list, &port->vlans); 640fc4a7489SPatrick Mullaney netif_stacked_transfer_operstate(lowerdev, dev); 641b863ceb7SPatrick McHardy return 0; 642b863ceb7SPatrick McHardy } 643fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_common_newlink); 644b863ceb7SPatrick McHardy 645fc0663d6SArnd Bergmann static int macvlan_newlink(struct net *src_net, struct net_device *dev, 646fc0663d6SArnd Bergmann struct nlattr *tb[], struct nlattr *data[]) 647fc0663d6SArnd Bergmann { 648fc0663d6SArnd Bergmann return macvlan_common_newlink(src_net, dev, tb, data, 649fc0663d6SArnd Bergmann netif_rx, 650fc0663d6SArnd Bergmann dev_forward_skb); 651fc0663d6SArnd Bergmann } 652fc0663d6SArnd Bergmann 653fc0663d6SArnd Bergmann void macvlan_dellink(struct net_device *dev, struct list_head *head) 654b863ceb7SPatrick McHardy { 655b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 656b863ceb7SPatrick McHardy struct macvlan_port *port = vlan->port; 657b863ceb7SPatrick McHardy 658b863ceb7SPatrick McHardy list_del(&vlan->list); 65923289a37SEric Dumazet unregister_netdevice_queue(dev, head); 660b863ceb7SPatrick McHardy 661b863ceb7SPatrick McHardy if (list_empty(&port->vlans)) 66273120964SPatrick McHardy macvlan_port_destroy(port->dev); 663b863ceb7SPatrick McHardy } 664fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_dellink); 665b863ceb7SPatrick McHardy 66627c0b1a8SArnd Bergmann static int macvlan_changelink(struct net_device *dev, 66727c0b1a8SArnd Bergmann struct nlattr *tb[], struct nlattr *data[]) 66827c0b1a8SArnd Bergmann { 66927c0b1a8SArnd Bergmann struct macvlan_dev *vlan = netdev_priv(dev); 67027c0b1a8SArnd Bergmann if (data && data[IFLA_MACVLAN_MODE]) 67127c0b1a8SArnd Bergmann vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]); 67227c0b1a8SArnd Bergmann return 0; 67327c0b1a8SArnd Bergmann } 67427c0b1a8SArnd Bergmann 67527c0b1a8SArnd Bergmann static size_t macvlan_get_size(const struct net_device *dev) 67627c0b1a8SArnd Bergmann { 67727c0b1a8SArnd Bergmann return nla_total_size(4); 67827c0b1a8SArnd Bergmann } 67927c0b1a8SArnd Bergmann 68027c0b1a8SArnd Bergmann static int macvlan_fill_info(struct sk_buff *skb, 68127c0b1a8SArnd Bergmann const struct net_device *dev) 68227c0b1a8SArnd Bergmann { 68327c0b1a8SArnd Bergmann struct macvlan_dev *vlan = netdev_priv(dev); 68427c0b1a8SArnd Bergmann 68527c0b1a8SArnd Bergmann NLA_PUT_U32(skb, IFLA_MACVLAN_MODE, vlan->mode); 68627c0b1a8SArnd Bergmann return 0; 68727c0b1a8SArnd Bergmann 68827c0b1a8SArnd Bergmann nla_put_failure: 68927c0b1a8SArnd Bergmann return -EMSGSIZE; 69027c0b1a8SArnd Bergmann } 69127c0b1a8SArnd Bergmann 69227c0b1a8SArnd Bergmann static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = { 69327c0b1a8SArnd Bergmann [IFLA_MACVLAN_MODE] = { .type = NLA_U32 }, 69427c0b1a8SArnd Bergmann }; 69527c0b1a8SArnd Bergmann 696fc0663d6SArnd Bergmann int macvlan_link_register(struct rtnl_link_ops *ops) 697fc0663d6SArnd Bergmann { 698fc0663d6SArnd Bergmann /* common fields */ 699fc0663d6SArnd Bergmann ops->priv_size = sizeof(struct macvlan_dev); 700fc0663d6SArnd Bergmann ops->get_tx_queues = macvlan_get_tx_queues; 701fc0663d6SArnd Bergmann ops->setup = macvlan_setup; 702fc0663d6SArnd Bergmann ops->validate = macvlan_validate; 703fc0663d6SArnd Bergmann ops->maxtype = IFLA_MACVLAN_MAX; 704fc0663d6SArnd Bergmann ops->policy = macvlan_policy; 705fc0663d6SArnd Bergmann ops->changelink = macvlan_changelink; 706fc0663d6SArnd Bergmann ops->get_size = macvlan_get_size; 707fc0663d6SArnd Bergmann ops->fill_info = macvlan_fill_info; 708fc0663d6SArnd Bergmann 709fc0663d6SArnd Bergmann return rtnl_link_register(ops); 710fc0663d6SArnd Bergmann }; 711fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_link_register); 712fc0663d6SArnd Bergmann 713fc0663d6SArnd Bergmann static struct rtnl_link_ops macvlan_link_ops = { 714b863ceb7SPatrick McHardy .kind = "macvlan", 715b863ceb7SPatrick McHardy .newlink = macvlan_newlink, 716b863ceb7SPatrick McHardy .dellink = macvlan_dellink, 717b863ceb7SPatrick McHardy }; 718b863ceb7SPatrick McHardy 719b863ceb7SPatrick McHardy static int macvlan_device_event(struct notifier_block *unused, 720b863ceb7SPatrick McHardy unsigned long event, void *ptr) 721b863ceb7SPatrick McHardy { 722b863ceb7SPatrick McHardy struct net_device *dev = ptr; 723b863ceb7SPatrick McHardy struct macvlan_dev *vlan, *next; 724b863ceb7SPatrick McHardy struct macvlan_port *port; 725b863ceb7SPatrick McHardy 726b863ceb7SPatrick McHardy port = dev->macvlan_port; 727b863ceb7SPatrick McHardy if (port == NULL) 728b863ceb7SPatrick McHardy return NOTIFY_DONE; 729b863ceb7SPatrick McHardy 730b863ceb7SPatrick McHardy switch (event) { 731b863ceb7SPatrick McHardy case NETDEV_CHANGE: 732b863ceb7SPatrick McHardy list_for_each_entry(vlan, &port->vlans, list) 733fc4a7489SPatrick Mullaney netif_stacked_transfer_operstate(vlan->lowerdev, 734fc4a7489SPatrick Mullaney vlan->dev); 735b863ceb7SPatrick McHardy break; 736b863ceb7SPatrick McHardy case NETDEV_FEAT_CHANGE: 737b863ceb7SPatrick McHardy list_for_each_entry(vlan, &port->vlans, list) { 738b863ceb7SPatrick McHardy vlan->dev->features = dev->features & MACVLAN_FEATURES; 7398c2acc53SPatrick McHardy vlan->dev->gso_max_size = dev->gso_max_size; 740b863ceb7SPatrick McHardy netdev_features_change(vlan->dev); 741b863ceb7SPatrick McHardy } 742b863ceb7SPatrick McHardy break; 743b863ceb7SPatrick McHardy case NETDEV_UNREGISTER: 744b863ceb7SPatrick McHardy list_for_each_entry_safe(vlan, next, &port->vlans, list) 745fc0663d6SArnd Bergmann vlan->dev->rtnl_link_ops->dellink(vlan->dev, NULL); 746b863ceb7SPatrick McHardy break; 7471c01fe14SJiri Pirko case NETDEV_PRE_TYPE_CHANGE: 7481c01fe14SJiri Pirko /* Forbid underlaying device to change its type. */ 7491c01fe14SJiri Pirko return NOTIFY_BAD; 750b863ceb7SPatrick McHardy } 751b863ceb7SPatrick McHardy return NOTIFY_DONE; 752b863ceb7SPatrick McHardy } 753b863ceb7SPatrick McHardy 754b863ceb7SPatrick McHardy static struct notifier_block macvlan_notifier_block __read_mostly = { 755b863ceb7SPatrick McHardy .notifier_call = macvlan_device_event, 756b863ceb7SPatrick McHardy }; 757b863ceb7SPatrick McHardy 758b863ceb7SPatrick McHardy static int __init macvlan_init_module(void) 759b863ceb7SPatrick McHardy { 760b863ceb7SPatrick McHardy int err; 761b863ceb7SPatrick McHardy 762b863ceb7SPatrick McHardy register_netdevice_notifier(&macvlan_notifier_block); 763b863ceb7SPatrick McHardy macvlan_handle_frame_hook = macvlan_handle_frame; 764b863ceb7SPatrick McHardy 765fc0663d6SArnd Bergmann err = macvlan_link_register(&macvlan_link_ops); 766b863ceb7SPatrick McHardy if (err < 0) 767b863ceb7SPatrick McHardy goto err1; 768b863ceb7SPatrick McHardy return 0; 769b863ceb7SPatrick McHardy err1: 77052913246SRami Rosen macvlan_handle_frame_hook = NULL; 771b863ceb7SPatrick McHardy unregister_netdevice_notifier(&macvlan_notifier_block); 772b863ceb7SPatrick McHardy return err; 773b863ceb7SPatrick McHardy } 774b863ceb7SPatrick McHardy 775b863ceb7SPatrick McHardy static void __exit macvlan_cleanup_module(void) 776b863ceb7SPatrick McHardy { 777b863ceb7SPatrick McHardy rtnl_link_unregister(&macvlan_link_ops); 778b863ceb7SPatrick McHardy macvlan_handle_frame_hook = NULL; 779b863ceb7SPatrick McHardy unregister_netdevice_notifier(&macvlan_notifier_block); 780b863ceb7SPatrick McHardy } 781b863ceb7SPatrick McHardy 782b863ceb7SPatrick McHardy module_init(macvlan_init_module); 783b863ceb7SPatrick McHardy module_exit(macvlan_cleanup_module); 784b863ceb7SPatrick McHardy 785b863ceb7SPatrick McHardy MODULE_LICENSE("GPL"); 786b863ceb7SPatrick McHardy MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); 787b863ceb7SPatrick McHardy MODULE_DESCRIPTION("Driver for MAC address based VLANs"); 788b863ceb7SPatrick McHardy MODULE_ALIAS_RTNL_LINK("macvlan"); 789