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) { 60a6700db1SJoe Perches if (ether_addr_equal_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 */ 99a6700db1SJoe Perches if (ether_addr_equal_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; 121a6700db1SJoe Perches if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast)) 122a1e514c5SArnd Bergmann skb->pkt_type = PACKET_BROADCAST; 123a1e514c5SArnd Bergmann else 124a1e514c5SArnd Bergmann skb->pkt_type = PACKET_MULTICAST; 125a1e514c5SArnd Bergmann 126fc0663d6SArnd Bergmann return vlan->receive(skb); 127a1e514c5SArnd Bergmann } 128a1e514c5SArnd Bergmann 129b863ceb7SPatrick McHardy static void macvlan_broadcast(struct sk_buff *skb, 130618e1b74SArnd Bergmann const struct macvlan_port *port, 131618e1b74SArnd Bergmann struct net_device *src, 132618e1b74SArnd Bergmann enum macvlan_mode mode) 133b863ceb7SPatrick McHardy { 134b863ceb7SPatrick McHardy const struct ethhdr *eth = eth_hdr(skb); 135b863ceb7SPatrick McHardy const struct macvlan_dev *vlan; 136b863ceb7SPatrick McHardy struct hlist_node *n; 137b863ceb7SPatrick McHardy struct sk_buff *nskb; 138b863ceb7SPatrick McHardy unsigned int i; 139a1e514c5SArnd Bergmann int err; 140b863ceb7SPatrick McHardy 141efbbced3SPatrick McHardy if (skb->protocol == htons(ETH_P_PAUSE)) 142efbbced3SPatrick McHardy return; 143efbbced3SPatrick McHardy 144b863ceb7SPatrick McHardy for (i = 0; i < MACVLAN_HASH_SIZE; i++) { 145b863ceb7SPatrick McHardy hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[i], hlist) { 146618e1b74SArnd Bergmann if (vlan->dev == src || !(vlan->mode & mode)) 147618e1b74SArnd Bergmann continue; 148618e1b74SArnd Bergmann 149b863ceb7SPatrick McHardy nskb = skb_clone(skb, GFP_ATOMIC); 150fc0663d6SArnd Bergmann err = macvlan_broadcast_one(nskb, vlan, eth, 151618e1b74SArnd Bergmann mode == MACVLAN_MODE_BRIDGE); 152a1e514c5SArnd Bergmann macvlan_count_rx(vlan, skb->len + ETH_HLEN, 153a1e514c5SArnd Bergmann err == NET_RX_SUCCESS, 1); 154b863ceb7SPatrick McHardy } 155b863ceb7SPatrick McHardy } 156b863ceb7SPatrick McHardy } 157b863ceb7SPatrick McHardy 158b863ceb7SPatrick McHardy /* called under rcu_read_lock() from netif_receive_skb */ 1598a4eb573SJiri Pirko static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb) 160b863ceb7SPatrick McHardy { 161ab95bfe0SJiri Pirko struct macvlan_port *port; 1628a4eb573SJiri Pirko struct sk_buff *skb = *pskb; 163b863ceb7SPatrick McHardy const struct ethhdr *eth = eth_hdr(skb); 164b863ceb7SPatrick McHardy const struct macvlan_dev *vlan; 165618e1b74SArnd Bergmann const struct macvlan_dev *src; 166b863ceb7SPatrick McHardy struct net_device *dev; 167ba01877fSSridhar Samudrala unsigned int len = 0; 168ba01877fSSridhar Samudrala int ret = NET_RX_DROP; 169b863ceb7SPatrick McHardy 170a35e2c1bSJiri Pirko port = macvlan_port_get_rcu(skb->dev); 171b863ceb7SPatrick McHardy if (is_multicast_ether_addr(eth->h_dest)) { 172bc416d97SEric Dumazet skb = ip_check_defrag(skb, IP_DEFRAG_MACVLAN); 173bc416d97SEric Dumazet if (!skb) 174bc416d97SEric Dumazet return RX_HANDLER_CONSUMED; 1754ec7ac12SEric Dumazet eth = eth_hdr(skb); 176618e1b74SArnd Bergmann src = macvlan_hash_lookup(port, eth->h_source); 177618e1b74SArnd Bergmann if (!src) 178618e1b74SArnd Bergmann /* frame comes from an external address */ 179618e1b74SArnd Bergmann macvlan_broadcast(skb, port, NULL, 180618e1b74SArnd Bergmann MACVLAN_MODE_PRIVATE | 181618e1b74SArnd Bergmann MACVLAN_MODE_VEPA | 182eb06acdcSSridhar Samudrala MACVLAN_MODE_PASSTHRU| 183618e1b74SArnd Bergmann MACVLAN_MODE_BRIDGE); 184618e1b74SArnd Bergmann else if (src->mode == MACVLAN_MODE_VEPA) 185618e1b74SArnd Bergmann /* flood to everyone except source */ 186618e1b74SArnd Bergmann macvlan_broadcast(skb, port, src->dev, 187618e1b74SArnd Bergmann MACVLAN_MODE_VEPA | 188618e1b74SArnd Bergmann MACVLAN_MODE_BRIDGE); 189618e1b74SArnd Bergmann else if (src->mode == MACVLAN_MODE_BRIDGE) 190618e1b74SArnd Bergmann /* 191618e1b74SArnd Bergmann * flood only to VEPA ports, bridge ports 192618e1b74SArnd Bergmann * already saw the frame on the way out. 193618e1b74SArnd Bergmann */ 194618e1b74SArnd Bergmann macvlan_broadcast(skb, port, src->dev, 195618e1b74SArnd Bergmann MACVLAN_MODE_VEPA); 196729e72a1Sstephen hemminger else { 197729e72a1Sstephen hemminger /* forward to original port. */ 198729e72a1Sstephen hemminger vlan = src; 199729e72a1Sstephen hemminger ret = macvlan_broadcast_one(skb, vlan, eth, 0); 200729e72a1Sstephen hemminger goto out; 201729e72a1Sstephen hemminger } 202729e72a1Sstephen hemminger 2038a4eb573SJiri Pirko return RX_HANDLER_PASS; 204b863ceb7SPatrick McHardy } 205b863ceb7SPatrick McHardy 206eb06acdcSSridhar Samudrala if (port->passthru) 207eb06acdcSSridhar Samudrala vlan = list_first_entry(&port->vlans, struct macvlan_dev, list); 208eb06acdcSSridhar Samudrala else 209b863ceb7SPatrick McHardy vlan = macvlan_hash_lookup(port, eth->h_dest); 210b863ceb7SPatrick McHardy if (vlan == NULL) 2118a4eb573SJiri Pirko return RX_HANDLER_PASS; 212b863ceb7SPatrick McHardy 213b863ceb7SPatrick McHardy dev = vlan->dev; 214b863ceb7SPatrick McHardy if (unlikely(!(dev->flags & IFF_UP))) { 215b863ceb7SPatrick McHardy kfree_skb(skb); 2168a4eb573SJiri Pirko return RX_HANDLER_CONSUMED; 217b863ceb7SPatrick McHardy } 218a1e514c5SArnd Bergmann len = skb->len + ETH_HLEN; 219b863ceb7SPatrick McHardy skb = skb_share_check(skb, GFP_ATOMIC); 220a1e514c5SArnd Bergmann if (!skb) 221ba01877fSSridhar Samudrala goto out; 222b863ceb7SPatrick McHardy 223b863ceb7SPatrick McHardy skb->dev = dev; 224b863ceb7SPatrick McHardy skb->pkt_type = PACKET_HOST; 225b863ceb7SPatrick McHardy 226ba01877fSSridhar Samudrala ret = vlan->receive(skb); 227ba01877fSSridhar Samudrala 228ba01877fSSridhar Samudrala out: 229ba01877fSSridhar Samudrala macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, 0); 2308a4eb573SJiri Pirko return RX_HANDLER_CONSUMED; 231b863ceb7SPatrick McHardy } 232b863ceb7SPatrick McHardy 233618e1b74SArnd Bergmann static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev) 234618e1b74SArnd Bergmann { 235618e1b74SArnd Bergmann const struct macvlan_dev *vlan = netdev_priv(dev); 236618e1b74SArnd Bergmann const struct macvlan_port *port = vlan->port; 237618e1b74SArnd Bergmann const struct macvlan_dev *dest; 23812a2856bSDaniel Lezcano __u8 ip_summed = skb->ip_summed; 239618e1b74SArnd Bergmann 240618e1b74SArnd Bergmann if (vlan->mode == MACVLAN_MODE_BRIDGE) { 241618e1b74SArnd Bergmann const struct ethhdr *eth = (void *)skb->data; 24212a2856bSDaniel Lezcano skb->ip_summed = CHECKSUM_UNNECESSARY; 243618e1b74SArnd Bergmann 244618e1b74SArnd Bergmann /* send to other bridge ports directly */ 245618e1b74SArnd Bergmann if (is_multicast_ether_addr(eth->h_dest)) { 246618e1b74SArnd Bergmann macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE); 247618e1b74SArnd Bergmann goto xmit_world; 248618e1b74SArnd Bergmann } 249618e1b74SArnd Bergmann 250618e1b74SArnd Bergmann dest = macvlan_hash_lookup(port, eth->h_dest); 251618e1b74SArnd Bergmann if (dest && dest->mode == MACVLAN_MODE_BRIDGE) { 252a37dd333SDavid Ward /* send to lowerdev first for its network taps */ 253cb2d0f3eSDavid Ward dev_forward_skb(vlan->lowerdev, skb); 254618e1b74SArnd Bergmann 255618e1b74SArnd Bergmann return NET_XMIT_SUCCESS; 256618e1b74SArnd Bergmann } 257618e1b74SArnd Bergmann } 258618e1b74SArnd Bergmann 259618e1b74SArnd Bergmann xmit_world: 26012a2856bSDaniel Lezcano skb->ip_summed = ip_summed; 26159b9997bSDavid S. Miller skb->dev = vlan->lowerdev; 262618e1b74SArnd Bergmann return dev_queue_xmit(skb); 263618e1b74SArnd Bergmann } 264618e1b74SArnd Bergmann 265fc0663d6SArnd Bergmann netdev_tx_t macvlan_start_xmit(struct sk_buff *skb, 266424efe9cSStephen Hemminger struct net_device *dev) 267b863ceb7SPatrick McHardy { 268b863ceb7SPatrick McHardy unsigned int len = skb->len; 269b863ceb7SPatrick McHardy int ret; 2708ffab51bSEric Dumazet const struct macvlan_dev *vlan = netdev_priv(dev); 271b863ceb7SPatrick McHardy 272618e1b74SArnd Bergmann ret = macvlan_queue_xmit(skb, dev); 2732d6c9ffcSEric Dumazet if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) { 2748ffab51bSEric Dumazet struct macvlan_pcpu_stats *pcpu_stats; 2752c114553SEric Dumazet 2768ffab51bSEric Dumazet pcpu_stats = this_cpu_ptr(vlan->pcpu_stats); 2778ffab51bSEric Dumazet u64_stats_update_begin(&pcpu_stats->syncp); 2788ffab51bSEric Dumazet pcpu_stats->tx_packets++; 2798ffab51bSEric Dumazet pcpu_stats->tx_bytes += len; 2808ffab51bSEric Dumazet u64_stats_update_end(&pcpu_stats->syncp); 2818ffab51bSEric Dumazet } else { 2828ffab51bSEric Dumazet this_cpu_inc(vlan->pcpu_stats->tx_dropped); 2838ffab51bSEric Dumazet } 284cbbef5e1SPatrick McHardy return ret; 285b863ceb7SPatrick McHardy } 286fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_start_xmit); 287b863ceb7SPatrick McHardy 288b863ceb7SPatrick McHardy static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev, 2893b04dddeSStephen Hemminger unsigned short type, const void *daddr, 2903b04dddeSStephen Hemminger const void *saddr, unsigned len) 291b863ceb7SPatrick McHardy { 292b863ceb7SPatrick McHardy const struct macvlan_dev *vlan = netdev_priv(dev); 293b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 294b863ceb7SPatrick McHardy 2950c4e8581SStephen Hemminger return dev_hard_header(skb, lowerdev, type, daddr, 296b863ceb7SPatrick McHardy saddr ? : dev->dev_addr, len); 297b863ceb7SPatrick McHardy } 298b863ceb7SPatrick McHardy 2993b04dddeSStephen Hemminger static const struct header_ops macvlan_hard_header_ops = { 3003b04dddeSStephen Hemminger .create = macvlan_hard_header, 3013b04dddeSStephen Hemminger .rebuild = eth_rebuild_header, 3023b04dddeSStephen Hemminger .parse = eth_header_parse, 3033b04dddeSStephen Hemminger .cache = eth_header_cache, 3043b04dddeSStephen Hemminger .cache_update = eth_header_cache_update, 3053b04dddeSStephen Hemminger }; 3063b04dddeSStephen Hemminger 307b863ceb7SPatrick McHardy static int macvlan_open(struct net_device *dev) 308b863ceb7SPatrick McHardy { 309b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 310b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 311b863ceb7SPatrick McHardy int err; 312b863ceb7SPatrick McHardy 313eb06acdcSSridhar Samudrala if (vlan->port->passthru) { 314df8ef8f3SJohn Fastabend if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) 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 347df8ef8f3SJohn Fastabend dev_uc_unsync(lowerdev, dev); 348df8ef8f3SJohn Fastabend dev_mc_unsync(lowerdev, dev); 349df8ef8f3SJohn Fastabend 350eb06acdcSSridhar Samudrala if (vlan->port->passthru) { 351df8ef8f3SJohn Fastabend if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) 352eb06acdcSSridhar Samudrala dev_set_promiscuity(lowerdev, -1); 353eb06acdcSSridhar Samudrala goto hash_del; 354eb06acdcSSridhar Samudrala } 355eb06acdcSSridhar Samudrala 356b863ceb7SPatrick McHardy if (dev->flags & IFF_ALLMULTI) 357b863ceb7SPatrick McHardy dev_set_allmulti(lowerdev, -1); 358b863ceb7SPatrick McHardy 359a748ee24SJiri Pirko dev_uc_del(lowerdev, dev->dev_addr); 360b863ceb7SPatrick McHardy 361eb06acdcSSridhar Samudrala hash_del: 362449f4544SEric Dumazet macvlan_hash_del(vlan, !dev->dismantle); 363b863ceb7SPatrick McHardy return 0; 364b863ceb7SPatrick McHardy } 365b863ceb7SPatrick McHardy 366ad5d20a6SPatrick McHardy static int macvlan_set_mac_address(struct net_device *dev, void *p) 367ad5d20a6SPatrick McHardy { 368ad5d20a6SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 369ad5d20a6SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 370ad5d20a6SPatrick McHardy struct sockaddr *addr = p; 371ad5d20a6SPatrick McHardy int err; 372ad5d20a6SPatrick McHardy 373ad5d20a6SPatrick McHardy if (!is_valid_ether_addr(addr->sa_data)) 374ad5d20a6SPatrick McHardy return -EADDRNOTAVAIL; 375ad5d20a6SPatrick McHardy 376f9ac30f0SEric Biederman if (!(dev->flags & IFF_UP)) { 377f9ac30f0SEric Biederman /* Just copy in the new address */ 3787ce5d222SDanny Kukawka dev->addr_assign_type &= ~NET_ADDR_RANDOM; 379f9ac30f0SEric Biederman memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN); 380f9ac30f0SEric Biederman } else { 381f9ac30f0SEric Biederman /* Rehash and update the device filters */ 382f9ac30f0SEric Biederman if (macvlan_addr_busy(vlan->port, addr->sa_data)) 383f9ac30f0SEric Biederman return -EBUSY; 384ad5d20a6SPatrick McHardy 385a748ee24SJiri Pirko err = dev_uc_add(lowerdev, addr->sa_data); 386ccffad25SJiri Pirko if (err) 387ad5d20a6SPatrick McHardy return err; 388f9ac30f0SEric Biederman 389a748ee24SJiri Pirko dev_uc_del(lowerdev, dev->dev_addr); 390ad5d20a6SPatrick McHardy 391f9ac30f0SEric Biederman macvlan_hash_change_addr(vlan, addr->sa_data); 392f9ac30f0SEric Biederman } 393ad5d20a6SPatrick McHardy return 0; 394ad5d20a6SPatrick McHardy } 395ad5d20a6SPatrick McHardy 396b863ceb7SPatrick McHardy static void macvlan_change_rx_flags(struct net_device *dev, int change) 397b863ceb7SPatrick McHardy { 398b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 399b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 400b863ceb7SPatrick McHardy 401b863ceb7SPatrick McHardy if (change & IFF_ALLMULTI) 402b863ceb7SPatrick McHardy dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1); 403b863ceb7SPatrick McHardy } 404b863ceb7SPatrick McHardy 405df8ef8f3SJohn Fastabend static void macvlan_set_mac_lists(struct net_device *dev) 406b863ceb7SPatrick McHardy { 407b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 408b863ceb7SPatrick McHardy 409df8ef8f3SJohn Fastabend dev_uc_sync(vlan->lowerdev, dev); 410b863ceb7SPatrick McHardy dev_mc_sync(vlan->lowerdev, dev); 411b863ceb7SPatrick McHardy } 412b863ceb7SPatrick McHardy 413b863ceb7SPatrick McHardy static int macvlan_change_mtu(struct net_device *dev, int new_mtu) 414b863ceb7SPatrick McHardy { 415b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 416b863ceb7SPatrick McHardy 417b863ceb7SPatrick McHardy if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu) 418b863ceb7SPatrick McHardy return -EINVAL; 419b863ceb7SPatrick McHardy dev->mtu = new_mtu; 420b863ceb7SPatrick McHardy return 0; 421b863ceb7SPatrick McHardy } 422b863ceb7SPatrick McHardy 423b863ceb7SPatrick McHardy /* 424b863ceb7SPatrick McHardy * macvlan network devices have devices nesting below it and are a special 425b863ceb7SPatrick McHardy * "super class" of normal network devices; split their locks off into a 426b863ceb7SPatrick McHardy * separate class since they always nest. 427b863ceb7SPatrick McHardy */ 428b863ceb7SPatrick McHardy static struct lock_class_key macvlan_netdev_xmit_lock_key; 429cf508b12SDavid S. Miller static struct lock_class_key macvlan_netdev_addr_lock_key; 430b863ceb7SPatrick McHardy 431b863ceb7SPatrick McHardy #define MACVLAN_FEATURES \ 432b863ceb7SPatrick McHardy (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \ 433b863ceb7SPatrick McHardy NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \ 4348d13e670SJohn Fastabend NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \ 4358d13e670SJohn Fastabend NETIF_F_HW_VLAN_FILTER) 436b863ceb7SPatrick McHardy 437b863ceb7SPatrick McHardy #define MACVLAN_STATE_MASK \ 438b863ceb7SPatrick McHardy ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT)) 439b863ceb7SPatrick McHardy 440e8a0464cSDavid S. Miller static void macvlan_set_lockdep_class_one(struct net_device *dev, 441e8a0464cSDavid S. Miller struct netdev_queue *txq, 442e8a0464cSDavid S. Miller void *_unused) 443c773e847SDavid S. Miller { 444c773e847SDavid S. Miller lockdep_set_class(&txq->_xmit_lock, 445c773e847SDavid S. Miller &macvlan_netdev_xmit_lock_key); 446c773e847SDavid S. Miller } 447c773e847SDavid S. Miller 448c773e847SDavid S. Miller static void macvlan_set_lockdep_class(struct net_device *dev) 449c773e847SDavid S. Miller { 450cf508b12SDavid S. Miller lockdep_set_class(&dev->addr_list_lock, 451cf508b12SDavid S. Miller &macvlan_netdev_addr_lock_key); 452e8a0464cSDavid S. Miller netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL); 453c773e847SDavid S. Miller } 454c773e847SDavid S. Miller 455b863ceb7SPatrick McHardy static int macvlan_init(struct net_device *dev) 456b863ceb7SPatrick McHardy { 457b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 458b863ceb7SPatrick McHardy const struct net_device *lowerdev = vlan->lowerdev; 459b863ceb7SPatrick McHardy 460b863ceb7SPatrick McHardy dev->state = (dev->state & ~MACVLAN_STATE_MASK) | 461b863ceb7SPatrick McHardy (lowerdev->state & MACVLAN_STATE_MASK); 462b863ceb7SPatrick McHardy dev->features = lowerdev->features & MACVLAN_FEATURES; 4638ffab51bSEric Dumazet dev->features |= NETIF_F_LLTX; 4648c2acc53SPatrick McHardy dev->gso_max_size = lowerdev->gso_max_size; 465b863ceb7SPatrick McHardy dev->iflink = lowerdev->ifindex; 466ef5c8996Ssg.tweak@gmail.com dev->hard_header_len = lowerdev->hard_header_len; 467b863ceb7SPatrick McHardy 468c773e847SDavid S. Miller macvlan_set_lockdep_class(dev); 469c773e847SDavid S. Miller 4708ffab51bSEric Dumazet vlan->pcpu_stats = alloc_percpu(struct macvlan_pcpu_stats); 4718ffab51bSEric Dumazet if (!vlan->pcpu_stats) 472fccaf710SEric Dumazet return -ENOMEM; 473fccaf710SEric Dumazet 474b863ceb7SPatrick McHardy return 0; 475b863ceb7SPatrick McHardy } 476b863ceb7SPatrick McHardy 477fccaf710SEric Dumazet static void macvlan_uninit(struct net_device *dev) 478fccaf710SEric Dumazet { 479fccaf710SEric Dumazet struct macvlan_dev *vlan = netdev_priv(dev); 480d5cd9244SEric W. Biederman struct macvlan_port *port = vlan->port; 481fccaf710SEric Dumazet 4828ffab51bSEric Dumazet free_percpu(vlan->pcpu_stats); 483d5cd9244SEric W. Biederman 484d5cd9244SEric W. Biederman port->count -= 1; 485d5cd9244SEric W. Biederman if (!port->count) 486d5cd9244SEric W. Biederman macvlan_port_destroy(port->dev); 487fccaf710SEric Dumazet } 488fccaf710SEric Dumazet 48928172739SEric Dumazet static struct rtnl_link_stats64 *macvlan_dev_get_stats64(struct net_device *dev, 49028172739SEric Dumazet struct rtnl_link_stats64 *stats) 491fccaf710SEric Dumazet { 492fccaf710SEric Dumazet struct macvlan_dev *vlan = netdev_priv(dev); 493fccaf710SEric Dumazet 4948ffab51bSEric Dumazet if (vlan->pcpu_stats) { 4958ffab51bSEric Dumazet struct macvlan_pcpu_stats *p; 4968ffab51bSEric Dumazet u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes; 4978ffab51bSEric Dumazet u32 rx_errors = 0, tx_dropped = 0; 498bc66154eSEric Dumazet unsigned int start; 499fccaf710SEric Dumazet int i; 500fccaf710SEric Dumazet 501fccaf710SEric Dumazet for_each_possible_cpu(i) { 5028ffab51bSEric Dumazet p = per_cpu_ptr(vlan->pcpu_stats, i); 503bc66154eSEric Dumazet do { 504bc66154eSEric Dumazet start = u64_stats_fetch_begin_bh(&p->syncp); 505bc66154eSEric Dumazet rx_packets = p->rx_packets; 506bc66154eSEric Dumazet rx_bytes = p->rx_bytes; 507bc66154eSEric Dumazet rx_multicast = p->rx_multicast; 5088ffab51bSEric Dumazet tx_packets = p->tx_packets; 5098ffab51bSEric Dumazet tx_bytes = p->tx_bytes; 510bc66154eSEric Dumazet } while (u64_stats_fetch_retry_bh(&p->syncp, start)); 5118ffab51bSEric Dumazet 5128ffab51bSEric Dumazet stats->rx_packets += rx_packets; 5138ffab51bSEric Dumazet stats->rx_bytes += rx_bytes; 5148ffab51bSEric Dumazet stats->multicast += rx_multicast; 5158ffab51bSEric Dumazet stats->tx_packets += tx_packets; 5168ffab51bSEric Dumazet stats->tx_bytes += tx_bytes; 5178ffab51bSEric Dumazet /* rx_errors & tx_dropped are u32, updated 5188ffab51bSEric Dumazet * without syncp protection. 5198ffab51bSEric Dumazet */ 5208ffab51bSEric Dumazet rx_errors += p->rx_errors; 5218ffab51bSEric Dumazet tx_dropped += p->tx_dropped; 522fccaf710SEric Dumazet } 5238ffab51bSEric Dumazet stats->rx_errors = rx_errors; 5248ffab51bSEric Dumazet stats->rx_dropped = rx_errors; 5258ffab51bSEric Dumazet stats->tx_dropped = tx_dropped; 526fccaf710SEric Dumazet } 527fccaf710SEric Dumazet return stats; 528fccaf710SEric Dumazet } 529fccaf710SEric Dumazet 5308e586137SJiri Pirko static int macvlan_vlan_rx_add_vid(struct net_device *dev, 5318d13e670SJohn Fastabend unsigned short vid) 5328d13e670SJohn Fastabend { 5338d13e670SJohn Fastabend struct macvlan_dev *vlan = netdev_priv(dev); 5348d13e670SJohn Fastabend struct net_device *lowerdev = vlan->lowerdev; 5358d13e670SJohn Fastabend 53687002b03SJiri Pirko return vlan_vid_add(lowerdev, vid); 5378d13e670SJohn Fastabend } 5388d13e670SJohn Fastabend 5398e586137SJiri Pirko static int macvlan_vlan_rx_kill_vid(struct net_device *dev, 5408d13e670SJohn Fastabend unsigned short vid) 5418d13e670SJohn Fastabend { 5428d13e670SJohn Fastabend struct macvlan_dev *vlan = netdev_priv(dev); 5438d13e670SJohn Fastabend struct net_device *lowerdev = vlan->lowerdev; 5448d13e670SJohn Fastabend 54587002b03SJiri Pirko vlan_vid_del(lowerdev, vid); 5468e586137SJiri Pirko return 0; 5478d13e670SJohn Fastabend } 5488d13e670SJohn Fastabend 549edc7d573Sstephen hemminger static int macvlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], 550df8ef8f3SJohn Fastabend struct net_device *dev, 5516b6e2725Sstephen hemminger const unsigned char *addr, 552df8ef8f3SJohn Fastabend u16 flags) 553df8ef8f3SJohn Fastabend { 554df8ef8f3SJohn Fastabend struct macvlan_dev *vlan = netdev_priv(dev); 555df8ef8f3SJohn Fastabend int err = -EINVAL; 556df8ef8f3SJohn Fastabend 557df8ef8f3SJohn Fastabend if (!vlan->port->passthru) 558df8ef8f3SJohn Fastabend return -EOPNOTSUPP; 559df8ef8f3SJohn Fastabend 560df8ef8f3SJohn Fastabend if (is_unicast_ether_addr(addr)) 561df8ef8f3SJohn Fastabend err = dev_uc_add_excl(dev, addr); 562df8ef8f3SJohn Fastabend else if (is_multicast_ether_addr(addr)) 563df8ef8f3SJohn Fastabend err = dev_mc_add_excl(dev, addr); 564df8ef8f3SJohn Fastabend 565df8ef8f3SJohn Fastabend return err; 566df8ef8f3SJohn Fastabend } 567df8ef8f3SJohn Fastabend 568df8ef8f3SJohn Fastabend static int macvlan_fdb_del(struct ndmsg *ndm, 569df8ef8f3SJohn Fastabend struct net_device *dev, 5706b6e2725Sstephen hemminger const unsigned char *addr) 571df8ef8f3SJohn Fastabend { 572df8ef8f3SJohn Fastabend struct macvlan_dev *vlan = netdev_priv(dev); 573df8ef8f3SJohn Fastabend int err = -EINVAL; 574df8ef8f3SJohn Fastabend 575df8ef8f3SJohn Fastabend if (!vlan->port->passthru) 576df8ef8f3SJohn Fastabend return -EOPNOTSUPP; 577df8ef8f3SJohn Fastabend 578df8ef8f3SJohn Fastabend if (is_unicast_ether_addr(addr)) 579df8ef8f3SJohn Fastabend err = dev_uc_del(dev, addr); 580df8ef8f3SJohn Fastabend else if (is_multicast_ether_addr(addr)) 581df8ef8f3SJohn Fastabend err = dev_mc_del(dev, addr); 582df8ef8f3SJohn Fastabend 583df8ef8f3SJohn Fastabend return err; 584df8ef8f3SJohn Fastabend } 585df8ef8f3SJohn Fastabend 586b863ceb7SPatrick McHardy static void macvlan_ethtool_get_drvinfo(struct net_device *dev, 587b863ceb7SPatrick McHardy struct ethtool_drvinfo *drvinfo) 588b863ceb7SPatrick McHardy { 589b863ceb7SPatrick McHardy snprintf(drvinfo->driver, 32, "macvlan"); 590b863ceb7SPatrick McHardy snprintf(drvinfo->version, 32, "0.1"); 591b863ceb7SPatrick McHardy } 592b863ceb7SPatrick McHardy 5939edb8bb6SStephen Hemminger static int macvlan_ethtool_get_settings(struct net_device *dev, 5949edb8bb6SStephen Hemminger struct ethtool_cmd *cmd) 5959edb8bb6SStephen Hemminger { 5969edb8bb6SStephen Hemminger const struct macvlan_dev *vlan = netdev_priv(dev); 5974bc71cb9SJiri Pirko 5984bc71cb9SJiri Pirko return __ethtool_get_settings(vlan->lowerdev, cmd); 5999edb8bb6SStephen Hemminger } 6009edb8bb6SStephen Hemminger 601b863ceb7SPatrick McHardy static const struct ethtool_ops macvlan_ethtool_ops = { 602b863ceb7SPatrick McHardy .get_link = ethtool_op_get_link, 6039edb8bb6SStephen Hemminger .get_settings = macvlan_ethtool_get_settings, 604b863ceb7SPatrick McHardy .get_drvinfo = macvlan_ethtool_get_drvinfo, 605b863ceb7SPatrick McHardy }; 606b863ceb7SPatrick McHardy 60754a30c97SStephen Hemminger static const struct net_device_ops macvlan_netdev_ops = { 60854a30c97SStephen Hemminger .ndo_init = macvlan_init, 609fccaf710SEric Dumazet .ndo_uninit = macvlan_uninit, 61054a30c97SStephen Hemminger .ndo_open = macvlan_open, 61154a30c97SStephen Hemminger .ndo_stop = macvlan_stop, 61200829823SStephen Hemminger .ndo_start_xmit = macvlan_start_xmit, 61354a30c97SStephen Hemminger .ndo_change_mtu = macvlan_change_mtu, 61454a30c97SStephen Hemminger .ndo_change_rx_flags = macvlan_change_rx_flags, 61554a30c97SStephen Hemminger .ndo_set_mac_address = macvlan_set_mac_address, 616df8ef8f3SJohn Fastabend .ndo_set_rx_mode = macvlan_set_mac_lists, 617bc66154eSEric Dumazet .ndo_get_stats64 = macvlan_dev_get_stats64, 61854a30c97SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 6198d13e670SJohn Fastabend .ndo_vlan_rx_add_vid = macvlan_vlan_rx_add_vid, 6208d13e670SJohn Fastabend .ndo_vlan_rx_kill_vid = macvlan_vlan_rx_kill_vid, 621df8ef8f3SJohn Fastabend .ndo_fdb_add = macvlan_fdb_add, 622df8ef8f3SJohn Fastabend .ndo_fdb_del = macvlan_fdb_del, 623df8ef8f3SJohn Fastabend .ndo_fdb_dump = ndo_dflt_fdb_dump, 62454a30c97SStephen Hemminger }; 62554a30c97SStephen Hemminger 6268a35747aSHerbert Xu void macvlan_common_setup(struct net_device *dev) 627b863ceb7SPatrick McHardy { 628b863ceb7SPatrick McHardy ether_setup(dev); 629b863ceb7SPatrick McHardy 630550fd08cSNeil Horman dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING); 63154a30c97SStephen Hemminger dev->netdev_ops = &macvlan_netdev_ops; 632b863ceb7SPatrick McHardy dev->destructor = free_netdev; 6333b04dddeSStephen Hemminger dev->header_ops = &macvlan_hard_header_ops, 634b863ceb7SPatrick McHardy dev->ethtool_ops = &macvlan_ethtool_ops; 6358a35747aSHerbert Xu } 6368a35747aSHerbert Xu EXPORT_SYMBOL_GPL(macvlan_common_setup); 6378a35747aSHerbert Xu 6388a35747aSHerbert Xu static void macvlan_setup(struct net_device *dev) 6398a35747aSHerbert Xu { 6408a35747aSHerbert Xu macvlan_common_setup(dev); 641b863ceb7SPatrick McHardy dev->tx_queue_len = 0; 642b863ceb7SPatrick McHardy } 643b863ceb7SPatrick McHardy 644b863ceb7SPatrick McHardy static int macvlan_port_create(struct net_device *dev) 645b863ceb7SPatrick McHardy { 646b863ceb7SPatrick McHardy struct macvlan_port *port; 647b863ceb7SPatrick McHardy unsigned int i; 648ab95bfe0SJiri Pirko int err; 649b863ceb7SPatrick McHardy 650b863ceb7SPatrick McHardy if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK) 651b863ceb7SPatrick McHardy return -EINVAL; 652b863ceb7SPatrick McHardy 653b863ceb7SPatrick McHardy port = kzalloc(sizeof(*port), GFP_KERNEL); 654b863ceb7SPatrick McHardy if (port == NULL) 655b863ceb7SPatrick McHardy return -ENOMEM; 656b863ceb7SPatrick McHardy 657eb06acdcSSridhar Samudrala port->passthru = false; 658b863ceb7SPatrick McHardy port->dev = dev; 659b863ceb7SPatrick McHardy INIT_LIST_HEAD(&port->vlans); 660b863ceb7SPatrick McHardy for (i = 0; i < MACVLAN_HASH_SIZE; i++) 661b863ceb7SPatrick McHardy INIT_HLIST_HEAD(&port->vlan_hash[i]); 662ab95bfe0SJiri Pirko 663a35e2c1bSJiri Pirko err = netdev_rx_handler_register(dev, macvlan_handle_frame, port); 664a35e2c1bSJiri Pirko if (err) 665ab95bfe0SJiri Pirko kfree(port); 666d9351561SEric Dumazet else 667a35e2c1bSJiri Pirko dev->priv_flags |= IFF_MACVLAN_PORT; 668ab95bfe0SJiri Pirko return err; 669b863ceb7SPatrick McHardy } 670b863ceb7SPatrick McHardy 671b863ceb7SPatrick McHardy static void macvlan_port_destroy(struct net_device *dev) 672b863ceb7SPatrick McHardy { 673a35e2c1bSJiri Pirko struct macvlan_port *port = macvlan_port_get(dev); 674b863ceb7SPatrick McHardy 675a35e2c1bSJiri Pirko dev->priv_flags &= ~IFF_MACVLAN_PORT; 676ab95bfe0SJiri Pirko netdev_rx_handler_unregister(dev); 6776e070aecSLai Jiangshan kfree_rcu(port, rcu); 678b863ceb7SPatrick McHardy } 679b863ceb7SPatrick McHardy 680b863ceb7SPatrick McHardy static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[]) 681b863ceb7SPatrick McHardy { 682b863ceb7SPatrick McHardy if (tb[IFLA_ADDRESS]) { 683b863ceb7SPatrick McHardy if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) 684b863ceb7SPatrick McHardy return -EINVAL; 685b863ceb7SPatrick McHardy if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) 686b863ceb7SPatrick McHardy return -EADDRNOTAVAIL; 687b863ceb7SPatrick McHardy } 68827c0b1a8SArnd Bergmann 68927c0b1a8SArnd Bergmann if (data && data[IFLA_MACVLAN_MODE]) { 69027c0b1a8SArnd Bergmann switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) { 69127c0b1a8SArnd Bergmann case MACVLAN_MODE_PRIVATE: 69227c0b1a8SArnd Bergmann case MACVLAN_MODE_VEPA: 69327c0b1a8SArnd Bergmann case MACVLAN_MODE_BRIDGE: 694eb06acdcSSridhar Samudrala case MACVLAN_MODE_PASSTHRU: 69527c0b1a8SArnd Bergmann break; 69627c0b1a8SArnd Bergmann default: 69727c0b1a8SArnd Bergmann return -EINVAL; 69827c0b1a8SArnd Bergmann } 69927c0b1a8SArnd Bergmann } 700b863ceb7SPatrick McHardy return 0; 701b863ceb7SPatrick McHardy } 702b863ceb7SPatrick McHardy 703fc0663d6SArnd Bergmann int macvlan_common_newlink(struct net *src_net, struct net_device *dev, 704fc0663d6SArnd Bergmann struct nlattr *tb[], struct nlattr *data[], 705fc0663d6SArnd Bergmann int (*receive)(struct sk_buff *skb), 706fc0663d6SArnd Bergmann int (*forward)(struct net_device *dev, 707fc0663d6SArnd Bergmann struct sk_buff *skb)) 708b863ceb7SPatrick McHardy { 709b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 710b863ceb7SPatrick McHardy struct macvlan_port *port; 711b863ceb7SPatrick McHardy struct net_device *lowerdev; 712b863ceb7SPatrick McHardy int err; 713b863ceb7SPatrick McHardy 714b863ceb7SPatrick McHardy if (!tb[IFLA_LINK]) 715b863ceb7SPatrick McHardy return -EINVAL; 716b863ceb7SPatrick McHardy 71781adee47SEric W. Biederman lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK])); 718b863ceb7SPatrick McHardy if (lowerdev == NULL) 719b863ceb7SPatrick McHardy return -ENODEV; 720b863ceb7SPatrick McHardy 721b0832a29SEric Biederman /* When creating macvlans on top of other macvlans - use 722b0832a29SEric Biederman * the real device as the lowerdev. 723a6ca5f1dSPatrick McHardy */ 724b0832a29SEric Biederman if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) { 725b0832a29SEric Biederman struct macvlan_dev *lowervlan = netdev_priv(lowerdev); 726b0832a29SEric Biederman lowerdev = lowervlan->lowerdev; 727b0832a29SEric Biederman } 728a6ca5f1dSPatrick McHardy 729b863ceb7SPatrick McHardy if (!tb[IFLA_MTU]) 730b863ceb7SPatrick McHardy dev->mtu = lowerdev->mtu; 731b863ceb7SPatrick McHardy else if (dev->mtu > lowerdev->mtu) 732b863ceb7SPatrick McHardy return -EINVAL; 733b863ceb7SPatrick McHardy 734b863ceb7SPatrick McHardy if (!tb[IFLA_ADDRESS]) 7357ce5d222SDanny Kukawka eth_hw_addr_random(dev); 736b863ceb7SPatrick McHardy 737a35e2c1bSJiri Pirko if (!macvlan_port_exists(lowerdev)) { 738b863ceb7SPatrick McHardy err = macvlan_port_create(lowerdev); 739b863ceb7SPatrick McHardy if (err < 0) 740b863ceb7SPatrick McHardy return err; 741b863ceb7SPatrick McHardy } 742a35e2c1bSJiri Pirko port = macvlan_port_get(lowerdev); 743b863ceb7SPatrick McHardy 744eb06acdcSSridhar Samudrala /* Only 1 macvlan device can be created in passthru mode */ 745eb06acdcSSridhar Samudrala if (port->passthru) 746eb06acdcSSridhar Samudrala return -EINVAL; 747eb06acdcSSridhar Samudrala 748b863ceb7SPatrick McHardy vlan->lowerdev = lowerdev; 749b863ceb7SPatrick McHardy vlan->dev = dev; 750b863ceb7SPatrick McHardy vlan->port = port; 751fc0663d6SArnd Bergmann vlan->receive = receive; 752fc0663d6SArnd Bergmann vlan->forward = forward; 753b863ceb7SPatrick McHardy 75427c0b1a8SArnd Bergmann vlan->mode = MACVLAN_MODE_VEPA; 75527c0b1a8SArnd Bergmann if (data && data[IFLA_MACVLAN_MODE]) 75627c0b1a8SArnd Bergmann vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]); 75727c0b1a8SArnd Bergmann 758df8ef8f3SJohn Fastabend if (data && data[IFLA_MACVLAN_FLAGS]) 759df8ef8f3SJohn Fastabend vlan->flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]); 760df8ef8f3SJohn Fastabend 761eb06acdcSSridhar Samudrala if (vlan->mode == MACVLAN_MODE_PASSTHRU) { 762d5cd9244SEric W. Biederman if (port->count) 763eb06acdcSSridhar Samudrala return -EINVAL; 764eb06acdcSSridhar Samudrala port->passthru = true; 765eb06acdcSSridhar Samudrala memcpy(dev->dev_addr, lowerdev->dev_addr, ETH_ALEN); 766eb06acdcSSridhar Samudrala } 767eb06acdcSSridhar Samudrala 768d5cd9244SEric W. Biederman port->count += 1; 769b863ceb7SPatrick McHardy err = register_netdevice(dev); 770b863ceb7SPatrick McHardy if (err < 0) 771f16d3d57SJiri Pirko goto destroy_port; 772b863ceb7SPatrick McHardy 773b863ceb7SPatrick McHardy list_add_tail(&vlan->list, &port->vlans); 774fc4a7489SPatrick Mullaney netif_stacked_transfer_operstate(lowerdev, dev); 775f16d3d57SJiri Pirko 776b863ceb7SPatrick McHardy return 0; 777f16d3d57SJiri Pirko 778f16d3d57SJiri Pirko destroy_port: 779d5cd9244SEric W. Biederman port->count -= 1; 780d5cd9244SEric W. Biederman if (!port->count) 781f16d3d57SJiri Pirko macvlan_port_destroy(lowerdev); 782f16d3d57SJiri Pirko 783f16d3d57SJiri Pirko return err; 784b863ceb7SPatrick McHardy } 785fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_common_newlink); 786b863ceb7SPatrick McHardy 787fc0663d6SArnd Bergmann static int macvlan_newlink(struct net *src_net, struct net_device *dev, 788fc0663d6SArnd Bergmann struct nlattr *tb[], struct nlattr *data[]) 789fc0663d6SArnd Bergmann { 790fc0663d6SArnd Bergmann return macvlan_common_newlink(src_net, dev, tb, data, 791fc0663d6SArnd Bergmann netif_rx, 792fc0663d6SArnd Bergmann dev_forward_skb); 793fc0663d6SArnd Bergmann } 794fc0663d6SArnd Bergmann 795fc0663d6SArnd Bergmann void macvlan_dellink(struct net_device *dev, struct list_head *head) 796b863ceb7SPatrick McHardy { 797b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 798b863ceb7SPatrick McHardy 799b863ceb7SPatrick McHardy list_del(&vlan->list); 80023289a37SEric Dumazet unregister_netdevice_queue(dev, head); 801b863ceb7SPatrick McHardy } 802fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_dellink); 803b863ceb7SPatrick McHardy 80427c0b1a8SArnd Bergmann static int macvlan_changelink(struct net_device *dev, 80527c0b1a8SArnd Bergmann struct nlattr *tb[], struct nlattr *data[]) 80627c0b1a8SArnd Bergmann { 80727c0b1a8SArnd Bergmann struct macvlan_dev *vlan = netdev_priv(dev); 80827c0b1a8SArnd Bergmann if (data && data[IFLA_MACVLAN_MODE]) 80927c0b1a8SArnd Bergmann vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]); 810df8ef8f3SJohn Fastabend if (data && data[IFLA_MACVLAN_FLAGS]) { 811df8ef8f3SJohn Fastabend __u16 flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]); 812df8ef8f3SJohn Fastabend bool promisc = (flags ^ vlan->flags) & MACVLAN_FLAG_NOPROMISC; 813df8ef8f3SJohn Fastabend 814df8ef8f3SJohn Fastabend if (promisc && (flags & MACVLAN_FLAG_NOPROMISC)) 815df8ef8f3SJohn Fastabend dev_set_promiscuity(vlan->lowerdev, -1); 816df8ef8f3SJohn Fastabend else if (promisc && !(flags & MACVLAN_FLAG_NOPROMISC)) 817df8ef8f3SJohn Fastabend dev_set_promiscuity(vlan->lowerdev, 1); 818df8ef8f3SJohn Fastabend vlan->flags = flags; 819df8ef8f3SJohn Fastabend } 82027c0b1a8SArnd Bergmann return 0; 82127c0b1a8SArnd Bergmann } 82227c0b1a8SArnd Bergmann 82327c0b1a8SArnd Bergmann static size_t macvlan_get_size(const struct net_device *dev) 82427c0b1a8SArnd Bergmann { 825*01fe944fSEric Dumazet return (0 826*01fe944fSEric Dumazet + nla_total_size(4) /* IFLA_MACVLAN_MODE */ 827*01fe944fSEric Dumazet + nla_total_size(2) /* IFLA_MACVLAN_FLAGS */ 828*01fe944fSEric Dumazet ); 82927c0b1a8SArnd Bergmann } 83027c0b1a8SArnd Bergmann 83127c0b1a8SArnd Bergmann static int macvlan_fill_info(struct sk_buff *skb, 83227c0b1a8SArnd Bergmann const struct net_device *dev) 83327c0b1a8SArnd Bergmann { 83427c0b1a8SArnd Bergmann struct macvlan_dev *vlan = netdev_priv(dev); 83527c0b1a8SArnd Bergmann 836ead9a76cSDavid S. Miller if (nla_put_u32(skb, IFLA_MACVLAN_MODE, vlan->mode)) 837ead9a76cSDavid S. Miller goto nla_put_failure; 838df8ef8f3SJohn Fastabend if (nla_put_u16(skb, IFLA_MACVLAN_FLAGS, vlan->flags)) 839df8ef8f3SJohn Fastabend goto nla_put_failure; 84027c0b1a8SArnd Bergmann return 0; 84127c0b1a8SArnd Bergmann 84227c0b1a8SArnd Bergmann nla_put_failure: 84327c0b1a8SArnd Bergmann return -EMSGSIZE; 84427c0b1a8SArnd Bergmann } 84527c0b1a8SArnd Bergmann 84627c0b1a8SArnd Bergmann static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = { 84727c0b1a8SArnd Bergmann [IFLA_MACVLAN_MODE] = { .type = NLA_U32 }, 848df8ef8f3SJohn Fastabend [IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 }, 84927c0b1a8SArnd Bergmann }; 85027c0b1a8SArnd Bergmann 851fc0663d6SArnd Bergmann int macvlan_link_register(struct rtnl_link_ops *ops) 852fc0663d6SArnd Bergmann { 853fc0663d6SArnd Bergmann /* common fields */ 854fc0663d6SArnd Bergmann ops->priv_size = sizeof(struct macvlan_dev); 855fc0663d6SArnd Bergmann ops->validate = macvlan_validate; 856fc0663d6SArnd Bergmann ops->maxtype = IFLA_MACVLAN_MAX; 857fc0663d6SArnd Bergmann ops->policy = macvlan_policy; 858fc0663d6SArnd Bergmann ops->changelink = macvlan_changelink; 859fc0663d6SArnd Bergmann ops->get_size = macvlan_get_size; 860fc0663d6SArnd Bergmann ops->fill_info = macvlan_fill_info; 861fc0663d6SArnd Bergmann 862fc0663d6SArnd Bergmann return rtnl_link_register(ops); 863fc0663d6SArnd Bergmann }; 864fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_link_register); 865fc0663d6SArnd Bergmann 866fc0663d6SArnd Bergmann static struct rtnl_link_ops macvlan_link_ops = { 867b863ceb7SPatrick McHardy .kind = "macvlan", 8688a35747aSHerbert Xu .setup = macvlan_setup, 869b863ceb7SPatrick McHardy .newlink = macvlan_newlink, 870b863ceb7SPatrick McHardy .dellink = macvlan_dellink, 871b863ceb7SPatrick McHardy }; 872b863ceb7SPatrick McHardy 873b863ceb7SPatrick McHardy static int macvlan_device_event(struct notifier_block *unused, 874b863ceb7SPatrick McHardy unsigned long event, void *ptr) 875b863ceb7SPatrick McHardy { 876b863ceb7SPatrick McHardy struct net_device *dev = ptr; 877b863ceb7SPatrick McHardy struct macvlan_dev *vlan, *next; 878b863ceb7SPatrick McHardy struct macvlan_port *port; 879226bd341SEric Dumazet LIST_HEAD(list_kill); 880b863ceb7SPatrick McHardy 881a35e2c1bSJiri Pirko if (!macvlan_port_exists(dev)) 882b863ceb7SPatrick McHardy return NOTIFY_DONE; 883b863ceb7SPatrick McHardy 884a35e2c1bSJiri Pirko port = macvlan_port_get(dev); 885a35e2c1bSJiri Pirko 886b863ceb7SPatrick McHardy switch (event) { 887b863ceb7SPatrick McHardy case NETDEV_CHANGE: 888b863ceb7SPatrick McHardy list_for_each_entry(vlan, &port->vlans, list) 889fc4a7489SPatrick Mullaney netif_stacked_transfer_operstate(vlan->lowerdev, 890fc4a7489SPatrick Mullaney vlan->dev); 891b863ceb7SPatrick McHardy break; 892b863ceb7SPatrick McHardy case NETDEV_FEAT_CHANGE: 893b863ceb7SPatrick McHardy list_for_each_entry(vlan, &port->vlans, list) { 894b863ceb7SPatrick McHardy vlan->dev->features = dev->features & MACVLAN_FEATURES; 8958c2acc53SPatrick McHardy vlan->dev->gso_max_size = dev->gso_max_size; 896b863ceb7SPatrick McHardy netdev_features_change(vlan->dev); 897b863ceb7SPatrick McHardy } 898b863ceb7SPatrick McHardy break; 899b863ceb7SPatrick McHardy case NETDEV_UNREGISTER: 9003b27e105SDavid Lamparter /* twiddle thumbs on netns device moves */ 9013b27e105SDavid Lamparter if (dev->reg_state != NETREG_UNREGISTERING) 9023b27e105SDavid Lamparter break; 9033b27e105SDavid Lamparter 904b863ceb7SPatrick McHardy list_for_each_entry_safe(vlan, next, &port->vlans, list) 905226bd341SEric Dumazet vlan->dev->rtnl_link_ops->dellink(vlan->dev, &list_kill); 906226bd341SEric Dumazet unregister_netdevice_many(&list_kill); 907226bd341SEric Dumazet list_del(&list_kill); 908b863ceb7SPatrick McHardy break; 9091c01fe14SJiri Pirko case NETDEV_PRE_TYPE_CHANGE: 9101c01fe14SJiri Pirko /* Forbid underlaying device to change its type. */ 9111c01fe14SJiri Pirko return NOTIFY_BAD; 912b863ceb7SPatrick McHardy } 913b863ceb7SPatrick McHardy return NOTIFY_DONE; 914b863ceb7SPatrick McHardy } 915b863ceb7SPatrick McHardy 916b863ceb7SPatrick McHardy static struct notifier_block macvlan_notifier_block __read_mostly = { 917b863ceb7SPatrick McHardy .notifier_call = macvlan_device_event, 918b863ceb7SPatrick McHardy }; 919b863ceb7SPatrick McHardy 920b863ceb7SPatrick McHardy static int __init macvlan_init_module(void) 921b863ceb7SPatrick McHardy { 922b863ceb7SPatrick McHardy int err; 923b863ceb7SPatrick McHardy 924b863ceb7SPatrick McHardy register_netdevice_notifier(&macvlan_notifier_block); 925b863ceb7SPatrick McHardy 926fc0663d6SArnd Bergmann err = macvlan_link_register(&macvlan_link_ops); 927b863ceb7SPatrick McHardy if (err < 0) 928b863ceb7SPatrick McHardy goto err1; 929b863ceb7SPatrick McHardy return 0; 930b863ceb7SPatrick McHardy err1: 931b863ceb7SPatrick McHardy unregister_netdevice_notifier(&macvlan_notifier_block); 932b863ceb7SPatrick McHardy return err; 933b863ceb7SPatrick McHardy } 934b863ceb7SPatrick McHardy 935b863ceb7SPatrick McHardy static void __exit macvlan_cleanup_module(void) 936b863ceb7SPatrick McHardy { 937b863ceb7SPatrick McHardy rtnl_link_unregister(&macvlan_link_ops); 938b863ceb7SPatrick McHardy unregister_netdevice_notifier(&macvlan_notifier_block); 939b863ceb7SPatrick McHardy } 940b863ceb7SPatrick McHardy 941b863ceb7SPatrick McHardy module_init(macvlan_init_module); 942b863ceb7SPatrick McHardy module_exit(macvlan_cleanup_module); 943b863ceb7SPatrick McHardy 944b863ceb7SPatrick McHardy MODULE_LICENSE("GPL"); 945b863ceb7SPatrick McHardy MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); 946b863ceb7SPatrick McHardy MODULE_DESCRIPTION("Driver for MAC address based VLANs"); 947b863ceb7SPatrick McHardy MODULE_ALIAS_RTNL_LINK("macvlan"); 948