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> 2987002b03SJiri Pirko #include <linux/if_vlan.h> 30b863ceb7SPatrick McHardy #include <linux/if_link.h> 31b863ceb7SPatrick McHardy #include <linux/if_macvlan.h> 32b863ceb7SPatrick McHardy #include <net/rtnetlink.h> 33618e1b74SArnd Bergmann #include <net/xfrm.h> 34b863ceb7SPatrick McHardy 35b863ceb7SPatrick McHardy #define MACVLAN_HASH_SIZE (1 << BITS_PER_BYTE) 36b863ceb7SPatrick McHardy 37b863ceb7SPatrick McHardy struct macvlan_port { 38b863ceb7SPatrick McHardy struct net_device *dev; 39b863ceb7SPatrick McHardy struct hlist_head vlan_hash[MACVLAN_HASH_SIZE]; 40b863ceb7SPatrick McHardy struct list_head vlans; 418b37ef0aSJiri Pirko struct rcu_head rcu; 42eb06acdcSSridhar Samudrala bool passthru; 43d5cd9244SEric W. Biederman int count; 44b863ceb7SPatrick McHardy }; 45b863ceb7SPatrick McHardy 46d5cd9244SEric W. Biederman static void macvlan_port_destroy(struct net_device *dev); 47d5cd9244SEric W. Biederman 48a35e2c1bSJiri Pirko #define macvlan_port_get_rcu(dev) \ 49a35e2c1bSJiri Pirko ((struct macvlan_port *) rcu_dereference(dev->rx_handler_data)) 50a35e2c1bSJiri Pirko #define macvlan_port_get(dev) ((struct macvlan_port *) dev->rx_handler_data) 51a35e2c1bSJiri Pirko #define macvlan_port_exists(dev) (dev->priv_flags & IFF_MACVLAN_PORT) 52a35e2c1bSJiri Pirko 53b863ceb7SPatrick McHardy static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port, 54b863ceb7SPatrick McHardy const unsigned char *addr) 55b863ceb7SPatrick McHardy { 56b863ceb7SPatrick McHardy struct macvlan_dev *vlan; 57b863ceb7SPatrick McHardy struct hlist_node *n; 58b863ceb7SPatrick McHardy 59b863ceb7SPatrick McHardy hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[addr[5]], hlist) { 60ac06713dSEric Dumazet if (!compare_ether_addr_64bits(vlan->dev->dev_addr, addr)) 61b863ceb7SPatrick McHardy return vlan; 62b863ceb7SPatrick McHardy } 63b863ceb7SPatrick McHardy return NULL; 64b863ceb7SPatrick McHardy } 65b863ceb7SPatrick McHardy 66f9ac30f0SEric Biederman static void macvlan_hash_add(struct macvlan_dev *vlan) 67f9ac30f0SEric Biederman { 68f9ac30f0SEric Biederman struct macvlan_port *port = vlan->port; 69f9ac30f0SEric Biederman const unsigned char *addr = vlan->dev->dev_addr; 70f9ac30f0SEric Biederman 71f9ac30f0SEric Biederman hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]); 72f9ac30f0SEric Biederman } 73f9ac30f0SEric Biederman 74449f4544SEric Dumazet static void macvlan_hash_del(struct macvlan_dev *vlan, bool sync) 75f9ac30f0SEric Biederman { 76f9ac30f0SEric Biederman hlist_del_rcu(&vlan->hlist); 77449f4544SEric Dumazet if (sync) 78f9ac30f0SEric Biederman synchronize_rcu(); 79f9ac30f0SEric Biederman } 80f9ac30f0SEric Biederman 81f9ac30f0SEric Biederman static void macvlan_hash_change_addr(struct macvlan_dev *vlan, 82f9ac30f0SEric Biederman const unsigned char *addr) 83f9ac30f0SEric Biederman { 84449f4544SEric Dumazet macvlan_hash_del(vlan, true); 85f9ac30f0SEric Biederman /* Now that we are unhashed it is safe to change the device 86f9ac30f0SEric Biederman * address without confusing packet delivery. 87f9ac30f0SEric Biederman */ 88f9ac30f0SEric Biederman memcpy(vlan->dev->dev_addr, addr, ETH_ALEN); 89f9ac30f0SEric Biederman macvlan_hash_add(vlan); 90f9ac30f0SEric Biederman } 91f9ac30f0SEric Biederman 92f9ac30f0SEric Biederman static int macvlan_addr_busy(const struct macvlan_port *port, 93f9ac30f0SEric Biederman const unsigned char *addr) 94f9ac30f0SEric Biederman { 95f9ac30f0SEric Biederman /* Test to see if the specified multicast address is 96f9ac30f0SEric Biederman * currently in use by the underlying device or 97f9ac30f0SEric Biederman * another macvlan. 98f9ac30f0SEric Biederman */ 99ac06713dSEric Dumazet if (!compare_ether_addr_64bits(port->dev->dev_addr, addr)) 100f9ac30f0SEric Biederman return 1; 101f9ac30f0SEric Biederman 102f9ac30f0SEric Biederman if (macvlan_hash_lookup(port, addr)) 103f9ac30f0SEric Biederman return 1; 104f9ac30f0SEric Biederman 105f9ac30f0SEric Biederman return 0; 106f9ac30f0SEric Biederman } 107f9ac30f0SEric Biederman 108a1e514c5SArnd Bergmann 109fc0663d6SArnd Bergmann static int macvlan_broadcast_one(struct sk_buff *skb, 110fc0663d6SArnd Bergmann const struct macvlan_dev *vlan, 111618e1b74SArnd Bergmann const struct ethhdr *eth, bool local) 112a1e514c5SArnd Bergmann { 113fc0663d6SArnd Bergmann struct net_device *dev = vlan->dev; 114a1e514c5SArnd Bergmann if (!skb) 115a1e514c5SArnd Bergmann return NET_RX_DROP; 116a1e514c5SArnd Bergmann 117618e1b74SArnd Bergmann if (local) 118fc0663d6SArnd Bergmann return vlan->forward(dev, skb); 119618e1b74SArnd Bergmann 120a1e514c5SArnd Bergmann skb->dev = dev; 121a1e514c5SArnd Bergmann if (!compare_ether_addr_64bits(eth->h_dest, 122a1e514c5SArnd Bergmann dev->broadcast)) 123a1e514c5SArnd Bergmann skb->pkt_type = PACKET_BROADCAST; 124a1e514c5SArnd Bergmann else 125a1e514c5SArnd Bergmann skb->pkt_type = PACKET_MULTICAST; 126a1e514c5SArnd Bergmann 127fc0663d6SArnd Bergmann return vlan->receive(skb); 128a1e514c5SArnd Bergmann } 129a1e514c5SArnd Bergmann 130b863ceb7SPatrick McHardy static void macvlan_broadcast(struct sk_buff *skb, 131618e1b74SArnd Bergmann const struct macvlan_port *port, 132618e1b74SArnd Bergmann struct net_device *src, 133618e1b74SArnd Bergmann enum macvlan_mode mode) 134b863ceb7SPatrick McHardy { 135b863ceb7SPatrick McHardy const struct ethhdr *eth = eth_hdr(skb); 136b863ceb7SPatrick McHardy const struct macvlan_dev *vlan; 137b863ceb7SPatrick McHardy struct hlist_node *n; 138b863ceb7SPatrick McHardy struct sk_buff *nskb; 139b863ceb7SPatrick McHardy unsigned int i; 140a1e514c5SArnd Bergmann int err; 141b863ceb7SPatrick McHardy 142efbbced3SPatrick McHardy if (skb->protocol == htons(ETH_P_PAUSE)) 143efbbced3SPatrick McHardy return; 144efbbced3SPatrick McHardy 145b863ceb7SPatrick McHardy for (i = 0; i < MACVLAN_HASH_SIZE; i++) { 146b863ceb7SPatrick McHardy hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[i], hlist) { 147618e1b74SArnd Bergmann if (vlan->dev == src || !(vlan->mode & mode)) 148618e1b74SArnd Bergmann continue; 149618e1b74SArnd Bergmann 150b863ceb7SPatrick McHardy nskb = skb_clone(skb, GFP_ATOMIC); 151fc0663d6SArnd Bergmann err = macvlan_broadcast_one(nskb, vlan, eth, 152618e1b74SArnd Bergmann mode == MACVLAN_MODE_BRIDGE); 153a1e514c5SArnd Bergmann macvlan_count_rx(vlan, skb->len + ETH_HLEN, 154a1e514c5SArnd Bergmann err == NET_RX_SUCCESS, 1); 155b863ceb7SPatrick McHardy } 156b863ceb7SPatrick McHardy } 157b863ceb7SPatrick McHardy } 158b863ceb7SPatrick McHardy 159b863ceb7SPatrick McHardy /* called under rcu_read_lock() from netif_receive_skb */ 1608a4eb573SJiri Pirko static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb) 161b863ceb7SPatrick McHardy { 162ab95bfe0SJiri Pirko struct macvlan_port *port; 1638a4eb573SJiri Pirko struct sk_buff *skb = *pskb; 164b863ceb7SPatrick McHardy const struct ethhdr *eth = eth_hdr(skb); 165b863ceb7SPatrick McHardy const struct macvlan_dev *vlan; 166618e1b74SArnd Bergmann const struct macvlan_dev *src; 167b863ceb7SPatrick McHardy struct net_device *dev; 168ba01877fSSridhar Samudrala unsigned int len = 0; 169ba01877fSSridhar Samudrala int ret = NET_RX_DROP; 170b863ceb7SPatrick McHardy 171a35e2c1bSJiri Pirko port = macvlan_port_get_rcu(skb->dev); 172b863ceb7SPatrick McHardy if (is_multicast_ether_addr(eth->h_dest)) { 173bc416d97SEric Dumazet skb = ip_check_defrag(skb, IP_DEFRAG_MACVLAN); 174bc416d97SEric Dumazet if (!skb) 175bc416d97SEric Dumazet return RX_HANDLER_CONSUMED; 176*4ec7ac12SEric Dumazet eth = eth_hdr(skb); 177618e1b74SArnd Bergmann src = macvlan_hash_lookup(port, eth->h_source); 178618e1b74SArnd Bergmann if (!src) 179618e1b74SArnd Bergmann /* frame comes from an external address */ 180618e1b74SArnd Bergmann macvlan_broadcast(skb, port, NULL, 181618e1b74SArnd Bergmann MACVLAN_MODE_PRIVATE | 182618e1b74SArnd Bergmann MACVLAN_MODE_VEPA | 183eb06acdcSSridhar Samudrala MACVLAN_MODE_PASSTHRU| 184618e1b74SArnd Bergmann MACVLAN_MODE_BRIDGE); 185618e1b74SArnd Bergmann else if (src->mode == MACVLAN_MODE_VEPA) 186618e1b74SArnd Bergmann /* flood to everyone except source */ 187618e1b74SArnd Bergmann macvlan_broadcast(skb, port, src->dev, 188618e1b74SArnd Bergmann MACVLAN_MODE_VEPA | 189618e1b74SArnd Bergmann MACVLAN_MODE_BRIDGE); 190618e1b74SArnd Bergmann else if (src->mode == MACVLAN_MODE_BRIDGE) 191618e1b74SArnd Bergmann /* 192618e1b74SArnd Bergmann * flood only to VEPA ports, bridge ports 193618e1b74SArnd Bergmann * already saw the frame on the way out. 194618e1b74SArnd Bergmann */ 195618e1b74SArnd Bergmann macvlan_broadcast(skb, port, src->dev, 196618e1b74SArnd Bergmann MACVLAN_MODE_VEPA); 197729e72a1Sstephen hemminger else { 198729e72a1Sstephen hemminger /* forward to original port. */ 199729e72a1Sstephen hemminger vlan = src; 200729e72a1Sstephen hemminger ret = macvlan_broadcast_one(skb, vlan, eth, 0); 201729e72a1Sstephen hemminger goto out; 202729e72a1Sstephen hemminger } 203729e72a1Sstephen hemminger 2048a4eb573SJiri Pirko return RX_HANDLER_PASS; 205b863ceb7SPatrick McHardy } 206b863ceb7SPatrick McHardy 207eb06acdcSSridhar Samudrala if (port->passthru) 208eb06acdcSSridhar Samudrala vlan = list_first_entry(&port->vlans, struct macvlan_dev, list); 209eb06acdcSSridhar Samudrala else 210b863ceb7SPatrick McHardy vlan = macvlan_hash_lookup(port, eth->h_dest); 211b863ceb7SPatrick McHardy if (vlan == NULL) 2128a4eb573SJiri Pirko return RX_HANDLER_PASS; 213b863ceb7SPatrick McHardy 214b863ceb7SPatrick McHardy dev = vlan->dev; 215b863ceb7SPatrick McHardy if (unlikely(!(dev->flags & IFF_UP))) { 216b863ceb7SPatrick McHardy kfree_skb(skb); 2178a4eb573SJiri Pirko return RX_HANDLER_CONSUMED; 218b863ceb7SPatrick McHardy } 219a1e514c5SArnd Bergmann len = skb->len + ETH_HLEN; 220b863ceb7SPatrick McHardy skb = skb_share_check(skb, GFP_ATOMIC); 221a1e514c5SArnd Bergmann if (!skb) 222ba01877fSSridhar Samudrala goto out; 223b863ceb7SPatrick McHardy 224b863ceb7SPatrick McHardy skb->dev = dev; 225b863ceb7SPatrick McHardy skb->pkt_type = PACKET_HOST; 226b863ceb7SPatrick McHardy 227ba01877fSSridhar Samudrala ret = vlan->receive(skb); 228ba01877fSSridhar Samudrala 229ba01877fSSridhar Samudrala out: 230ba01877fSSridhar Samudrala macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, 0); 2318a4eb573SJiri Pirko return RX_HANDLER_CONSUMED; 232b863ceb7SPatrick McHardy } 233b863ceb7SPatrick McHardy 234618e1b74SArnd Bergmann static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev) 235618e1b74SArnd Bergmann { 236618e1b74SArnd Bergmann const struct macvlan_dev *vlan = netdev_priv(dev); 237618e1b74SArnd Bergmann const struct macvlan_port *port = vlan->port; 238618e1b74SArnd Bergmann const struct macvlan_dev *dest; 23912a2856bSDaniel Lezcano __u8 ip_summed = skb->ip_summed; 240618e1b74SArnd Bergmann 241618e1b74SArnd Bergmann if (vlan->mode == MACVLAN_MODE_BRIDGE) { 242618e1b74SArnd Bergmann const struct ethhdr *eth = (void *)skb->data; 24312a2856bSDaniel Lezcano skb->ip_summed = CHECKSUM_UNNECESSARY; 244618e1b74SArnd Bergmann 245618e1b74SArnd Bergmann /* send to other bridge ports directly */ 246618e1b74SArnd Bergmann if (is_multicast_ether_addr(eth->h_dest)) { 247618e1b74SArnd Bergmann macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE); 248618e1b74SArnd Bergmann goto xmit_world; 249618e1b74SArnd Bergmann } 250618e1b74SArnd Bergmann 251618e1b74SArnd Bergmann dest = macvlan_hash_lookup(port, eth->h_dest); 252618e1b74SArnd Bergmann if (dest && dest->mode == MACVLAN_MODE_BRIDGE) { 253a37dd333SDavid Ward /* send to lowerdev first for its network taps */ 254cb2d0f3eSDavid Ward dev_forward_skb(vlan->lowerdev, skb); 255618e1b74SArnd Bergmann 256618e1b74SArnd Bergmann return NET_XMIT_SUCCESS; 257618e1b74SArnd Bergmann } 258618e1b74SArnd Bergmann } 259618e1b74SArnd Bergmann 260618e1b74SArnd Bergmann xmit_world: 26112a2856bSDaniel Lezcano skb->ip_summed = ip_summed; 2628a83a00bSArnd Bergmann skb_set_dev(skb, vlan->lowerdev); 263618e1b74SArnd Bergmann return dev_queue_xmit(skb); 264618e1b74SArnd Bergmann } 265618e1b74SArnd Bergmann 266fc0663d6SArnd Bergmann netdev_tx_t macvlan_start_xmit(struct sk_buff *skb, 267424efe9cSStephen Hemminger struct net_device *dev) 268b863ceb7SPatrick McHardy { 269b863ceb7SPatrick McHardy unsigned int len = skb->len; 270b863ceb7SPatrick McHardy int ret; 2718ffab51bSEric Dumazet const struct macvlan_dev *vlan = netdev_priv(dev); 272b863ceb7SPatrick McHardy 273618e1b74SArnd Bergmann ret = macvlan_queue_xmit(skb, dev); 2742d6c9ffcSEric Dumazet if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) { 2758ffab51bSEric Dumazet struct macvlan_pcpu_stats *pcpu_stats; 2762c114553SEric Dumazet 2778ffab51bSEric Dumazet pcpu_stats = this_cpu_ptr(vlan->pcpu_stats); 2788ffab51bSEric Dumazet u64_stats_update_begin(&pcpu_stats->syncp); 2798ffab51bSEric Dumazet pcpu_stats->tx_packets++; 2808ffab51bSEric Dumazet pcpu_stats->tx_bytes += len; 2818ffab51bSEric Dumazet u64_stats_update_end(&pcpu_stats->syncp); 2828ffab51bSEric Dumazet } else { 2838ffab51bSEric Dumazet this_cpu_inc(vlan->pcpu_stats->tx_dropped); 2848ffab51bSEric Dumazet } 285cbbef5e1SPatrick McHardy return ret; 286b863ceb7SPatrick McHardy } 287fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_start_xmit); 288b863ceb7SPatrick McHardy 289b863ceb7SPatrick McHardy static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev, 2903b04dddeSStephen Hemminger unsigned short type, const void *daddr, 2913b04dddeSStephen Hemminger const void *saddr, unsigned len) 292b863ceb7SPatrick McHardy { 293b863ceb7SPatrick McHardy const struct macvlan_dev *vlan = netdev_priv(dev); 294b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 295b863ceb7SPatrick McHardy 2960c4e8581SStephen Hemminger return dev_hard_header(skb, lowerdev, type, daddr, 297b863ceb7SPatrick McHardy saddr ? : dev->dev_addr, len); 298b863ceb7SPatrick McHardy } 299b863ceb7SPatrick McHardy 3003b04dddeSStephen Hemminger static const struct header_ops macvlan_hard_header_ops = { 3013b04dddeSStephen Hemminger .create = macvlan_hard_header, 3023b04dddeSStephen Hemminger .rebuild = eth_rebuild_header, 3033b04dddeSStephen Hemminger .parse = eth_header_parse, 3043b04dddeSStephen Hemminger .cache = eth_header_cache, 3053b04dddeSStephen Hemminger .cache_update = eth_header_cache_update, 3063b04dddeSStephen Hemminger }; 3073b04dddeSStephen Hemminger 308b863ceb7SPatrick McHardy static int macvlan_open(struct net_device *dev) 309b863ceb7SPatrick McHardy { 310b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 311b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 312b863ceb7SPatrick McHardy int err; 313b863ceb7SPatrick McHardy 314eb06acdcSSridhar Samudrala if (vlan->port->passthru) { 315eb06acdcSSridhar Samudrala dev_set_promiscuity(lowerdev, 1); 316eb06acdcSSridhar Samudrala goto hash_add; 317eb06acdcSSridhar Samudrala } 318eb06acdcSSridhar Samudrala 319f9ac30f0SEric Biederman err = -EBUSY; 320f9ac30f0SEric Biederman if (macvlan_addr_busy(vlan->port, dev->dev_addr)) 321f9ac30f0SEric Biederman goto out; 322f9ac30f0SEric Biederman 323a748ee24SJiri Pirko err = dev_uc_add(lowerdev, dev->dev_addr); 324b863ceb7SPatrick McHardy if (err < 0) 325b89fb7daSWang Chen goto out; 326b89fb7daSWang Chen if (dev->flags & IFF_ALLMULTI) { 327b89fb7daSWang Chen err = dev_set_allmulti(lowerdev, 1); 328b89fb7daSWang Chen if (err < 0) 329b89fb7daSWang Chen goto del_unicast; 330b89fb7daSWang Chen } 331eb06acdcSSridhar Samudrala 332eb06acdcSSridhar Samudrala hash_add: 333f9ac30f0SEric Biederman macvlan_hash_add(vlan); 334b863ceb7SPatrick McHardy return 0; 335b89fb7daSWang Chen 336b89fb7daSWang Chen del_unicast: 337a748ee24SJiri Pirko dev_uc_del(lowerdev, dev->dev_addr); 338b89fb7daSWang Chen out: 339b89fb7daSWang Chen return err; 340b863ceb7SPatrick McHardy } 341b863ceb7SPatrick McHardy 342b863ceb7SPatrick McHardy static int macvlan_stop(struct net_device *dev) 343b863ceb7SPatrick McHardy { 344b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 345b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 346b863ceb7SPatrick McHardy 347eb06acdcSSridhar Samudrala if (vlan->port->passthru) { 348eb06acdcSSridhar Samudrala dev_set_promiscuity(lowerdev, -1); 349eb06acdcSSridhar Samudrala goto hash_del; 350eb06acdcSSridhar Samudrala } 351eb06acdcSSridhar Samudrala 352b863ceb7SPatrick McHardy dev_mc_unsync(lowerdev, dev); 353b863ceb7SPatrick McHardy if (dev->flags & IFF_ALLMULTI) 354b863ceb7SPatrick McHardy dev_set_allmulti(lowerdev, -1); 355b863ceb7SPatrick McHardy 356a748ee24SJiri Pirko dev_uc_del(lowerdev, dev->dev_addr); 357b863ceb7SPatrick McHardy 358eb06acdcSSridhar Samudrala hash_del: 359449f4544SEric Dumazet macvlan_hash_del(vlan, !dev->dismantle); 360b863ceb7SPatrick McHardy return 0; 361b863ceb7SPatrick McHardy } 362b863ceb7SPatrick McHardy 363ad5d20a6SPatrick McHardy static int macvlan_set_mac_address(struct net_device *dev, void *p) 364ad5d20a6SPatrick McHardy { 365ad5d20a6SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 366ad5d20a6SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 367ad5d20a6SPatrick McHardy struct sockaddr *addr = p; 368ad5d20a6SPatrick McHardy int err; 369ad5d20a6SPatrick McHardy 370ad5d20a6SPatrick McHardy if (!is_valid_ether_addr(addr->sa_data)) 371ad5d20a6SPatrick McHardy return -EADDRNOTAVAIL; 372ad5d20a6SPatrick McHardy 373f9ac30f0SEric Biederman if (!(dev->flags & IFF_UP)) { 374f9ac30f0SEric Biederman /* Just copy in the new address */ 375f9ac30f0SEric Biederman memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN); 376f9ac30f0SEric Biederman } else { 377f9ac30f0SEric Biederman /* Rehash and update the device filters */ 378f9ac30f0SEric Biederman if (macvlan_addr_busy(vlan->port, addr->sa_data)) 379f9ac30f0SEric Biederman return -EBUSY; 380ad5d20a6SPatrick McHardy 381a748ee24SJiri Pirko err = dev_uc_add(lowerdev, addr->sa_data); 382ccffad25SJiri Pirko if (err) 383ad5d20a6SPatrick McHardy return err; 384f9ac30f0SEric Biederman 385a748ee24SJiri Pirko dev_uc_del(lowerdev, dev->dev_addr); 386ad5d20a6SPatrick McHardy 387f9ac30f0SEric Biederman macvlan_hash_change_addr(vlan, addr->sa_data); 388f9ac30f0SEric Biederman } 389ad5d20a6SPatrick McHardy return 0; 390ad5d20a6SPatrick McHardy } 391ad5d20a6SPatrick McHardy 392b863ceb7SPatrick McHardy static void macvlan_change_rx_flags(struct net_device *dev, int change) 393b863ceb7SPatrick McHardy { 394b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 395b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 396b863ceb7SPatrick McHardy 397b863ceb7SPatrick McHardy if (change & IFF_ALLMULTI) 398b863ceb7SPatrick McHardy dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1); 399b863ceb7SPatrick McHardy } 400b863ceb7SPatrick McHardy 401b863ceb7SPatrick McHardy static void macvlan_set_multicast_list(struct net_device *dev) 402b863ceb7SPatrick McHardy { 403b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 404b863ceb7SPatrick McHardy 405b863ceb7SPatrick McHardy dev_mc_sync(vlan->lowerdev, dev); 406b863ceb7SPatrick McHardy } 407b863ceb7SPatrick McHardy 408b863ceb7SPatrick McHardy static int macvlan_change_mtu(struct net_device *dev, int new_mtu) 409b863ceb7SPatrick McHardy { 410b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 411b863ceb7SPatrick McHardy 412b863ceb7SPatrick McHardy if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu) 413b863ceb7SPatrick McHardy return -EINVAL; 414b863ceb7SPatrick McHardy dev->mtu = new_mtu; 415b863ceb7SPatrick McHardy return 0; 416b863ceb7SPatrick McHardy } 417b863ceb7SPatrick McHardy 418b863ceb7SPatrick McHardy /* 419b863ceb7SPatrick McHardy * macvlan network devices have devices nesting below it and are a special 420b863ceb7SPatrick McHardy * "super class" of normal network devices; split their locks off into a 421b863ceb7SPatrick McHardy * separate class since they always nest. 422b863ceb7SPatrick McHardy */ 423b863ceb7SPatrick McHardy static struct lock_class_key macvlan_netdev_xmit_lock_key; 424cf508b12SDavid S. Miller static struct lock_class_key macvlan_netdev_addr_lock_key; 425b863ceb7SPatrick McHardy 426b863ceb7SPatrick McHardy #define MACVLAN_FEATURES \ 427b863ceb7SPatrick McHardy (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \ 428b863ceb7SPatrick McHardy NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \ 4298d13e670SJohn Fastabend NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \ 4308d13e670SJohn Fastabend NETIF_F_HW_VLAN_FILTER) 431b863ceb7SPatrick McHardy 432b863ceb7SPatrick McHardy #define MACVLAN_STATE_MASK \ 433b863ceb7SPatrick McHardy ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT)) 434b863ceb7SPatrick McHardy 435e8a0464cSDavid S. Miller static void macvlan_set_lockdep_class_one(struct net_device *dev, 436e8a0464cSDavid S. Miller struct netdev_queue *txq, 437e8a0464cSDavid S. Miller void *_unused) 438c773e847SDavid S. Miller { 439c773e847SDavid S. Miller lockdep_set_class(&txq->_xmit_lock, 440c773e847SDavid S. Miller &macvlan_netdev_xmit_lock_key); 441c773e847SDavid S. Miller } 442c773e847SDavid S. Miller 443c773e847SDavid S. Miller static void macvlan_set_lockdep_class(struct net_device *dev) 444c773e847SDavid S. Miller { 445cf508b12SDavid S. Miller lockdep_set_class(&dev->addr_list_lock, 446cf508b12SDavid S. Miller &macvlan_netdev_addr_lock_key); 447e8a0464cSDavid S. Miller netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL); 448c773e847SDavid S. Miller } 449c773e847SDavid S. Miller 450b863ceb7SPatrick McHardy static int macvlan_init(struct net_device *dev) 451b863ceb7SPatrick McHardy { 452b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 453b863ceb7SPatrick McHardy const struct net_device *lowerdev = vlan->lowerdev; 454b863ceb7SPatrick McHardy 455b863ceb7SPatrick McHardy dev->state = (dev->state & ~MACVLAN_STATE_MASK) | 456b863ceb7SPatrick McHardy (lowerdev->state & MACVLAN_STATE_MASK); 457b863ceb7SPatrick McHardy dev->features = lowerdev->features & MACVLAN_FEATURES; 4588ffab51bSEric Dumazet dev->features |= NETIF_F_LLTX; 4598c2acc53SPatrick McHardy dev->gso_max_size = lowerdev->gso_max_size; 460b863ceb7SPatrick McHardy dev->iflink = lowerdev->ifindex; 461ef5c8996Ssg.tweak@gmail.com dev->hard_header_len = lowerdev->hard_header_len; 462b863ceb7SPatrick McHardy 463c773e847SDavid S. Miller macvlan_set_lockdep_class(dev); 464c773e847SDavid S. Miller 4658ffab51bSEric Dumazet vlan->pcpu_stats = alloc_percpu(struct macvlan_pcpu_stats); 4668ffab51bSEric Dumazet if (!vlan->pcpu_stats) 467fccaf710SEric Dumazet return -ENOMEM; 468fccaf710SEric Dumazet 469b863ceb7SPatrick McHardy return 0; 470b863ceb7SPatrick McHardy } 471b863ceb7SPatrick McHardy 472fccaf710SEric Dumazet static void macvlan_uninit(struct net_device *dev) 473fccaf710SEric Dumazet { 474fccaf710SEric Dumazet struct macvlan_dev *vlan = netdev_priv(dev); 475d5cd9244SEric W. Biederman struct macvlan_port *port = vlan->port; 476fccaf710SEric Dumazet 4778ffab51bSEric Dumazet free_percpu(vlan->pcpu_stats); 478d5cd9244SEric W. Biederman 479d5cd9244SEric W. Biederman port->count -= 1; 480d5cd9244SEric W. Biederman if (!port->count) 481d5cd9244SEric W. Biederman macvlan_port_destroy(port->dev); 482fccaf710SEric Dumazet } 483fccaf710SEric Dumazet 48428172739SEric Dumazet static struct rtnl_link_stats64 *macvlan_dev_get_stats64(struct net_device *dev, 48528172739SEric Dumazet struct rtnl_link_stats64 *stats) 486fccaf710SEric Dumazet { 487fccaf710SEric Dumazet struct macvlan_dev *vlan = netdev_priv(dev); 488fccaf710SEric Dumazet 4898ffab51bSEric Dumazet if (vlan->pcpu_stats) { 4908ffab51bSEric Dumazet struct macvlan_pcpu_stats *p; 4918ffab51bSEric Dumazet u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes; 4928ffab51bSEric Dumazet u32 rx_errors = 0, tx_dropped = 0; 493bc66154eSEric Dumazet unsigned int start; 494fccaf710SEric Dumazet int i; 495fccaf710SEric Dumazet 496fccaf710SEric Dumazet for_each_possible_cpu(i) { 4978ffab51bSEric Dumazet p = per_cpu_ptr(vlan->pcpu_stats, i); 498bc66154eSEric Dumazet do { 499bc66154eSEric Dumazet start = u64_stats_fetch_begin_bh(&p->syncp); 500bc66154eSEric Dumazet rx_packets = p->rx_packets; 501bc66154eSEric Dumazet rx_bytes = p->rx_bytes; 502bc66154eSEric Dumazet rx_multicast = p->rx_multicast; 5038ffab51bSEric Dumazet tx_packets = p->tx_packets; 5048ffab51bSEric Dumazet tx_bytes = p->tx_bytes; 505bc66154eSEric Dumazet } while (u64_stats_fetch_retry_bh(&p->syncp, start)); 5068ffab51bSEric Dumazet 5078ffab51bSEric Dumazet stats->rx_packets += rx_packets; 5088ffab51bSEric Dumazet stats->rx_bytes += rx_bytes; 5098ffab51bSEric Dumazet stats->multicast += rx_multicast; 5108ffab51bSEric Dumazet stats->tx_packets += tx_packets; 5118ffab51bSEric Dumazet stats->tx_bytes += tx_bytes; 5128ffab51bSEric Dumazet /* rx_errors & tx_dropped are u32, updated 5138ffab51bSEric Dumazet * without syncp protection. 5148ffab51bSEric Dumazet */ 5158ffab51bSEric Dumazet rx_errors += p->rx_errors; 5168ffab51bSEric Dumazet tx_dropped += p->tx_dropped; 517fccaf710SEric Dumazet } 5188ffab51bSEric Dumazet stats->rx_errors = rx_errors; 5198ffab51bSEric Dumazet stats->rx_dropped = rx_errors; 5208ffab51bSEric Dumazet stats->tx_dropped = tx_dropped; 521fccaf710SEric Dumazet } 522fccaf710SEric Dumazet return stats; 523fccaf710SEric Dumazet } 524fccaf710SEric Dumazet 5258e586137SJiri Pirko static int macvlan_vlan_rx_add_vid(struct net_device *dev, 5268d13e670SJohn Fastabend unsigned short vid) 5278d13e670SJohn Fastabend { 5288d13e670SJohn Fastabend struct macvlan_dev *vlan = netdev_priv(dev); 5298d13e670SJohn Fastabend struct net_device *lowerdev = vlan->lowerdev; 5308d13e670SJohn Fastabend 53187002b03SJiri Pirko return vlan_vid_add(lowerdev, vid); 5328d13e670SJohn Fastabend } 5338d13e670SJohn Fastabend 5348e586137SJiri Pirko static int macvlan_vlan_rx_kill_vid(struct net_device *dev, 5358d13e670SJohn Fastabend unsigned short vid) 5368d13e670SJohn Fastabend { 5378d13e670SJohn Fastabend struct macvlan_dev *vlan = netdev_priv(dev); 5388d13e670SJohn Fastabend struct net_device *lowerdev = vlan->lowerdev; 5398d13e670SJohn Fastabend 54087002b03SJiri Pirko vlan_vid_del(lowerdev, vid); 5418e586137SJiri Pirko return 0; 5428d13e670SJohn Fastabend } 5438d13e670SJohn Fastabend 544b863ceb7SPatrick McHardy static void macvlan_ethtool_get_drvinfo(struct net_device *dev, 545b863ceb7SPatrick McHardy struct ethtool_drvinfo *drvinfo) 546b863ceb7SPatrick McHardy { 547b863ceb7SPatrick McHardy snprintf(drvinfo->driver, 32, "macvlan"); 548b863ceb7SPatrick McHardy snprintf(drvinfo->version, 32, "0.1"); 549b863ceb7SPatrick McHardy } 550b863ceb7SPatrick McHardy 5519edb8bb6SStephen Hemminger static int macvlan_ethtool_get_settings(struct net_device *dev, 5529edb8bb6SStephen Hemminger struct ethtool_cmd *cmd) 5539edb8bb6SStephen Hemminger { 5549edb8bb6SStephen Hemminger const struct macvlan_dev *vlan = netdev_priv(dev); 5554bc71cb9SJiri Pirko 5564bc71cb9SJiri Pirko return __ethtool_get_settings(vlan->lowerdev, cmd); 5579edb8bb6SStephen Hemminger } 5589edb8bb6SStephen Hemminger 559b863ceb7SPatrick McHardy static const struct ethtool_ops macvlan_ethtool_ops = { 560b863ceb7SPatrick McHardy .get_link = ethtool_op_get_link, 5619edb8bb6SStephen Hemminger .get_settings = macvlan_ethtool_get_settings, 562b863ceb7SPatrick McHardy .get_drvinfo = macvlan_ethtool_get_drvinfo, 563b863ceb7SPatrick McHardy }; 564b863ceb7SPatrick McHardy 56554a30c97SStephen Hemminger static const struct net_device_ops macvlan_netdev_ops = { 56654a30c97SStephen Hemminger .ndo_init = macvlan_init, 567fccaf710SEric Dumazet .ndo_uninit = macvlan_uninit, 56854a30c97SStephen Hemminger .ndo_open = macvlan_open, 56954a30c97SStephen Hemminger .ndo_stop = macvlan_stop, 57000829823SStephen Hemminger .ndo_start_xmit = macvlan_start_xmit, 57154a30c97SStephen Hemminger .ndo_change_mtu = macvlan_change_mtu, 57254a30c97SStephen Hemminger .ndo_change_rx_flags = macvlan_change_rx_flags, 57354a30c97SStephen Hemminger .ndo_set_mac_address = macvlan_set_mac_address, 574afc4b13dSJiri Pirko .ndo_set_rx_mode = macvlan_set_multicast_list, 575bc66154eSEric Dumazet .ndo_get_stats64 = macvlan_dev_get_stats64, 57654a30c97SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 5778d13e670SJohn Fastabend .ndo_vlan_rx_add_vid = macvlan_vlan_rx_add_vid, 5788d13e670SJohn Fastabend .ndo_vlan_rx_kill_vid = macvlan_vlan_rx_kill_vid, 57954a30c97SStephen Hemminger }; 58054a30c97SStephen Hemminger 5818a35747aSHerbert Xu void macvlan_common_setup(struct net_device *dev) 582b863ceb7SPatrick McHardy { 583b863ceb7SPatrick McHardy ether_setup(dev); 584b863ceb7SPatrick McHardy 585550fd08cSNeil Horman dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING); 58654a30c97SStephen Hemminger dev->netdev_ops = &macvlan_netdev_ops; 587b863ceb7SPatrick McHardy dev->destructor = free_netdev; 5883b04dddeSStephen Hemminger dev->header_ops = &macvlan_hard_header_ops, 589b863ceb7SPatrick McHardy dev->ethtool_ops = &macvlan_ethtool_ops; 5908a35747aSHerbert Xu } 5918a35747aSHerbert Xu EXPORT_SYMBOL_GPL(macvlan_common_setup); 5928a35747aSHerbert Xu 5938a35747aSHerbert Xu static void macvlan_setup(struct net_device *dev) 5948a35747aSHerbert Xu { 5958a35747aSHerbert Xu macvlan_common_setup(dev); 596b863ceb7SPatrick McHardy dev->tx_queue_len = 0; 597b863ceb7SPatrick McHardy } 598b863ceb7SPatrick McHardy 599b863ceb7SPatrick McHardy static int macvlan_port_create(struct net_device *dev) 600b863ceb7SPatrick McHardy { 601b863ceb7SPatrick McHardy struct macvlan_port *port; 602b863ceb7SPatrick McHardy unsigned int i; 603ab95bfe0SJiri Pirko int err; 604b863ceb7SPatrick McHardy 605b863ceb7SPatrick McHardy if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK) 606b863ceb7SPatrick McHardy return -EINVAL; 607b863ceb7SPatrick McHardy 608b863ceb7SPatrick McHardy port = kzalloc(sizeof(*port), GFP_KERNEL); 609b863ceb7SPatrick McHardy if (port == NULL) 610b863ceb7SPatrick McHardy return -ENOMEM; 611b863ceb7SPatrick McHardy 612eb06acdcSSridhar Samudrala port->passthru = false; 613b863ceb7SPatrick McHardy port->dev = dev; 614b863ceb7SPatrick McHardy INIT_LIST_HEAD(&port->vlans); 615b863ceb7SPatrick McHardy for (i = 0; i < MACVLAN_HASH_SIZE; i++) 616b863ceb7SPatrick McHardy INIT_HLIST_HEAD(&port->vlan_hash[i]); 617ab95bfe0SJiri Pirko 618a35e2c1bSJiri Pirko err = netdev_rx_handler_register(dev, macvlan_handle_frame, port); 619a35e2c1bSJiri Pirko if (err) 620ab95bfe0SJiri Pirko kfree(port); 621d9351561SEric Dumazet else 622a35e2c1bSJiri Pirko dev->priv_flags |= IFF_MACVLAN_PORT; 623ab95bfe0SJiri Pirko return err; 624b863ceb7SPatrick McHardy } 625b863ceb7SPatrick McHardy 626b863ceb7SPatrick McHardy static void macvlan_port_destroy(struct net_device *dev) 627b863ceb7SPatrick McHardy { 628a35e2c1bSJiri Pirko struct macvlan_port *port = macvlan_port_get(dev); 629b863ceb7SPatrick McHardy 630a35e2c1bSJiri Pirko dev->priv_flags &= ~IFF_MACVLAN_PORT; 631ab95bfe0SJiri Pirko netdev_rx_handler_unregister(dev); 6326e070aecSLai Jiangshan kfree_rcu(port, rcu); 633b863ceb7SPatrick McHardy } 634b863ceb7SPatrick McHardy 635b863ceb7SPatrick McHardy static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[]) 636b863ceb7SPatrick McHardy { 637b863ceb7SPatrick McHardy if (tb[IFLA_ADDRESS]) { 638b863ceb7SPatrick McHardy if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) 639b863ceb7SPatrick McHardy return -EINVAL; 640b863ceb7SPatrick McHardy if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) 641b863ceb7SPatrick McHardy return -EADDRNOTAVAIL; 642b863ceb7SPatrick McHardy } 64327c0b1a8SArnd Bergmann 64427c0b1a8SArnd Bergmann if (data && data[IFLA_MACVLAN_MODE]) { 64527c0b1a8SArnd Bergmann switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) { 64627c0b1a8SArnd Bergmann case MACVLAN_MODE_PRIVATE: 64727c0b1a8SArnd Bergmann case MACVLAN_MODE_VEPA: 64827c0b1a8SArnd Bergmann case MACVLAN_MODE_BRIDGE: 649eb06acdcSSridhar Samudrala case MACVLAN_MODE_PASSTHRU: 65027c0b1a8SArnd Bergmann break; 65127c0b1a8SArnd Bergmann default: 65227c0b1a8SArnd Bergmann return -EINVAL; 65327c0b1a8SArnd Bergmann } 65427c0b1a8SArnd Bergmann } 655b863ceb7SPatrick McHardy return 0; 656b863ceb7SPatrick McHardy } 657b863ceb7SPatrick McHardy 658fc0663d6SArnd Bergmann int macvlan_common_newlink(struct net *src_net, struct net_device *dev, 659fc0663d6SArnd Bergmann struct nlattr *tb[], struct nlattr *data[], 660fc0663d6SArnd Bergmann int (*receive)(struct sk_buff *skb), 661fc0663d6SArnd Bergmann int (*forward)(struct net_device *dev, 662fc0663d6SArnd Bergmann struct sk_buff *skb)) 663b863ceb7SPatrick McHardy { 664b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 665b863ceb7SPatrick McHardy struct macvlan_port *port; 666b863ceb7SPatrick McHardy struct net_device *lowerdev; 667b863ceb7SPatrick McHardy int err; 668b863ceb7SPatrick McHardy 669b863ceb7SPatrick McHardy if (!tb[IFLA_LINK]) 670b863ceb7SPatrick McHardy return -EINVAL; 671b863ceb7SPatrick McHardy 67281adee47SEric W. Biederman lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK])); 673b863ceb7SPatrick McHardy if (lowerdev == NULL) 674b863ceb7SPatrick McHardy return -ENODEV; 675b863ceb7SPatrick McHardy 676b0832a29SEric Biederman /* When creating macvlans on top of other macvlans - use 677b0832a29SEric Biederman * the real device as the lowerdev. 678a6ca5f1dSPatrick McHardy */ 679b0832a29SEric Biederman if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) { 680b0832a29SEric Biederman struct macvlan_dev *lowervlan = netdev_priv(lowerdev); 681b0832a29SEric Biederman lowerdev = lowervlan->lowerdev; 682b0832a29SEric Biederman } 683a6ca5f1dSPatrick McHardy 684b863ceb7SPatrick McHardy if (!tb[IFLA_MTU]) 685b863ceb7SPatrick McHardy dev->mtu = lowerdev->mtu; 686b863ceb7SPatrick McHardy else if (dev->mtu > lowerdev->mtu) 687b863ceb7SPatrick McHardy return -EINVAL; 688b863ceb7SPatrick McHardy 689b863ceb7SPatrick McHardy if (!tb[IFLA_ADDRESS]) 690b863ceb7SPatrick McHardy random_ether_addr(dev->dev_addr); 691b863ceb7SPatrick McHardy 692a35e2c1bSJiri Pirko if (!macvlan_port_exists(lowerdev)) { 693b863ceb7SPatrick McHardy err = macvlan_port_create(lowerdev); 694b863ceb7SPatrick McHardy if (err < 0) 695b863ceb7SPatrick McHardy return err; 696b863ceb7SPatrick McHardy } 697a35e2c1bSJiri Pirko port = macvlan_port_get(lowerdev); 698b863ceb7SPatrick McHardy 699eb06acdcSSridhar Samudrala /* Only 1 macvlan device can be created in passthru mode */ 700eb06acdcSSridhar Samudrala if (port->passthru) 701eb06acdcSSridhar Samudrala return -EINVAL; 702eb06acdcSSridhar Samudrala 703b863ceb7SPatrick McHardy vlan->lowerdev = lowerdev; 704b863ceb7SPatrick McHardy vlan->dev = dev; 705b863ceb7SPatrick McHardy vlan->port = port; 706fc0663d6SArnd Bergmann vlan->receive = receive; 707fc0663d6SArnd Bergmann vlan->forward = forward; 708b863ceb7SPatrick McHardy 70927c0b1a8SArnd Bergmann vlan->mode = MACVLAN_MODE_VEPA; 71027c0b1a8SArnd Bergmann if (data && data[IFLA_MACVLAN_MODE]) 71127c0b1a8SArnd Bergmann vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]); 71227c0b1a8SArnd Bergmann 713eb06acdcSSridhar Samudrala if (vlan->mode == MACVLAN_MODE_PASSTHRU) { 714d5cd9244SEric W. Biederman if (port->count) 715eb06acdcSSridhar Samudrala return -EINVAL; 716eb06acdcSSridhar Samudrala port->passthru = true; 717eb06acdcSSridhar Samudrala memcpy(dev->dev_addr, lowerdev->dev_addr, ETH_ALEN); 718eb06acdcSSridhar Samudrala } 719eb06acdcSSridhar Samudrala 720d5cd9244SEric W. Biederman port->count += 1; 721b863ceb7SPatrick McHardy err = register_netdevice(dev); 722b863ceb7SPatrick McHardy if (err < 0) 723f16d3d57SJiri Pirko goto destroy_port; 724b863ceb7SPatrick McHardy 725b863ceb7SPatrick McHardy list_add_tail(&vlan->list, &port->vlans); 726fc4a7489SPatrick Mullaney netif_stacked_transfer_operstate(lowerdev, dev); 727f16d3d57SJiri Pirko 728b863ceb7SPatrick McHardy return 0; 729f16d3d57SJiri Pirko 730f16d3d57SJiri Pirko destroy_port: 731d5cd9244SEric W. Biederman port->count -= 1; 732d5cd9244SEric W. Biederman if (!port->count) 733f16d3d57SJiri Pirko macvlan_port_destroy(lowerdev); 734f16d3d57SJiri Pirko 735f16d3d57SJiri Pirko return err; 736b863ceb7SPatrick McHardy } 737fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_common_newlink); 738b863ceb7SPatrick McHardy 739fc0663d6SArnd Bergmann static int macvlan_newlink(struct net *src_net, struct net_device *dev, 740fc0663d6SArnd Bergmann struct nlattr *tb[], struct nlattr *data[]) 741fc0663d6SArnd Bergmann { 742fc0663d6SArnd Bergmann return macvlan_common_newlink(src_net, dev, tb, data, 743fc0663d6SArnd Bergmann netif_rx, 744fc0663d6SArnd Bergmann dev_forward_skb); 745fc0663d6SArnd Bergmann } 746fc0663d6SArnd Bergmann 747fc0663d6SArnd Bergmann void macvlan_dellink(struct net_device *dev, struct list_head *head) 748b863ceb7SPatrick McHardy { 749b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 750b863ceb7SPatrick McHardy 751b863ceb7SPatrick McHardy list_del(&vlan->list); 75223289a37SEric Dumazet unregister_netdevice_queue(dev, head); 753b863ceb7SPatrick McHardy } 754fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_dellink); 755b863ceb7SPatrick McHardy 75627c0b1a8SArnd Bergmann static int macvlan_changelink(struct net_device *dev, 75727c0b1a8SArnd Bergmann struct nlattr *tb[], struct nlattr *data[]) 75827c0b1a8SArnd Bergmann { 75927c0b1a8SArnd Bergmann struct macvlan_dev *vlan = netdev_priv(dev); 76027c0b1a8SArnd Bergmann if (data && data[IFLA_MACVLAN_MODE]) 76127c0b1a8SArnd Bergmann vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]); 76227c0b1a8SArnd Bergmann return 0; 76327c0b1a8SArnd Bergmann } 76427c0b1a8SArnd Bergmann 76527c0b1a8SArnd Bergmann static size_t macvlan_get_size(const struct net_device *dev) 76627c0b1a8SArnd Bergmann { 76727c0b1a8SArnd Bergmann return nla_total_size(4); 76827c0b1a8SArnd Bergmann } 76927c0b1a8SArnd Bergmann 77027c0b1a8SArnd Bergmann static int macvlan_fill_info(struct sk_buff *skb, 77127c0b1a8SArnd Bergmann const struct net_device *dev) 77227c0b1a8SArnd Bergmann { 77327c0b1a8SArnd Bergmann struct macvlan_dev *vlan = netdev_priv(dev); 77427c0b1a8SArnd Bergmann 77527c0b1a8SArnd Bergmann NLA_PUT_U32(skb, IFLA_MACVLAN_MODE, vlan->mode); 77627c0b1a8SArnd Bergmann return 0; 77727c0b1a8SArnd Bergmann 77827c0b1a8SArnd Bergmann nla_put_failure: 77927c0b1a8SArnd Bergmann return -EMSGSIZE; 78027c0b1a8SArnd Bergmann } 78127c0b1a8SArnd Bergmann 78227c0b1a8SArnd Bergmann static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = { 78327c0b1a8SArnd Bergmann [IFLA_MACVLAN_MODE] = { .type = NLA_U32 }, 78427c0b1a8SArnd Bergmann }; 78527c0b1a8SArnd Bergmann 786fc0663d6SArnd Bergmann int macvlan_link_register(struct rtnl_link_ops *ops) 787fc0663d6SArnd Bergmann { 788fc0663d6SArnd Bergmann /* common fields */ 789fc0663d6SArnd Bergmann ops->priv_size = sizeof(struct macvlan_dev); 790fc0663d6SArnd Bergmann ops->validate = macvlan_validate; 791fc0663d6SArnd Bergmann ops->maxtype = IFLA_MACVLAN_MAX; 792fc0663d6SArnd Bergmann ops->policy = macvlan_policy; 793fc0663d6SArnd Bergmann ops->changelink = macvlan_changelink; 794fc0663d6SArnd Bergmann ops->get_size = macvlan_get_size; 795fc0663d6SArnd Bergmann ops->fill_info = macvlan_fill_info; 796fc0663d6SArnd Bergmann 797fc0663d6SArnd Bergmann return rtnl_link_register(ops); 798fc0663d6SArnd Bergmann }; 799fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_link_register); 800fc0663d6SArnd Bergmann 801fc0663d6SArnd Bergmann static struct rtnl_link_ops macvlan_link_ops = { 802b863ceb7SPatrick McHardy .kind = "macvlan", 8038a35747aSHerbert Xu .setup = macvlan_setup, 804b863ceb7SPatrick McHardy .newlink = macvlan_newlink, 805b863ceb7SPatrick McHardy .dellink = macvlan_dellink, 806b863ceb7SPatrick McHardy }; 807b863ceb7SPatrick McHardy 808b863ceb7SPatrick McHardy static int macvlan_device_event(struct notifier_block *unused, 809b863ceb7SPatrick McHardy unsigned long event, void *ptr) 810b863ceb7SPatrick McHardy { 811b863ceb7SPatrick McHardy struct net_device *dev = ptr; 812b863ceb7SPatrick McHardy struct macvlan_dev *vlan, *next; 813b863ceb7SPatrick McHardy struct macvlan_port *port; 814226bd341SEric Dumazet LIST_HEAD(list_kill); 815b863ceb7SPatrick McHardy 816a35e2c1bSJiri Pirko if (!macvlan_port_exists(dev)) 817b863ceb7SPatrick McHardy return NOTIFY_DONE; 818b863ceb7SPatrick McHardy 819a35e2c1bSJiri Pirko port = macvlan_port_get(dev); 820a35e2c1bSJiri Pirko 821b863ceb7SPatrick McHardy switch (event) { 822b863ceb7SPatrick McHardy case NETDEV_CHANGE: 823b863ceb7SPatrick McHardy list_for_each_entry(vlan, &port->vlans, list) 824fc4a7489SPatrick Mullaney netif_stacked_transfer_operstate(vlan->lowerdev, 825fc4a7489SPatrick Mullaney vlan->dev); 826b863ceb7SPatrick McHardy break; 827b863ceb7SPatrick McHardy case NETDEV_FEAT_CHANGE: 828b863ceb7SPatrick McHardy list_for_each_entry(vlan, &port->vlans, list) { 829b863ceb7SPatrick McHardy vlan->dev->features = dev->features & MACVLAN_FEATURES; 8308c2acc53SPatrick McHardy vlan->dev->gso_max_size = dev->gso_max_size; 831b863ceb7SPatrick McHardy netdev_features_change(vlan->dev); 832b863ceb7SPatrick McHardy } 833b863ceb7SPatrick McHardy break; 834b863ceb7SPatrick McHardy case NETDEV_UNREGISTER: 8353b27e105SDavid Lamparter /* twiddle thumbs on netns device moves */ 8363b27e105SDavid Lamparter if (dev->reg_state != NETREG_UNREGISTERING) 8373b27e105SDavid Lamparter break; 8383b27e105SDavid Lamparter 839b863ceb7SPatrick McHardy list_for_each_entry_safe(vlan, next, &port->vlans, list) 840226bd341SEric Dumazet vlan->dev->rtnl_link_ops->dellink(vlan->dev, &list_kill); 841226bd341SEric Dumazet unregister_netdevice_many(&list_kill); 842226bd341SEric Dumazet list_del(&list_kill); 843b863ceb7SPatrick McHardy break; 8441c01fe14SJiri Pirko case NETDEV_PRE_TYPE_CHANGE: 8451c01fe14SJiri Pirko /* Forbid underlaying device to change its type. */ 8461c01fe14SJiri Pirko return NOTIFY_BAD; 847b863ceb7SPatrick McHardy } 848b863ceb7SPatrick McHardy return NOTIFY_DONE; 849b863ceb7SPatrick McHardy } 850b863ceb7SPatrick McHardy 851b863ceb7SPatrick McHardy static struct notifier_block macvlan_notifier_block __read_mostly = { 852b863ceb7SPatrick McHardy .notifier_call = macvlan_device_event, 853b863ceb7SPatrick McHardy }; 854b863ceb7SPatrick McHardy 855b863ceb7SPatrick McHardy static int __init macvlan_init_module(void) 856b863ceb7SPatrick McHardy { 857b863ceb7SPatrick McHardy int err; 858b863ceb7SPatrick McHardy 859b863ceb7SPatrick McHardy register_netdevice_notifier(&macvlan_notifier_block); 860b863ceb7SPatrick McHardy 861fc0663d6SArnd Bergmann err = macvlan_link_register(&macvlan_link_ops); 862b863ceb7SPatrick McHardy if (err < 0) 863b863ceb7SPatrick McHardy goto err1; 864b863ceb7SPatrick McHardy return 0; 865b863ceb7SPatrick McHardy err1: 866b863ceb7SPatrick McHardy unregister_netdevice_notifier(&macvlan_notifier_block); 867b863ceb7SPatrick McHardy return err; 868b863ceb7SPatrick McHardy } 869b863ceb7SPatrick McHardy 870b863ceb7SPatrick McHardy static void __exit macvlan_cleanup_module(void) 871b863ceb7SPatrick McHardy { 872b863ceb7SPatrick McHardy rtnl_link_unregister(&macvlan_link_ops); 873b863ceb7SPatrick McHardy unregister_netdevice_notifier(&macvlan_notifier_block); 874b863ceb7SPatrick McHardy } 875b863ceb7SPatrick McHardy 876b863ceb7SPatrick McHardy module_init(macvlan_init_module); 877b863ceb7SPatrick McHardy module_exit(macvlan_cleanup_module); 878b863ceb7SPatrick McHardy 879b863ceb7SPatrick McHardy MODULE_LICENSE("GPL"); 880b863ceb7SPatrick McHardy MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); 881b863ceb7SPatrick McHardy MODULE_DESCRIPTION("Driver for MAC address based VLANs"); 882b863ceb7SPatrick McHardy MODULE_ALIAS_RTNL_LINK("macvlan"); 883