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 */ 148b863ceb7SPatrick McHardy static struct sk_buff *macvlan_handle_frame(struct sk_buff *skb) 149b863ceb7SPatrick McHardy { 150b863ceb7SPatrick McHardy const struct ethhdr *eth = eth_hdr(skb); 151b863ceb7SPatrick McHardy const struct macvlan_port *port; 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 port = rcu_dereference(skb->dev->macvlan_port); 158b863ceb7SPatrick McHardy if (port == NULL) 159b863ceb7SPatrick McHardy return skb; 160b863ceb7SPatrick McHardy 161b863ceb7SPatrick McHardy if (is_multicast_ether_addr(eth->h_dest)) { 162618e1b74SArnd Bergmann src = macvlan_hash_lookup(port, eth->h_source); 163618e1b74SArnd Bergmann if (!src) 164618e1b74SArnd Bergmann /* frame comes from an external address */ 165618e1b74SArnd Bergmann macvlan_broadcast(skb, port, NULL, 166618e1b74SArnd Bergmann MACVLAN_MODE_PRIVATE | 167618e1b74SArnd Bergmann MACVLAN_MODE_VEPA | 168618e1b74SArnd Bergmann MACVLAN_MODE_BRIDGE); 169618e1b74SArnd Bergmann else if (src->mode == MACVLAN_MODE_VEPA) 170618e1b74SArnd Bergmann /* flood to everyone except source */ 171618e1b74SArnd Bergmann macvlan_broadcast(skb, port, src->dev, 172618e1b74SArnd Bergmann MACVLAN_MODE_VEPA | 173618e1b74SArnd Bergmann MACVLAN_MODE_BRIDGE); 174618e1b74SArnd Bergmann else if (src->mode == MACVLAN_MODE_BRIDGE) 175618e1b74SArnd Bergmann /* 176618e1b74SArnd Bergmann * flood only to VEPA ports, bridge ports 177618e1b74SArnd Bergmann * already saw the frame on the way out. 178618e1b74SArnd Bergmann */ 179618e1b74SArnd Bergmann macvlan_broadcast(skb, port, src->dev, 180618e1b74SArnd Bergmann MACVLAN_MODE_VEPA); 181b863ceb7SPatrick McHardy return skb; 182b863ceb7SPatrick McHardy } 183b863ceb7SPatrick McHardy 184b863ceb7SPatrick McHardy vlan = macvlan_hash_lookup(port, eth->h_dest); 185b863ceb7SPatrick McHardy if (vlan == NULL) 186b863ceb7SPatrick McHardy return skb; 187b863ceb7SPatrick McHardy 188b863ceb7SPatrick McHardy dev = vlan->dev; 189b863ceb7SPatrick McHardy if (unlikely(!(dev->flags & IFF_UP))) { 190b863ceb7SPatrick McHardy kfree_skb(skb); 191b863ceb7SPatrick McHardy return NULL; 192b863ceb7SPatrick McHardy } 193a1e514c5SArnd Bergmann len = skb->len + ETH_HLEN; 194b863ceb7SPatrick McHardy skb = skb_share_check(skb, GFP_ATOMIC); 195a1e514c5SArnd Bergmann macvlan_count_rx(vlan, len, skb != NULL, 0); 196a1e514c5SArnd Bergmann if (!skb) 197b863ceb7SPatrick McHardy return NULL; 198b863ceb7SPatrick McHardy 199b863ceb7SPatrick McHardy skb->dev = dev; 200b863ceb7SPatrick McHardy skb->pkt_type = PACKET_HOST; 201b863ceb7SPatrick McHardy 202fc0663d6SArnd Bergmann vlan->receive(skb); 203b863ceb7SPatrick McHardy return NULL; 204b863ceb7SPatrick McHardy } 205b863ceb7SPatrick McHardy 206618e1b74SArnd Bergmann static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev) 207618e1b74SArnd Bergmann { 208618e1b74SArnd Bergmann const struct macvlan_dev *vlan = netdev_priv(dev); 209618e1b74SArnd Bergmann const struct macvlan_port *port = vlan->port; 210618e1b74SArnd Bergmann const struct macvlan_dev *dest; 211618e1b74SArnd Bergmann 212618e1b74SArnd Bergmann if (vlan->mode == MACVLAN_MODE_BRIDGE) { 213618e1b74SArnd Bergmann const struct ethhdr *eth = (void *)skb->data; 214618e1b74SArnd Bergmann 215618e1b74SArnd Bergmann /* send to other bridge ports directly */ 216618e1b74SArnd Bergmann if (is_multicast_ether_addr(eth->h_dest)) { 217618e1b74SArnd Bergmann macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE); 218618e1b74SArnd Bergmann goto xmit_world; 219618e1b74SArnd Bergmann } 220618e1b74SArnd Bergmann 221618e1b74SArnd Bergmann dest = macvlan_hash_lookup(port, eth->h_dest); 222618e1b74SArnd Bergmann if (dest && dest->mode == MACVLAN_MODE_BRIDGE) { 223618e1b74SArnd Bergmann unsigned int length = skb->len + ETH_HLEN; 224fc0663d6SArnd Bergmann int ret = dest->forward(dest->dev, skb); 225618e1b74SArnd Bergmann macvlan_count_rx(dest, length, 226618e1b74SArnd Bergmann ret == NET_RX_SUCCESS, 0); 227618e1b74SArnd Bergmann 228618e1b74SArnd Bergmann return NET_XMIT_SUCCESS; 229618e1b74SArnd Bergmann } 230618e1b74SArnd Bergmann } 231618e1b74SArnd Bergmann 232618e1b74SArnd Bergmann xmit_world: 2338a83a00bSArnd Bergmann skb_set_dev(skb, vlan->lowerdev); 234618e1b74SArnd Bergmann return dev_queue_xmit(skb); 235618e1b74SArnd Bergmann } 236618e1b74SArnd Bergmann 237fc0663d6SArnd Bergmann netdev_tx_t macvlan_start_xmit(struct sk_buff *skb, 238424efe9cSStephen Hemminger struct net_device *dev) 239b863ceb7SPatrick McHardy { 2402c114553SEric Dumazet int i = skb_get_queue_mapping(skb); 2412c114553SEric Dumazet struct netdev_queue *txq = netdev_get_tx_queue(dev, i); 242b863ceb7SPatrick McHardy unsigned int len = skb->len; 243b863ceb7SPatrick McHardy int ret; 244b863ceb7SPatrick McHardy 245618e1b74SArnd Bergmann ret = macvlan_queue_xmit(skb, dev); 246b863ceb7SPatrick McHardy if (likely(ret == NET_XMIT_SUCCESS)) { 2472c114553SEric Dumazet txq->tx_packets++; 2482c114553SEric Dumazet txq->tx_bytes += len; 2492c114553SEric Dumazet } else 2502c114553SEric Dumazet txq->tx_dropped++; 2512c114553SEric Dumazet 252cbbef5e1SPatrick McHardy return ret; 253b863ceb7SPatrick McHardy } 254fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_start_xmit); 255b863ceb7SPatrick McHardy 256b863ceb7SPatrick McHardy static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev, 2573b04dddeSStephen Hemminger unsigned short type, const void *daddr, 2583b04dddeSStephen Hemminger const void *saddr, unsigned len) 259b863ceb7SPatrick McHardy { 260b863ceb7SPatrick McHardy const struct macvlan_dev *vlan = netdev_priv(dev); 261b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 262b863ceb7SPatrick McHardy 2630c4e8581SStephen Hemminger return dev_hard_header(skb, lowerdev, type, daddr, 264b863ceb7SPatrick McHardy saddr ? : dev->dev_addr, len); 265b863ceb7SPatrick McHardy } 266b863ceb7SPatrick McHardy 2673b04dddeSStephen Hemminger static const struct header_ops macvlan_hard_header_ops = { 2683b04dddeSStephen Hemminger .create = macvlan_hard_header, 2693b04dddeSStephen Hemminger .rebuild = eth_rebuild_header, 2703b04dddeSStephen Hemminger .parse = eth_header_parse, 2713b04dddeSStephen Hemminger .cache = eth_header_cache, 2723b04dddeSStephen Hemminger .cache_update = eth_header_cache_update, 2733b04dddeSStephen Hemminger }; 2743b04dddeSStephen Hemminger 275b863ceb7SPatrick McHardy static int macvlan_open(struct net_device *dev) 276b863ceb7SPatrick McHardy { 277b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 278b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 279b863ceb7SPatrick McHardy int err; 280b863ceb7SPatrick McHardy 281f9ac30f0SEric Biederman err = -EBUSY; 282f9ac30f0SEric Biederman if (macvlan_addr_busy(vlan->port, dev->dev_addr)) 283f9ac30f0SEric Biederman goto out; 284f9ac30f0SEric Biederman 285*a748ee24SJiri Pirko err = dev_uc_add(lowerdev, dev->dev_addr); 286b863ceb7SPatrick McHardy if (err < 0) 287b89fb7daSWang Chen goto out; 288b89fb7daSWang Chen if (dev->flags & IFF_ALLMULTI) { 289b89fb7daSWang Chen err = dev_set_allmulti(lowerdev, 1); 290b89fb7daSWang Chen if (err < 0) 291b89fb7daSWang Chen goto del_unicast; 292b89fb7daSWang Chen } 293f9ac30f0SEric Biederman macvlan_hash_add(vlan); 294b863ceb7SPatrick McHardy return 0; 295b89fb7daSWang Chen 296b89fb7daSWang Chen del_unicast: 297*a748ee24SJiri Pirko dev_uc_del(lowerdev, dev->dev_addr); 298b89fb7daSWang Chen out: 299b89fb7daSWang Chen return err; 300b863ceb7SPatrick McHardy } 301b863ceb7SPatrick McHardy 302b863ceb7SPatrick McHardy static int macvlan_stop(struct net_device *dev) 303b863ceb7SPatrick McHardy { 304b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 305b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 306b863ceb7SPatrick McHardy 307b863ceb7SPatrick McHardy dev_mc_unsync(lowerdev, dev); 308b863ceb7SPatrick McHardy if (dev->flags & IFF_ALLMULTI) 309b863ceb7SPatrick McHardy dev_set_allmulti(lowerdev, -1); 310b863ceb7SPatrick McHardy 311*a748ee24SJiri Pirko dev_uc_del(lowerdev, dev->dev_addr); 312b863ceb7SPatrick McHardy 313f9ac30f0SEric Biederman macvlan_hash_del(vlan); 314b863ceb7SPatrick McHardy return 0; 315b863ceb7SPatrick McHardy } 316b863ceb7SPatrick McHardy 317ad5d20a6SPatrick McHardy static int macvlan_set_mac_address(struct net_device *dev, void *p) 318ad5d20a6SPatrick McHardy { 319ad5d20a6SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 320ad5d20a6SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 321ad5d20a6SPatrick McHardy struct sockaddr *addr = p; 322ad5d20a6SPatrick McHardy int err; 323ad5d20a6SPatrick McHardy 324ad5d20a6SPatrick McHardy if (!is_valid_ether_addr(addr->sa_data)) 325ad5d20a6SPatrick McHardy return -EADDRNOTAVAIL; 326ad5d20a6SPatrick McHardy 327f9ac30f0SEric Biederman if (!(dev->flags & IFF_UP)) { 328f9ac30f0SEric Biederman /* Just copy in the new address */ 329f9ac30f0SEric Biederman memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN); 330f9ac30f0SEric Biederman } else { 331f9ac30f0SEric Biederman /* Rehash and update the device filters */ 332f9ac30f0SEric Biederman if (macvlan_addr_busy(vlan->port, addr->sa_data)) 333f9ac30f0SEric Biederman return -EBUSY; 334ad5d20a6SPatrick McHardy 335*a748ee24SJiri Pirko err = dev_uc_add(lowerdev, addr->sa_data); 336ccffad25SJiri Pirko if (err) 337ad5d20a6SPatrick McHardy return err; 338f9ac30f0SEric Biederman 339*a748ee24SJiri Pirko dev_uc_del(lowerdev, dev->dev_addr); 340ad5d20a6SPatrick McHardy 341f9ac30f0SEric Biederman macvlan_hash_change_addr(vlan, addr->sa_data); 342f9ac30f0SEric Biederman } 343ad5d20a6SPatrick McHardy return 0; 344ad5d20a6SPatrick McHardy } 345ad5d20a6SPatrick McHardy 346b863ceb7SPatrick McHardy static void macvlan_change_rx_flags(struct net_device *dev, int change) 347b863ceb7SPatrick McHardy { 348b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 349b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 350b863ceb7SPatrick McHardy 351b863ceb7SPatrick McHardy if (change & IFF_ALLMULTI) 352b863ceb7SPatrick McHardy dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1); 353b863ceb7SPatrick McHardy } 354b863ceb7SPatrick McHardy 355b863ceb7SPatrick McHardy static void macvlan_set_multicast_list(struct net_device *dev) 356b863ceb7SPatrick McHardy { 357b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 358b863ceb7SPatrick McHardy 359b863ceb7SPatrick McHardy dev_mc_sync(vlan->lowerdev, dev); 360b863ceb7SPatrick McHardy } 361b863ceb7SPatrick McHardy 362b863ceb7SPatrick McHardy static int macvlan_change_mtu(struct net_device *dev, int new_mtu) 363b863ceb7SPatrick McHardy { 364b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 365b863ceb7SPatrick McHardy 366b863ceb7SPatrick McHardy if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu) 367b863ceb7SPatrick McHardy return -EINVAL; 368b863ceb7SPatrick McHardy dev->mtu = new_mtu; 369b863ceb7SPatrick McHardy return 0; 370b863ceb7SPatrick McHardy } 371b863ceb7SPatrick McHardy 372b863ceb7SPatrick McHardy /* 373b863ceb7SPatrick McHardy * macvlan network devices have devices nesting below it and are a special 374b863ceb7SPatrick McHardy * "super class" of normal network devices; split their locks off into a 375b863ceb7SPatrick McHardy * separate class since they always nest. 376b863ceb7SPatrick McHardy */ 377b863ceb7SPatrick McHardy static struct lock_class_key macvlan_netdev_xmit_lock_key; 378cf508b12SDavid S. Miller static struct lock_class_key macvlan_netdev_addr_lock_key; 379b863ceb7SPatrick McHardy 380b863ceb7SPatrick McHardy #define MACVLAN_FEATURES \ 381b863ceb7SPatrick McHardy (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \ 382b863ceb7SPatrick McHardy NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \ 3836eb3a855SPatrick Mullaney NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO) 384b863ceb7SPatrick McHardy 385b863ceb7SPatrick McHardy #define MACVLAN_STATE_MASK \ 386b863ceb7SPatrick McHardy ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT)) 387b863ceb7SPatrick McHardy 388e8a0464cSDavid S. Miller static void macvlan_set_lockdep_class_one(struct net_device *dev, 389e8a0464cSDavid S. Miller struct netdev_queue *txq, 390e8a0464cSDavid S. Miller void *_unused) 391c773e847SDavid S. Miller { 392c773e847SDavid S. Miller lockdep_set_class(&txq->_xmit_lock, 393c773e847SDavid S. Miller &macvlan_netdev_xmit_lock_key); 394c773e847SDavid S. Miller } 395c773e847SDavid S. Miller 396c773e847SDavid S. Miller static void macvlan_set_lockdep_class(struct net_device *dev) 397c773e847SDavid S. Miller { 398cf508b12SDavid S. Miller lockdep_set_class(&dev->addr_list_lock, 399cf508b12SDavid S. Miller &macvlan_netdev_addr_lock_key); 400e8a0464cSDavid S. Miller netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL); 401c773e847SDavid S. Miller } 402c773e847SDavid S. Miller 403b863ceb7SPatrick McHardy static int macvlan_init(struct net_device *dev) 404b863ceb7SPatrick McHardy { 405b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 406b863ceb7SPatrick McHardy const struct net_device *lowerdev = vlan->lowerdev; 407b863ceb7SPatrick McHardy 408b863ceb7SPatrick McHardy dev->state = (dev->state & ~MACVLAN_STATE_MASK) | 409b863ceb7SPatrick McHardy (lowerdev->state & MACVLAN_STATE_MASK); 410b863ceb7SPatrick McHardy dev->features = lowerdev->features & MACVLAN_FEATURES; 4118c2acc53SPatrick McHardy dev->gso_max_size = lowerdev->gso_max_size; 412b863ceb7SPatrick McHardy dev->iflink = lowerdev->ifindex; 413ef5c8996Ssg.tweak@gmail.com dev->hard_header_len = lowerdev->hard_header_len; 414b863ceb7SPatrick McHardy 415c773e847SDavid S. Miller macvlan_set_lockdep_class(dev); 416c773e847SDavid S. Miller 417fccaf710SEric Dumazet vlan->rx_stats = alloc_percpu(struct macvlan_rx_stats); 418fccaf710SEric Dumazet if (!vlan->rx_stats) 419fccaf710SEric Dumazet return -ENOMEM; 420fccaf710SEric Dumazet 421b863ceb7SPatrick McHardy return 0; 422b863ceb7SPatrick McHardy } 423b863ceb7SPatrick McHardy 424fccaf710SEric Dumazet static void macvlan_uninit(struct net_device *dev) 425fccaf710SEric Dumazet { 426fccaf710SEric Dumazet struct macvlan_dev *vlan = netdev_priv(dev); 427fccaf710SEric Dumazet 428fccaf710SEric Dumazet free_percpu(vlan->rx_stats); 429fccaf710SEric Dumazet } 430fccaf710SEric Dumazet 431fccaf710SEric Dumazet static struct net_device_stats *macvlan_dev_get_stats(struct net_device *dev) 432fccaf710SEric Dumazet { 433fccaf710SEric Dumazet struct net_device_stats *stats = &dev->stats; 434fccaf710SEric Dumazet struct macvlan_dev *vlan = netdev_priv(dev); 435fccaf710SEric Dumazet 436fccaf710SEric Dumazet dev_txq_stats_fold(dev, stats); 437fccaf710SEric Dumazet 438fccaf710SEric Dumazet if (vlan->rx_stats) { 439fccaf710SEric Dumazet struct macvlan_rx_stats *p, rx = {0}; 440fccaf710SEric Dumazet int i; 441fccaf710SEric Dumazet 442fccaf710SEric Dumazet for_each_possible_cpu(i) { 443fccaf710SEric Dumazet p = per_cpu_ptr(vlan->rx_stats, i); 444fccaf710SEric Dumazet rx.rx_packets += p->rx_packets; 445fccaf710SEric Dumazet rx.rx_bytes += p->rx_bytes; 446fccaf710SEric Dumazet rx.rx_errors += p->rx_errors; 447fccaf710SEric Dumazet rx.multicast += p->multicast; 448fccaf710SEric Dumazet } 449fccaf710SEric Dumazet stats->rx_packets = rx.rx_packets; 450fccaf710SEric Dumazet stats->rx_bytes = rx.rx_bytes; 451fccaf710SEric Dumazet stats->rx_errors = rx.rx_errors; 452fccaf710SEric Dumazet stats->rx_dropped = rx.rx_errors; 453fccaf710SEric Dumazet stats->multicast = rx.multicast; 454fccaf710SEric Dumazet } 455fccaf710SEric Dumazet return stats; 456fccaf710SEric Dumazet } 457fccaf710SEric Dumazet 458b863ceb7SPatrick McHardy static void macvlan_ethtool_get_drvinfo(struct net_device *dev, 459b863ceb7SPatrick McHardy struct ethtool_drvinfo *drvinfo) 460b863ceb7SPatrick McHardy { 461b863ceb7SPatrick McHardy snprintf(drvinfo->driver, 32, "macvlan"); 462b863ceb7SPatrick McHardy snprintf(drvinfo->version, 32, "0.1"); 463b863ceb7SPatrick McHardy } 464b863ceb7SPatrick McHardy 465b863ceb7SPatrick McHardy static u32 macvlan_ethtool_get_rx_csum(struct net_device *dev) 466b863ceb7SPatrick McHardy { 467b863ceb7SPatrick McHardy const struct macvlan_dev *vlan = netdev_priv(dev); 468b1b67dd4SPatrick McHardy return dev_ethtool_get_rx_csum(vlan->lowerdev); 469b863ceb7SPatrick McHardy } 470b863ceb7SPatrick McHardy 4719edb8bb6SStephen Hemminger static int macvlan_ethtool_get_settings(struct net_device *dev, 4729edb8bb6SStephen Hemminger struct ethtool_cmd *cmd) 4739edb8bb6SStephen Hemminger { 4749edb8bb6SStephen Hemminger const struct macvlan_dev *vlan = netdev_priv(dev); 475b1b67dd4SPatrick McHardy return dev_ethtool_get_settings(vlan->lowerdev, cmd); 4769edb8bb6SStephen Hemminger } 4779edb8bb6SStephen Hemminger 4789edb8bb6SStephen Hemminger static u32 macvlan_ethtool_get_flags(struct net_device *dev) 4799edb8bb6SStephen Hemminger { 4809edb8bb6SStephen Hemminger const struct macvlan_dev *vlan = netdev_priv(dev); 481b1b67dd4SPatrick McHardy return dev_ethtool_get_flags(vlan->lowerdev); 4829edb8bb6SStephen Hemminger } 4839edb8bb6SStephen Hemminger 484b863ceb7SPatrick McHardy static const struct ethtool_ops macvlan_ethtool_ops = { 485b863ceb7SPatrick McHardy .get_link = ethtool_op_get_link, 4869edb8bb6SStephen Hemminger .get_settings = macvlan_ethtool_get_settings, 487b863ceb7SPatrick McHardy .get_rx_csum = macvlan_ethtool_get_rx_csum, 488b863ceb7SPatrick McHardy .get_drvinfo = macvlan_ethtool_get_drvinfo, 4899edb8bb6SStephen Hemminger .get_flags = macvlan_ethtool_get_flags, 490b863ceb7SPatrick McHardy }; 491b863ceb7SPatrick McHardy 49254a30c97SStephen Hemminger static const struct net_device_ops macvlan_netdev_ops = { 49354a30c97SStephen Hemminger .ndo_init = macvlan_init, 494fccaf710SEric Dumazet .ndo_uninit = macvlan_uninit, 49554a30c97SStephen Hemminger .ndo_open = macvlan_open, 49654a30c97SStephen Hemminger .ndo_stop = macvlan_stop, 49700829823SStephen Hemminger .ndo_start_xmit = macvlan_start_xmit, 49854a30c97SStephen Hemminger .ndo_change_mtu = macvlan_change_mtu, 49954a30c97SStephen Hemminger .ndo_change_rx_flags = macvlan_change_rx_flags, 50054a30c97SStephen Hemminger .ndo_set_mac_address = macvlan_set_mac_address, 50154a30c97SStephen Hemminger .ndo_set_multicast_list = macvlan_set_multicast_list, 502fccaf710SEric Dumazet .ndo_get_stats = macvlan_dev_get_stats, 50354a30c97SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 50454a30c97SStephen Hemminger }; 50554a30c97SStephen Hemminger 506b863ceb7SPatrick McHardy static void macvlan_setup(struct net_device *dev) 507b863ceb7SPatrick McHardy { 508b863ceb7SPatrick McHardy ether_setup(dev); 509b863ceb7SPatrick McHardy 51093f154b5SEric Dumazet dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; 51154a30c97SStephen Hemminger dev->netdev_ops = &macvlan_netdev_ops; 512b863ceb7SPatrick McHardy dev->destructor = free_netdev; 5133b04dddeSStephen Hemminger dev->header_ops = &macvlan_hard_header_ops, 514b863ceb7SPatrick McHardy dev->ethtool_ops = &macvlan_ethtool_ops; 515b863ceb7SPatrick McHardy dev->tx_queue_len = 0; 516b863ceb7SPatrick McHardy } 517b863ceb7SPatrick McHardy 518b863ceb7SPatrick McHardy static int macvlan_port_create(struct net_device *dev) 519b863ceb7SPatrick McHardy { 520b863ceb7SPatrick McHardy struct macvlan_port *port; 521b863ceb7SPatrick McHardy unsigned int i; 522b863ceb7SPatrick McHardy 523b863ceb7SPatrick McHardy if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK) 524b863ceb7SPatrick McHardy return -EINVAL; 525b863ceb7SPatrick McHardy 526b863ceb7SPatrick McHardy port = kzalloc(sizeof(*port), GFP_KERNEL); 527b863ceb7SPatrick McHardy if (port == NULL) 528b863ceb7SPatrick McHardy return -ENOMEM; 529b863ceb7SPatrick McHardy 530b863ceb7SPatrick McHardy port->dev = dev; 531b863ceb7SPatrick McHardy INIT_LIST_HEAD(&port->vlans); 532b863ceb7SPatrick McHardy for (i = 0; i < MACVLAN_HASH_SIZE; i++) 533b863ceb7SPatrick McHardy INIT_HLIST_HEAD(&port->vlan_hash[i]); 534b863ceb7SPatrick McHardy rcu_assign_pointer(dev->macvlan_port, port); 535b863ceb7SPatrick McHardy return 0; 536b863ceb7SPatrick McHardy } 537b863ceb7SPatrick McHardy 538b863ceb7SPatrick McHardy static void macvlan_port_destroy(struct net_device *dev) 539b863ceb7SPatrick McHardy { 540b863ceb7SPatrick McHardy struct macvlan_port *port = dev->macvlan_port; 541b863ceb7SPatrick McHardy 542b863ceb7SPatrick McHardy rcu_assign_pointer(dev->macvlan_port, NULL); 543b863ceb7SPatrick McHardy synchronize_rcu(); 544b863ceb7SPatrick McHardy kfree(port); 545b863ceb7SPatrick McHardy } 546b863ceb7SPatrick McHardy 547b863ceb7SPatrick McHardy static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[]) 548b863ceb7SPatrick McHardy { 549b863ceb7SPatrick McHardy if (tb[IFLA_ADDRESS]) { 550b863ceb7SPatrick McHardy if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) 551b863ceb7SPatrick McHardy return -EINVAL; 552b863ceb7SPatrick McHardy if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) 553b863ceb7SPatrick McHardy return -EADDRNOTAVAIL; 554b863ceb7SPatrick McHardy } 55527c0b1a8SArnd Bergmann 55627c0b1a8SArnd Bergmann if (data && data[IFLA_MACVLAN_MODE]) { 55727c0b1a8SArnd Bergmann switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) { 55827c0b1a8SArnd Bergmann case MACVLAN_MODE_PRIVATE: 55927c0b1a8SArnd Bergmann case MACVLAN_MODE_VEPA: 56027c0b1a8SArnd Bergmann case MACVLAN_MODE_BRIDGE: 56127c0b1a8SArnd Bergmann break; 56227c0b1a8SArnd Bergmann default: 56327c0b1a8SArnd Bergmann return -EINVAL; 56427c0b1a8SArnd Bergmann } 56527c0b1a8SArnd Bergmann } 566b863ceb7SPatrick McHardy return 0; 567b863ceb7SPatrick McHardy } 568b863ceb7SPatrick McHardy 5692c114553SEric Dumazet static int macvlan_get_tx_queues(struct net *net, 5702c114553SEric Dumazet struct nlattr *tb[], 5712c114553SEric Dumazet unsigned int *num_tx_queues, 5722c114553SEric Dumazet unsigned int *real_num_tx_queues) 5732c114553SEric Dumazet { 5742c114553SEric Dumazet struct net_device *real_dev; 5752c114553SEric Dumazet 5762c114553SEric Dumazet if (!tb[IFLA_LINK]) 5772c114553SEric Dumazet return -EINVAL; 5782c114553SEric Dumazet 5792c114553SEric Dumazet real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK])); 5802c114553SEric Dumazet if (!real_dev) 5812c114553SEric Dumazet return -ENODEV; 5822c114553SEric Dumazet 5832c114553SEric Dumazet *num_tx_queues = real_dev->num_tx_queues; 5842c114553SEric Dumazet *real_num_tx_queues = real_dev->real_num_tx_queues; 5852c114553SEric Dumazet return 0; 5862c114553SEric Dumazet } 5872c114553SEric Dumazet 588fc0663d6SArnd Bergmann int macvlan_common_newlink(struct net *src_net, struct net_device *dev, 589fc0663d6SArnd Bergmann struct nlattr *tb[], struct nlattr *data[], 590fc0663d6SArnd Bergmann int (*receive)(struct sk_buff *skb), 591fc0663d6SArnd Bergmann int (*forward)(struct net_device *dev, 592fc0663d6SArnd Bergmann struct sk_buff *skb)) 593b863ceb7SPatrick McHardy { 594b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 595b863ceb7SPatrick McHardy struct macvlan_port *port; 596b863ceb7SPatrick McHardy struct net_device *lowerdev; 597b863ceb7SPatrick McHardy int err; 598b863ceb7SPatrick McHardy 599b863ceb7SPatrick McHardy if (!tb[IFLA_LINK]) 600b863ceb7SPatrick McHardy return -EINVAL; 601b863ceb7SPatrick McHardy 60281adee47SEric W. Biederman lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK])); 603b863ceb7SPatrick McHardy if (lowerdev == NULL) 604b863ceb7SPatrick McHardy return -ENODEV; 605b863ceb7SPatrick McHardy 606b0832a29SEric Biederman /* When creating macvlans on top of other macvlans - use 607b0832a29SEric Biederman * the real device as the lowerdev. 608a6ca5f1dSPatrick McHardy */ 609b0832a29SEric Biederman if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) { 610b0832a29SEric Biederman struct macvlan_dev *lowervlan = netdev_priv(lowerdev); 611b0832a29SEric Biederman lowerdev = lowervlan->lowerdev; 612b0832a29SEric Biederman } 613a6ca5f1dSPatrick McHardy 614b863ceb7SPatrick McHardy if (!tb[IFLA_MTU]) 615b863ceb7SPatrick McHardy dev->mtu = lowerdev->mtu; 616b863ceb7SPatrick McHardy else if (dev->mtu > lowerdev->mtu) 617b863ceb7SPatrick McHardy return -EINVAL; 618b863ceb7SPatrick McHardy 619b863ceb7SPatrick McHardy if (!tb[IFLA_ADDRESS]) 620b863ceb7SPatrick McHardy random_ether_addr(dev->dev_addr); 621b863ceb7SPatrick McHardy 622b863ceb7SPatrick McHardy if (lowerdev->macvlan_port == NULL) { 623b863ceb7SPatrick McHardy err = macvlan_port_create(lowerdev); 624b863ceb7SPatrick McHardy if (err < 0) 625b863ceb7SPatrick McHardy return err; 626b863ceb7SPatrick McHardy } 627b863ceb7SPatrick McHardy port = lowerdev->macvlan_port; 628b863ceb7SPatrick McHardy 629b863ceb7SPatrick McHardy vlan->lowerdev = lowerdev; 630b863ceb7SPatrick McHardy vlan->dev = dev; 631b863ceb7SPatrick McHardy vlan->port = port; 632fc0663d6SArnd Bergmann vlan->receive = receive; 633fc0663d6SArnd Bergmann vlan->forward = forward; 634b863ceb7SPatrick McHardy 63527c0b1a8SArnd Bergmann vlan->mode = MACVLAN_MODE_VEPA; 63627c0b1a8SArnd Bergmann if (data && data[IFLA_MACVLAN_MODE]) 63727c0b1a8SArnd Bergmann vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]); 63827c0b1a8SArnd Bergmann 639b863ceb7SPatrick McHardy err = register_netdevice(dev); 640b863ceb7SPatrick McHardy if (err < 0) 641b863ceb7SPatrick McHardy return err; 642b863ceb7SPatrick McHardy 643b863ceb7SPatrick McHardy list_add_tail(&vlan->list, &port->vlans); 644fc4a7489SPatrick Mullaney netif_stacked_transfer_operstate(lowerdev, dev); 645b863ceb7SPatrick McHardy return 0; 646b863ceb7SPatrick McHardy } 647fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_common_newlink); 648b863ceb7SPatrick McHardy 649fc0663d6SArnd Bergmann static int macvlan_newlink(struct net *src_net, struct net_device *dev, 650fc0663d6SArnd Bergmann struct nlattr *tb[], struct nlattr *data[]) 651fc0663d6SArnd Bergmann { 652fc0663d6SArnd Bergmann return macvlan_common_newlink(src_net, dev, tb, data, 653fc0663d6SArnd Bergmann netif_rx, 654fc0663d6SArnd Bergmann dev_forward_skb); 655fc0663d6SArnd Bergmann } 656fc0663d6SArnd Bergmann 657fc0663d6SArnd Bergmann void macvlan_dellink(struct net_device *dev, struct list_head *head) 658b863ceb7SPatrick McHardy { 659b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 660b863ceb7SPatrick McHardy struct macvlan_port *port = vlan->port; 661b863ceb7SPatrick McHardy 662b863ceb7SPatrick McHardy list_del(&vlan->list); 66323289a37SEric Dumazet unregister_netdevice_queue(dev, head); 664b863ceb7SPatrick McHardy 665b863ceb7SPatrick McHardy if (list_empty(&port->vlans)) 66673120964SPatrick McHardy macvlan_port_destroy(port->dev); 667b863ceb7SPatrick McHardy } 668fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_dellink); 669b863ceb7SPatrick McHardy 67027c0b1a8SArnd Bergmann static int macvlan_changelink(struct net_device *dev, 67127c0b1a8SArnd Bergmann struct nlattr *tb[], struct nlattr *data[]) 67227c0b1a8SArnd Bergmann { 67327c0b1a8SArnd Bergmann struct macvlan_dev *vlan = netdev_priv(dev); 67427c0b1a8SArnd Bergmann if (data && data[IFLA_MACVLAN_MODE]) 67527c0b1a8SArnd Bergmann vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]); 67627c0b1a8SArnd Bergmann return 0; 67727c0b1a8SArnd Bergmann } 67827c0b1a8SArnd Bergmann 67927c0b1a8SArnd Bergmann static size_t macvlan_get_size(const struct net_device *dev) 68027c0b1a8SArnd Bergmann { 68127c0b1a8SArnd Bergmann return nla_total_size(4); 68227c0b1a8SArnd Bergmann } 68327c0b1a8SArnd Bergmann 68427c0b1a8SArnd Bergmann static int macvlan_fill_info(struct sk_buff *skb, 68527c0b1a8SArnd Bergmann const struct net_device *dev) 68627c0b1a8SArnd Bergmann { 68727c0b1a8SArnd Bergmann struct macvlan_dev *vlan = netdev_priv(dev); 68827c0b1a8SArnd Bergmann 68927c0b1a8SArnd Bergmann NLA_PUT_U32(skb, IFLA_MACVLAN_MODE, vlan->mode); 69027c0b1a8SArnd Bergmann return 0; 69127c0b1a8SArnd Bergmann 69227c0b1a8SArnd Bergmann nla_put_failure: 69327c0b1a8SArnd Bergmann return -EMSGSIZE; 69427c0b1a8SArnd Bergmann } 69527c0b1a8SArnd Bergmann 69627c0b1a8SArnd Bergmann static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = { 69727c0b1a8SArnd Bergmann [IFLA_MACVLAN_MODE] = { .type = NLA_U32 }, 69827c0b1a8SArnd Bergmann }; 69927c0b1a8SArnd Bergmann 700fc0663d6SArnd Bergmann int macvlan_link_register(struct rtnl_link_ops *ops) 701fc0663d6SArnd Bergmann { 702fc0663d6SArnd Bergmann /* common fields */ 703fc0663d6SArnd Bergmann ops->priv_size = sizeof(struct macvlan_dev); 704fc0663d6SArnd Bergmann ops->get_tx_queues = macvlan_get_tx_queues; 705fc0663d6SArnd Bergmann ops->setup = macvlan_setup; 706fc0663d6SArnd Bergmann ops->validate = macvlan_validate; 707fc0663d6SArnd Bergmann ops->maxtype = IFLA_MACVLAN_MAX; 708fc0663d6SArnd Bergmann ops->policy = macvlan_policy; 709fc0663d6SArnd Bergmann ops->changelink = macvlan_changelink; 710fc0663d6SArnd Bergmann ops->get_size = macvlan_get_size; 711fc0663d6SArnd Bergmann ops->fill_info = macvlan_fill_info; 712fc0663d6SArnd Bergmann 713fc0663d6SArnd Bergmann return rtnl_link_register(ops); 714fc0663d6SArnd Bergmann }; 715fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_link_register); 716fc0663d6SArnd Bergmann 717fc0663d6SArnd Bergmann static struct rtnl_link_ops macvlan_link_ops = { 718b863ceb7SPatrick McHardy .kind = "macvlan", 719b863ceb7SPatrick McHardy .newlink = macvlan_newlink, 720b863ceb7SPatrick McHardy .dellink = macvlan_dellink, 721b863ceb7SPatrick McHardy }; 722b863ceb7SPatrick McHardy 723b863ceb7SPatrick McHardy static int macvlan_device_event(struct notifier_block *unused, 724b863ceb7SPatrick McHardy unsigned long event, void *ptr) 725b863ceb7SPatrick McHardy { 726b863ceb7SPatrick McHardy struct net_device *dev = ptr; 727b863ceb7SPatrick McHardy struct macvlan_dev *vlan, *next; 728b863ceb7SPatrick McHardy struct macvlan_port *port; 729b863ceb7SPatrick McHardy 730b863ceb7SPatrick McHardy port = dev->macvlan_port; 731b863ceb7SPatrick McHardy if (port == NULL) 732b863ceb7SPatrick McHardy return NOTIFY_DONE; 733b863ceb7SPatrick McHardy 734b863ceb7SPatrick McHardy switch (event) { 735b863ceb7SPatrick McHardy case NETDEV_CHANGE: 736b863ceb7SPatrick McHardy list_for_each_entry(vlan, &port->vlans, list) 737fc4a7489SPatrick Mullaney netif_stacked_transfer_operstate(vlan->lowerdev, 738fc4a7489SPatrick Mullaney vlan->dev); 739b863ceb7SPatrick McHardy break; 740b863ceb7SPatrick McHardy case NETDEV_FEAT_CHANGE: 741b863ceb7SPatrick McHardy list_for_each_entry(vlan, &port->vlans, list) { 742b863ceb7SPatrick McHardy vlan->dev->features = dev->features & MACVLAN_FEATURES; 7438c2acc53SPatrick McHardy vlan->dev->gso_max_size = dev->gso_max_size; 744b863ceb7SPatrick McHardy netdev_features_change(vlan->dev); 745b863ceb7SPatrick McHardy } 746b863ceb7SPatrick McHardy break; 747b863ceb7SPatrick McHardy case NETDEV_UNREGISTER: 748b863ceb7SPatrick McHardy list_for_each_entry_safe(vlan, next, &port->vlans, list) 749fc0663d6SArnd Bergmann vlan->dev->rtnl_link_ops->dellink(vlan->dev, NULL); 750b863ceb7SPatrick McHardy break; 7511c01fe14SJiri Pirko case NETDEV_PRE_TYPE_CHANGE: 7521c01fe14SJiri Pirko /* Forbid underlaying device to change its type. */ 7531c01fe14SJiri Pirko return NOTIFY_BAD; 754b863ceb7SPatrick McHardy } 755b863ceb7SPatrick McHardy return NOTIFY_DONE; 756b863ceb7SPatrick McHardy } 757b863ceb7SPatrick McHardy 758b863ceb7SPatrick McHardy static struct notifier_block macvlan_notifier_block __read_mostly = { 759b863ceb7SPatrick McHardy .notifier_call = macvlan_device_event, 760b863ceb7SPatrick McHardy }; 761b863ceb7SPatrick McHardy 762b863ceb7SPatrick McHardy static int __init macvlan_init_module(void) 763b863ceb7SPatrick McHardy { 764b863ceb7SPatrick McHardy int err; 765b863ceb7SPatrick McHardy 766b863ceb7SPatrick McHardy register_netdevice_notifier(&macvlan_notifier_block); 767b863ceb7SPatrick McHardy macvlan_handle_frame_hook = macvlan_handle_frame; 768b863ceb7SPatrick McHardy 769fc0663d6SArnd Bergmann err = macvlan_link_register(&macvlan_link_ops); 770b863ceb7SPatrick McHardy if (err < 0) 771b863ceb7SPatrick McHardy goto err1; 772b863ceb7SPatrick McHardy return 0; 773b863ceb7SPatrick McHardy err1: 77452913246SRami Rosen macvlan_handle_frame_hook = NULL; 775b863ceb7SPatrick McHardy unregister_netdevice_notifier(&macvlan_notifier_block); 776b863ceb7SPatrick McHardy return err; 777b863ceb7SPatrick McHardy } 778b863ceb7SPatrick McHardy 779b863ceb7SPatrick McHardy static void __exit macvlan_cleanup_module(void) 780b863ceb7SPatrick McHardy { 781b863ceb7SPatrick McHardy rtnl_link_unregister(&macvlan_link_ops); 782b863ceb7SPatrick McHardy macvlan_handle_frame_hook = NULL; 783b863ceb7SPatrick McHardy unregister_netdevice_notifier(&macvlan_notifier_block); 784b863ceb7SPatrick McHardy } 785b863ceb7SPatrick McHardy 786b863ceb7SPatrick McHardy module_init(macvlan_init_module); 787b863ceb7SPatrick McHardy module_exit(macvlan_cleanup_module); 788b863ceb7SPatrick McHardy 789b863ceb7SPatrick McHardy MODULE_LICENSE("GPL"); 790b863ceb7SPatrick McHardy MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); 791b863ceb7SPatrick McHardy MODULE_DESCRIPTION("Driver for MAC address based VLANs"); 792b863ceb7SPatrick McHardy MODULE_ALIAS_RTNL_LINK("macvlan"); 793