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> 32cd431e73SEric Dumazet #include <linux/hash.h> 33b863ceb7SPatrick McHardy #include <net/rtnetlink.h> 34618e1b74SArnd Bergmann #include <net/xfrm.h> 35b863ceb7SPatrick McHardy 36b863ceb7SPatrick McHardy #define MACVLAN_HASH_SIZE (1 << BITS_PER_BYTE) 37b863ceb7SPatrick McHardy 38b863ceb7SPatrick McHardy struct macvlan_port { 39b863ceb7SPatrick McHardy struct net_device *dev; 40b863ceb7SPatrick McHardy struct hlist_head vlan_hash[MACVLAN_HASH_SIZE]; 41b863ceb7SPatrick McHardy struct list_head vlans; 428b37ef0aSJiri Pirko struct rcu_head rcu; 43eb06acdcSSridhar Samudrala bool passthru; 44d5cd9244SEric W. Biederman int count; 45b863ceb7SPatrick McHardy }; 46b863ceb7SPatrick McHardy 47d5cd9244SEric W. Biederman static void macvlan_port_destroy(struct net_device *dev); 48d5cd9244SEric W. Biederman 49e052f7e6SEric Dumazet static struct macvlan_port *macvlan_port_get_rcu(const struct net_device *dev) 50e052f7e6SEric Dumazet { 51e052f7e6SEric Dumazet return rcu_dereference(dev->rx_handler_data); 52e052f7e6SEric Dumazet } 53e052f7e6SEric Dumazet 54e052f7e6SEric Dumazet static struct macvlan_port *macvlan_port_get_rtnl(const struct net_device *dev) 55e052f7e6SEric Dumazet { 56e052f7e6SEric Dumazet return rtnl_dereference(dev->rx_handler_data); 57e052f7e6SEric Dumazet } 58e052f7e6SEric Dumazet 59a35e2c1bSJiri Pirko #define macvlan_port_exists(dev) (dev->priv_flags & IFF_MACVLAN_PORT) 60a35e2c1bSJiri Pirko 61b863ceb7SPatrick McHardy static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port, 62b863ceb7SPatrick McHardy const unsigned char *addr) 63b863ceb7SPatrick McHardy { 64b863ceb7SPatrick McHardy struct macvlan_dev *vlan; 65b863ceb7SPatrick McHardy 66b67bfe0dSSasha Levin hlist_for_each_entry_rcu(vlan, &port->vlan_hash[addr[5]], hlist) { 67a6700db1SJoe Perches if (ether_addr_equal_64bits(vlan->dev->dev_addr, addr)) 68b863ceb7SPatrick McHardy return vlan; 69b863ceb7SPatrick McHardy } 70b863ceb7SPatrick McHardy return NULL; 71b863ceb7SPatrick McHardy } 72b863ceb7SPatrick McHardy 73f9ac30f0SEric Biederman static void macvlan_hash_add(struct macvlan_dev *vlan) 74f9ac30f0SEric Biederman { 75f9ac30f0SEric Biederman struct macvlan_port *port = vlan->port; 76f9ac30f0SEric Biederman const unsigned char *addr = vlan->dev->dev_addr; 77f9ac30f0SEric Biederman 78f9ac30f0SEric Biederman hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]); 79f9ac30f0SEric Biederman } 80f9ac30f0SEric Biederman 81449f4544SEric Dumazet static void macvlan_hash_del(struct macvlan_dev *vlan, bool sync) 82f9ac30f0SEric Biederman { 83f9ac30f0SEric Biederman hlist_del_rcu(&vlan->hlist); 84449f4544SEric Dumazet if (sync) 85f9ac30f0SEric Biederman synchronize_rcu(); 86f9ac30f0SEric Biederman } 87f9ac30f0SEric Biederman 88f9ac30f0SEric Biederman static void macvlan_hash_change_addr(struct macvlan_dev *vlan, 89f9ac30f0SEric Biederman const unsigned char *addr) 90f9ac30f0SEric Biederman { 91449f4544SEric Dumazet macvlan_hash_del(vlan, true); 92f9ac30f0SEric Biederman /* Now that we are unhashed it is safe to change the device 93f9ac30f0SEric Biederman * address without confusing packet delivery. 94f9ac30f0SEric Biederman */ 95f9ac30f0SEric Biederman memcpy(vlan->dev->dev_addr, addr, ETH_ALEN); 96f9ac30f0SEric Biederman macvlan_hash_add(vlan); 97f9ac30f0SEric Biederman } 98f9ac30f0SEric Biederman 99f9ac30f0SEric Biederman static int macvlan_addr_busy(const struct macvlan_port *port, 100f9ac30f0SEric Biederman const unsigned char *addr) 101f9ac30f0SEric Biederman { 102f9ac30f0SEric Biederman /* Test to see if the specified multicast address is 103f9ac30f0SEric Biederman * currently in use by the underlying device or 104f9ac30f0SEric Biederman * another macvlan. 105f9ac30f0SEric Biederman */ 106a6700db1SJoe Perches if (ether_addr_equal_64bits(port->dev->dev_addr, addr)) 107f9ac30f0SEric Biederman return 1; 108f9ac30f0SEric Biederman 109f9ac30f0SEric Biederman if (macvlan_hash_lookup(port, addr)) 110f9ac30f0SEric Biederman return 1; 111f9ac30f0SEric Biederman 112f9ac30f0SEric Biederman return 0; 113f9ac30f0SEric Biederman } 114f9ac30f0SEric Biederman 115a1e514c5SArnd Bergmann 116fc0663d6SArnd Bergmann static int macvlan_broadcast_one(struct sk_buff *skb, 117fc0663d6SArnd Bergmann const struct macvlan_dev *vlan, 118618e1b74SArnd Bergmann const struct ethhdr *eth, bool local) 119a1e514c5SArnd Bergmann { 120fc0663d6SArnd Bergmann struct net_device *dev = vlan->dev; 121a1e514c5SArnd Bergmann if (!skb) 122a1e514c5SArnd Bergmann return NET_RX_DROP; 123a1e514c5SArnd Bergmann 124618e1b74SArnd Bergmann if (local) 125fc0663d6SArnd Bergmann return vlan->forward(dev, skb); 126618e1b74SArnd Bergmann 127a1e514c5SArnd Bergmann skb->dev = dev; 128a6700db1SJoe Perches if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast)) 129a1e514c5SArnd Bergmann skb->pkt_type = PACKET_BROADCAST; 130a1e514c5SArnd Bergmann else 131a1e514c5SArnd Bergmann skb->pkt_type = PACKET_MULTICAST; 132a1e514c5SArnd Bergmann 133fc0663d6SArnd Bergmann return vlan->receive(skb); 134a1e514c5SArnd Bergmann } 135a1e514c5SArnd Bergmann 1363807ff58SEric Dumazet static u32 macvlan_hash_mix(const struct macvlan_dev *vlan) 1373807ff58SEric Dumazet { 1383807ff58SEric Dumazet return (u32)(((unsigned long)vlan) >> L1_CACHE_SHIFT); 1393807ff58SEric Dumazet } 1403807ff58SEric Dumazet 1413807ff58SEric Dumazet 1423807ff58SEric Dumazet static unsigned int mc_hash(const struct macvlan_dev *vlan, 1433807ff58SEric Dumazet const unsigned char *addr) 144cd431e73SEric Dumazet { 145cd431e73SEric Dumazet u32 val = __get_unaligned_cpu32(addr + 2); 146cd431e73SEric Dumazet 1473807ff58SEric Dumazet val ^= macvlan_hash_mix(vlan); 148cd431e73SEric Dumazet return hash_32(val, MACVLAN_MC_FILTER_BITS); 149cd431e73SEric Dumazet } 150cd431e73SEric Dumazet 151b863ceb7SPatrick McHardy static void macvlan_broadcast(struct sk_buff *skb, 152618e1b74SArnd Bergmann const struct macvlan_port *port, 153618e1b74SArnd Bergmann struct net_device *src, 154618e1b74SArnd Bergmann enum macvlan_mode mode) 155b863ceb7SPatrick McHardy { 156b863ceb7SPatrick McHardy const struct ethhdr *eth = eth_hdr(skb); 157b863ceb7SPatrick McHardy const struct macvlan_dev *vlan; 158b863ceb7SPatrick McHardy struct sk_buff *nskb; 159b863ceb7SPatrick McHardy unsigned int i; 160a1e514c5SArnd Bergmann int err; 1613807ff58SEric Dumazet unsigned int hash; 162b863ceb7SPatrick McHardy 163efbbced3SPatrick McHardy if (skb->protocol == htons(ETH_P_PAUSE)) 164efbbced3SPatrick McHardy return; 165efbbced3SPatrick McHardy 166b863ceb7SPatrick McHardy for (i = 0; i < MACVLAN_HASH_SIZE; i++) { 167b67bfe0dSSasha Levin hlist_for_each_entry_rcu(vlan, &port->vlan_hash[i], hlist) { 168618e1b74SArnd Bergmann if (vlan->dev == src || !(vlan->mode & mode)) 169618e1b74SArnd Bergmann continue; 170618e1b74SArnd Bergmann 1713807ff58SEric Dumazet hash = mc_hash(vlan, eth->h_dest); 172cd431e73SEric Dumazet if (!test_bit(hash, vlan->mc_filter)) 173cd431e73SEric Dumazet continue; 174b863ceb7SPatrick McHardy nskb = skb_clone(skb, GFP_ATOMIC); 175fc0663d6SArnd Bergmann err = macvlan_broadcast_one(nskb, vlan, eth, 176618e1b74SArnd Bergmann mode == MACVLAN_MODE_BRIDGE); 177a1e514c5SArnd Bergmann macvlan_count_rx(vlan, skb->len + ETH_HLEN, 178a1e514c5SArnd Bergmann err == NET_RX_SUCCESS, 1); 179b863ceb7SPatrick McHardy } 180b863ceb7SPatrick McHardy } 181b863ceb7SPatrick McHardy } 182b863ceb7SPatrick McHardy 183b863ceb7SPatrick McHardy /* called under rcu_read_lock() from netif_receive_skb */ 1848a4eb573SJiri Pirko static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb) 185b863ceb7SPatrick McHardy { 186ab95bfe0SJiri Pirko struct macvlan_port *port; 1878a4eb573SJiri Pirko struct sk_buff *skb = *pskb; 188b863ceb7SPatrick McHardy const struct ethhdr *eth = eth_hdr(skb); 189b863ceb7SPatrick McHardy const struct macvlan_dev *vlan; 190618e1b74SArnd Bergmann const struct macvlan_dev *src; 191b863ceb7SPatrick McHardy struct net_device *dev; 192ba01877fSSridhar Samudrala unsigned int len = 0; 193ba01877fSSridhar Samudrala int ret = NET_RX_DROP; 194b863ceb7SPatrick McHardy 195a35e2c1bSJiri Pirko port = macvlan_port_get_rcu(skb->dev); 196b863ceb7SPatrick McHardy if (is_multicast_ether_addr(eth->h_dest)) { 197bc416d97SEric Dumazet skb = ip_check_defrag(skb, IP_DEFRAG_MACVLAN); 198bc416d97SEric Dumazet if (!skb) 199bc416d97SEric Dumazet return RX_HANDLER_CONSUMED; 2004ec7ac12SEric Dumazet eth = eth_hdr(skb); 201618e1b74SArnd Bergmann src = macvlan_hash_lookup(port, eth->h_source); 202618e1b74SArnd Bergmann if (!src) 203618e1b74SArnd Bergmann /* frame comes from an external address */ 204618e1b74SArnd Bergmann macvlan_broadcast(skb, port, NULL, 205618e1b74SArnd Bergmann MACVLAN_MODE_PRIVATE | 206618e1b74SArnd Bergmann MACVLAN_MODE_VEPA | 207eb06acdcSSridhar Samudrala MACVLAN_MODE_PASSTHRU| 208618e1b74SArnd Bergmann MACVLAN_MODE_BRIDGE); 209618e1b74SArnd Bergmann else if (src->mode == MACVLAN_MODE_VEPA) 210618e1b74SArnd Bergmann /* flood to everyone except source */ 211618e1b74SArnd Bergmann macvlan_broadcast(skb, port, src->dev, 212618e1b74SArnd Bergmann MACVLAN_MODE_VEPA | 213618e1b74SArnd Bergmann MACVLAN_MODE_BRIDGE); 214618e1b74SArnd Bergmann else if (src->mode == MACVLAN_MODE_BRIDGE) 215618e1b74SArnd Bergmann /* 216618e1b74SArnd Bergmann * flood only to VEPA ports, bridge ports 217618e1b74SArnd Bergmann * already saw the frame on the way out. 218618e1b74SArnd Bergmann */ 219618e1b74SArnd Bergmann macvlan_broadcast(skb, port, src->dev, 220618e1b74SArnd Bergmann MACVLAN_MODE_VEPA); 221729e72a1Sstephen hemminger else { 222729e72a1Sstephen hemminger /* forward to original port. */ 223729e72a1Sstephen hemminger vlan = src; 224729e72a1Sstephen hemminger ret = macvlan_broadcast_one(skb, vlan, eth, 0); 225729e72a1Sstephen hemminger goto out; 226729e72a1Sstephen hemminger } 227729e72a1Sstephen hemminger 2288a4eb573SJiri Pirko return RX_HANDLER_PASS; 229b863ceb7SPatrick McHardy } 230b863ceb7SPatrick McHardy 231eb06acdcSSridhar Samudrala if (port->passthru) 232eb06acdcSSridhar Samudrala vlan = list_first_entry(&port->vlans, struct macvlan_dev, list); 233eb06acdcSSridhar Samudrala else 234b863ceb7SPatrick McHardy vlan = macvlan_hash_lookup(port, eth->h_dest); 235b863ceb7SPatrick McHardy if (vlan == NULL) 2368a4eb573SJiri Pirko return RX_HANDLER_PASS; 237b863ceb7SPatrick McHardy 238b863ceb7SPatrick McHardy dev = vlan->dev; 239b863ceb7SPatrick McHardy if (unlikely(!(dev->flags & IFF_UP))) { 240b863ceb7SPatrick McHardy kfree_skb(skb); 2418a4eb573SJiri Pirko return RX_HANDLER_CONSUMED; 242b863ceb7SPatrick McHardy } 243a1e514c5SArnd Bergmann len = skb->len + ETH_HLEN; 244b863ceb7SPatrick McHardy skb = skb_share_check(skb, GFP_ATOMIC); 245a1e514c5SArnd Bergmann if (!skb) 246ba01877fSSridhar Samudrala goto out; 247b863ceb7SPatrick McHardy 248b863ceb7SPatrick McHardy skb->dev = dev; 249b863ceb7SPatrick McHardy skb->pkt_type = PACKET_HOST; 250b863ceb7SPatrick McHardy 251ba01877fSSridhar Samudrala ret = vlan->receive(skb); 252ba01877fSSridhar Samudrala 253ba01877fSSridhar Samudrala out: 254ba01877fSSridhar Samudrala macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, 0); 2558a4eb573SJiri Pirko return RX_HANDLER_CONSUMED; 256b863ceb7SPatrick McHardy } 257b863ceb7SPatrick McHardy 258618e1b74SArnd Bergmann static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev) 259618e1b74SArnd Bergmann { 260618e1b74SArnd Bergmann const struct macvlan_dev *vlan = netdev_priv(dev); 261618e1b74SArnd Bergmann const struct macvlan_port *port = vlan->port; 262618e1b74SArnd Bergmann const struct macvlan_dev *dest; 26312a2856bSDaniel Lezcano __u8 ip_summed = skb->ip_summed; 264618e1b74SArnd Bergmann 265618e1b74SArnd Bergmann if (vlan->mode == MACVLAN_MODE_BRIDGE) { 266618e1b74SArnd Bergmann const struct ethhdr *eth = (void *)skb->data; 26712a2856bSDaniel Lezcano skb->ip_summed = CHECKSUM_UNNECESSARY; 268618e1b74SArnd Bergmann 269618e1b74SArnd Bergmann /* send to other bridge ports directly */ 270618e1b74SArnd Bergmann if (is_multicast_ether_addr(eth->h_dest)) { 271618e1b74SArnd Bergmann macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE); 272618e1b74SArnd Bergmann goto xmit_world; 273618e1b74SArnd Bergmann } 274618e1b74SArnd Bergmann 275618e1b74SArnd Bergmann dest = macvlan_hash_lookup(port, eth->h_dest); 276618e1b74SArnd Bergmann if (dest && dest->mode == MACVLAN_MODE_BRIDGE) { 277a37dd333SDavid Ward /* send to lowerdev first for its network taps */ 278cb2d0f3eSDavid Ward dev_forward_skb(vlan->lowerdev, skb); 279618e1b74SArnd Bergmann 280618e1b74SArnd Bergmann return NET_XMIT_SUCCESS; 281618e1b74SArnd Bergmann } 282618e1b74SArnd Bergmann } 283618e1b74SArnd Bergmann 284618e1b74SArnd Bergmann xmit_world: 28512a2856bSDaniel Lezcano skb->ip_summed = ip_summed; 28659b9997bSDavid S. Miller skb->dev = vlan->lowerdev; 287618e1b74SArnd Bergmann return dev_queue_xmit(skb); 288618e1b74SArnd Bergmann } 289618e1b74SArnd Bergmann 290fc0663d6SArnd Bergmann netdev_tx_t macvlan_start_xmit(struct sk_buff *skb, 291424efe9cSStephen Hemminger struct net_device *dev) 292b863ceb7SPatrick McHardy { 293b863ceb7SPatrick McHardy unsigned int len = skb->len; 294b863ceb7SPatrick McHardy int ret; 2958ffab51bSEric Dumazet const struct macvlan_dev *vlan = netdev_priv(dev); 296b863ceb7SPatrick McHardy 297618e1b74SArnd Bergmann ret = macvlan_queue_xmit(skb, dev); 2982d6c9ffcSEric Dumazet if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) { 2998ffab51bSEric Dumazet struct macvlan_pcpu_stats *pcpu_stats; 3002c114553SEric Dumazet 3018ffab51bSEric Dumazet pcpu_stats = this_cpu_ptr(vlan->pcpu_stats); 3028ffab51bSEric Dumazet u64_stats_update_begin(&pcpu_stats->syncp); 3038ffab51bSEric Dumazet pcpu_stats->tx_packets++; 3048ffab51bSEric Dumazet pcpu_stats->tx_bytes += len; 3058ffab51bSEric Dumazet u64_stats_update_end(&pcpu_stats->syncp); 3068ffab51bSEric Dumazet } else { 3078ffab51bSEric Dumazet this_cpu_inc(vlan->pcpu_stats->tx_dropped); 3088ffab51bSEric Dumazet } 309cbbef5e1SPatrick McHardy return ret; 310b863ceb7SPatrick McHardy } 311fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_start_xmit); 312b863ceb7SPatrick McHardy 313b863ceb7SPatrick McHardy static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev, 3143b04dddeSStephen Hemminger unsigned short type, const void *daddr, 3153b04dddeSStephen Hemminger const void *saddr, unsigned len) 316b863ceb7SPatrick McHardy { 317b863ceb7SPatrick McHardy const struct macvlan_dev *vlan = netdev_priv(dev); 318b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 319b863ceb7SPatrick McHardy 3200c4e8581SStephen Hemminger return dev_hard_header(skb, lowerdev, type, daddr, 321b863ceb7SPatrick McHardy saddr ? : dev->dev_addr, len); 322b863ceb7SPatrick McHardy } 323b863ceb7SPatrick McHardy 3243b04dddeSStephen Hemminger static const struct header_ops macvlan_hard_header_ops = { 3253b04dddeSStephen Hemminger .create = macvlan_hard_header, 3263b04dddeSStephen Hemminger .rebuild = eth_rebuild_header, 3273b04dddeSStephen Hemminger .parse = eth_header_parse, 3283b04dddeSStephen Hemminger .cache = eth_header_cache, 3293b04dddeSStephen Hemminger .cache_update = eth_header_cache_update, 3303b04dddeSStephen Hemminger }; 3313b04dddeSStephen Hemminger 332b863ceb7SPatrick McHardy static int macvlan_open(struct net_device *dev) 333b863ceb7SPatrick McHardy { 334b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 335b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 336b863ceb7SPatrick McHardy int err; 337b863ceb7SPatrick McHardy 338eb06acdcSSridhar Samudrala if (vlan->port->passthru) { 339df8ef8f3SJohn Fastabend if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) 340eb06acdcSSridhar Samudrala dev_set_promiscuity(lowerdev, 1); 341eb06acdcSSridhar Samudrala goto hash_add; 342eb06acdcSSridhar Samudrala } 343eb06acdcSSridhar Samudrala 344f9ac30f0SEric Biederman err = -EBUSY; 345f9ac30f0SEric Biederman if (macvlan_addr_busy(vlan->port, dev->dev_addr)) 346f9ac30f0SEric Biederman goto out; 347f9ac30f0SEric Biederman 348a748ee24SJiri Pirko err = dev_uc_add(lowerdev, dev->dev_addr); 349b863ceb7SPatrick McHardy if (err < 0) 350b89fb7daSWang Chen goto out; 351b89fb7daSWang Chen if (dev->flags & IFF_ALLMULTI) { 352b89fb7daSWang Chen err = dev_set_allmulti(lowerdev, 1); 353b89fb7daSWang Chen if (err < 0) 354b89fb7daSWang Chen goto del_unicast; 355b89fb7daSWang Chen } 356eb06acdcSSridhar Samudrala 357eb06acdcSSridhar Samudrala hash_add: 358f9ac30f0SEric Biederman macvlan_hash_add(vlan); 359b863ceb7SPatrick McHardy return 0; 360b89fb7daSWang Chen 361b89fb7daSWang Chen del_unicast: 362a748ee24SJiri Pirko dev_uc_del(lowerdev, dev->dev_addr); 363b89fb7daSWang Chen out: 364b89fb7daSWang Chen return err; 365b863ceb7SPatrick McHardy } 366b863ceb7SPatrick McHardy 367b863ceb7SPatrick McHardy static int macvlan_stop(struct net_device *dev) 368b863ceb7SPatrick McHardy { 369b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 370b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 371b863ceb7SPatrick McHardy 372df8ef8f3SJohn Fastabend dev_uc_unsync(lowerdev, dev); 373df8ef8f3SJohn Fastabend dev_mc_unsync(lowerdev, dev); 374df8ef8f3SJohn Fastabend 375eb06acdcSSridhar Samudrala if (vlan->port->passthru) { 376df8ef8f3SJohn Fastabend if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) 377eb06acdcSSridhar Samudrala dev_set_promiscuity(lowerdev, -1); 378eb06acdcSSridhar Samudrala goto hash_del; 379eb06acdcSSridhar Samudrala } 380eb06acdcSSridhar Samudrala 381b863ceb7SPatrick McHardy if (dev->flags & IFF_ALLMULTI) 382b863ceb7SPatrick McHardy dev_set_allmulti(lowerdev, -1); 383b863ceb7SPatrick McHardy 384a748ee24SJiri Pirko dev_uc_del(lowerdev, dev->dev_addr); 385b863ceb7SPatrick McHardy 386eb06acdcSSridhar Samudrala hash_del: 387449f4544SEric Dumazet macvlan_hash_del(vlan, !dev->dismantle); 388b863ceb7SPatrick McHardy return 0; 389b863ceb7SPatrick McHardy } 390b863ceb7SPatrick McHardy 391ad5d20a6SPatrick McHardy static int macvlan_set_mac_address(struct net_device *dev, void *p) 392ad5d20a6SPatrick McHardy { 393ad5d20a6SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 394ad5d20a6SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 395ad5d20a6SPatrick McHardy struct sockaddr *addr = p; 396ad5d20a6SPatrick McHardy int err; 397ad5d20a6SPatrick McHardy 398ad5d20a6SPatrick McHardy if (!is_valid_ether_addr(addr->sa_data)) 399ad5d20a6SPatrick McHardy return -EADDRNOTAVAIL; 400ad5d20a6SPatrick McHardy 401f9ac30f0SEric Biederman if (!(dev->flags & IFF_UP)) { 402f9ac30f0SEric Biederman /* Just copy in the new address */ 403f9ac30f0SEric Biederman memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN); 404f9ac30f0SEric Biederman } else { 405f9ac30f0SEric Biederman /* Rehash and update the device filters */ 406f9ac30f0SEric Biederman if (macvlan_addr_busy(vlan->port, addr->sa_data)) 407f9ac30f0SEric Biederman return -EBUSY; 408ad5d20a6SPatrick McHardy 409a748ee24SJiri Pirko err = dev_uc_add(lowerdev, addr->sa_data); 410ccffad25SJiri Pirko if (err) 411ad5d20a6SPatrick McHardy return err; 412f9ac30f0SEric Biederman 413a748ee24SJiri Pirko dev_uc_del(lowerdev, dev->dev_addr); 414ad5d20a6SPatrick McHardy 415f9ac30f0SEric Biederman macvlan_hash_change_addr(vlan, addr->sa_data); 416f9ac30f0SEric Biederman } 417ad5d20a6SPatrick McHardy return 0; 418ad5d20a6SPatrick McHardy } 419ad5d20a6SPatrick McHardy 420b863ceb7SPatrick McHardy static void macvlan_change_rx_flags(struct net_device *dev, int change) 421b863ceb7SPatrick McHardy { 422b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 423b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 424b863ceb7SPatrick McHardy 425b863ceb7SPatrick McHardy if (change & IFF_ALLMULTI) 426b863ceb7SPatrick McHardy dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1); 427b863ceb7SPatrick McHardy } 428b863ceb7SPatrick McHardy 429df8ef8f3SJohn Fastabend static void macvlan_set_mac_lists(struct net_device *dev) 430b863ceb7SPatrick McHardy { 431b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 432b863ceb7SPatrick McHardy 433cd431e73SEric Dumazet if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) { 434cd431e73SEric Dumazet bitmap_fill(vlan->mc_filter, MACVLAN_MC_FILTER_SZ); 435cd431e73SEric Dumazet } else { 436cd431e73SEric Dumazet struct netdev_hw_addr *ha; 437cd431e73SEric Dumazet DECLARE_BITMAP(filter, MACVLAN_MC_FILTER_SZ); 438cd431e73SEric Dumazet 439cd431e73SEric Dumazet bitmap_zero(filter, MACVLAN_MC_FILTER_SZ); 440cd431e73SEric Dumazet netdev_for_each_mc_addr(ha, dev) { 4413807ff58SEric Dumazet __set_bit(mc_hash(vlan, ha->addr), filter); 442cd431e73SEric Dumazet } 443d5270430SEric Dumazet 4443807ff58SEric Dumazet __set_bit(mc_hash(vlan, dev->broadcast), filter); 445d5270430SEric Dumazet 446cd431e73SEric Dumazet bitmap_copy(vlan->mc_filter, filter, MACVLAN_MC_FILTER_SZ); 447cd431e73SEric Dumazet } 448df8ef8f3SJohn Fastabend dev_uc_sync(vlan->lowerdev, dev); 449b863ceb7SPatrick McHardy dev_mc_sync(vlan->lowerdev, dev); 450b863ceb7SPatrick McHardy } 451b863ceb7SPatrick McHardy 452b863ceb7SPatrick McHardy static int macvlan_change_mtu(struct net_device *dev, int new_mtu) 453b863ceb7SPatrick McHardy { 454b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 455b863ceb7SPatrick McHardy 456b863ceb7SPatrick McHardy if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu) 457b863ceb7SPatrick McHardy return -EINVAL; 458b863ceb7SPatrick McHardy dev->mtu = new_mtu; 459b863ceb7SPatrick McHardy return 0; 460b863ceb7SPatrick McHardy } 461b863ceb7SPatrick McHardy 462b863ceb7SPatrick McHardy /* 463b863ceb7SPatrick McHardy * macvlan network devices have devices nesting below it and are a special 464b863ceb7SPatrick McHardy * "super class" of normal network devices; split their locks off into a 465b863ceb7SPatrick McHardy * separate class since they always nest. 466b863ceb7SPatrick McHardy */ 467b863ceb7SPatrick McHardy static struct lock_class_key macvlan_netdev_xmit_lock_key; 468cf508b12SDavid S. Miller static struct lock_class_key macvlan_netdev_addr_lock_key; 469b863ceb7SPatrick McHardy 470b863ceb7SPatrick McHardy #define MACVLAN_FEATURES \ 471b863ceb7SPatrick McHardy (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \ 472b863ceb7SPatrick McHardy NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \ 4738d13e670SJohn Fastabend NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \ 474f646968fSPatrick McHardy NETIF_F_HW_VLAN_CTAG_FILTER) 475b863ceb7SPatrick McHardy 476b863ceb7SPatrick McHardy #define MACVLAN_STATE_MASK \ 477b863ceb7SPatrick McHardy ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT)) 478b863ceb7SPatrick McHardy 479e8a0464cSDavid S. Miller static void macvlan_set_lockdep_class_one(struct net_device *dev, 480e8a0464cSDavid S. Miller struct netdev_queue *txq, 481e8a0464cSDavid S. Miller void *_unused) 482c773e847SDavid S. Miller { 483c773e847SDavid S. Miller lockdep_set_class(&txq->_xmit_lock, 484c773e847SDavid S. Miller &macvlan_netdev_xmit_lock_key); 485c773e847SDavid S. Miller } 486c773e847SDavid S. Miller 487c773e847SDavid S. Miller static void macvlan_set_lockdep_class(struct net_device *dev) 488c773e847SDavid S. Miller { 489cf508b12SDavid S. Miller lockdep_set_class(&dev->addr_list_lock, 490cf508b12SDavid S. Miller &macvlan_netdev_addr_lock_key); 491e8a0464cSDavid S. Miller netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL); 492c773e847SDavid S. Miller } 493c773e847SDavid S. Miller 494b863ceb7SPatrick McHardy static int macvlan_init(struct net_device *dev) 495b863ceb7SPatrick McHardy { 496b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 497b863ceb7SPatrick McHardy const struct net_device *lowerdev = vlan->lowerdev; 498b863ceb7SPatrick McHardy 499b863ceb7SPatrick McHardy dev->state = (dev->state & ~MACVLAN_STATE_MASK) | 500b863ceb7SPatrick McHardy (lowerdev->state & MACVLAN_STATE_MASK); 501b863ceb7SPatrick McHardy dev->features = lowerdev->features & MACVLAN_FEATURES; 5028ffab51bSEric Dumazet dev->features |= NETIF_F_LLTX; 5038c2acc53SPatrick McHardy dev->gso_max_size = lowerdev->gso_max_size; 504b863ceb7SPatrick McHardy dev->iflink = lowerdev->ifindex; 505ef5c8996Ssg.tweak@gmail.com dev->hard_header_len = lowerdev->hard_header_len; 506b863ceb7SPatrick McHardy 507c773e847SDavid S. Miller macvlan_set_lockdep_class(dev); 508c773e847SDavid S. Miller 5098ffab51bSEric Dumazet vlan->pcpu_stats = alloc_percpu(struct macvlan_pcpu_stats); 5108ffab51bSEric Dumazet if (!vlan->pcpu_stats) 511fccaf710SEric Dumazet return -ENOMEM; 512fccaf710SEric Dumazet 513b863ceb7SPatrick McHardy return 0; 514b863ceb7SPatrick McHardy } 515b863ceb7SPatrick McHardy 516fccaf710SEric Dumazet static void macvlan_uninit(struct net_device *dev) 517fccaf710SEric Dumazet { 518fccaf710SEric Dumazet struct macvlan_dev *vlan = netdev_priv(dev); 519d5cd9244SEric W. Biederman struct macvlan_port *port = vlan->port; 520fccaf710SEric Dumazet 5218ffab51bSEric Dumazet free_percpu(vlan->pcpu_stats); 522d5cd9244SEric W. Biederman 523d5cd9244SEric W. Biederman port->count -= 1; 524d5cd9244SEric W. Biederman if (!port->count) 525d5cd9244SEric W. Biederman macvlan_port_destroy(port->dev); 526fccaf710SEric Dumazet } 527fccaf710SEric Dumazet 52828172739SEric Dumazet static struct rtnl_link_stats64 *macvlan_dev_get_stats64(struct net_device *dev, 52928172739SEric Dumazet struct rtnl_link_stats64 *stats) 530fccaf710SEric Dumazet { 531fccaf710SEric Dumazet struct macvlan_dev *vlan = netdev_priv(dev); 532fccaf710SEric Dumazet 5338ffab51bSEric Dumazet if (vlan->pcpu_stats) { 5348ffab51bSEric Dumazet struct macvlan_pcpu_stats *p; 5358ffab51bSEric Dumazet u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes; 5368ffab51bSEric Dumazet u32 rx_errors = 0, tx_dropped = 0; 537bc66154eSEric Dumazet unsigned int start; 538fccaf710SEric Dumazet int i; 539fccaf710SEric Dumazet 540fccaf710SEric Dumazet for_each_possible_cpu(i) { 5418ffab51bSEric Dumazet p = per_cpu_ptr(vlan->pcpu_stats, i); 542bc66154eSEric Dumazet do { 543bc66154eSEric Dumazet start = u64_stats_fetch_begin_bh(&p->syncp); 544bc66154eSEric Dumazet rx_packets = p->rx_packets; 545bc66154eSEric Dumazet rx_bytes = p->rx_bytes; 546bc66154eSEric Dumazet rx_multicast = p->rx_multicast; 5478ffab51bSEric Dumazet tx_packets = p->tx_packets; 5488ffab51bSEric Dumazet tx_bytes = p->tx_bytes; 549bc66154eSEric Dumazet } while (u64_stats_fetch_retry_bh(&p->syncp, start)); 5508ffab51bSEric Dumazet 5518ffab51bSEric Dumazet stats->rx_packets += rx_packets; 5528ffab51bSEric Dumazet stats->rx_bytes += rx_bytes; 5538ffab51bSEric Dumazet stats->multicast += rx_multicast; 5548ffab51bSEric Dumazet stats->tx_packets += tx_packets; 5558ffab51bSEric Dumazet stats->tx_bytes += tx_bytes; 5568ffab51bSEric Dumazet /* rx_errors & tx_dropped are u32, updated 5578ffab51bSEric Dumazet * without syncp protection. 5588ffab51bSEric Dumazet */ 5598ffab51bSEric Dumazet rx_errors += p->rx_errors; 5608ffab51bSEric Dumazet tx_dropped += p->tx_dropped; 561fccaf710SEric Dumazet } 5628ffab51bSEric Dumazet stats->rx_errors = rx_errors; 5638ffab51bSEric Dumazet stats->rx_dropped = rx_errors; 5648ffab51bSEric Dumazet stats->tx_dropped = tx_dropped; 565fccaf710SEric Dumazet } 566fccaf710SEric Dumazet return stats; 567fccaf710SEric Dumazet } 568fccaf710SEric Dumazet 5698e586137SJiri Pirko static int macvlan_vlan_rx_add_vid(struct net_device *dev, 570*80d5c368SPatrick McHardy __be16 proto, u16 vid) 5718d13e670SJohn Fastabend { 5728d13e670SJohn Fastabend struct macvlan_dev *vlan = netdev_priv(dev); 5738d13e670SJohn Fastabend struct net_device *lowerdev = vlan->lowerdev; 5748d13e670SJohn Fastabend 575*80d5c368SPatrick McHardy return vlan_vid_add(lowerdev, proto, vid); 5768d13e670SJohn Fastabend } 5778d13e670SJohn Fastabend 5788e586137SJiri Pirko static int macvlan_vlan_rx_kill_vid(struct net_device *dev, 579*80d5c368SPatrick McHardy __be16 proto, u16 vid) 5808d13e670SJohn Fastabend { 5818d13e670SJohn Fastabend struct macvlan_dev *vlan = netdev_priv(dev); 5828d13e670SJohn Fastabend struct net_device *lowerdev = vlan->lowerdev; 5838d13e670SJohn Fastabend 584*80d5c368SPatrick McHardy vlan_vid_del(lowerdev, proto, vid); 5858e586137SJiri Pirko return 0; 5868d13e670SJohn Fastabend } 5878d13e670SJohn Fastabend 588edc7d573Sstephen hemminger static int macvlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], 589df8ef8f3SJohn Fastabend struct net_device *dev, 5906b6e2725Sstephen hemminger const unsigned char *addr, 591df8ef8f3SJohn Fastabend u16 flags) 592df8ef8f3SJohn Fastabend { 593df8ef8f3SJohn Fastabend struct macvlan_dev *vlan = netdev_priv(dev); 594df8ef8f3SJohn Fastabend int err = -EINVAL; 595df8ef8f3SJohn Fastabend 596df8ef8f3SJohn Fastabend if (!vlan->port->passthru) 597df8ef8f3SJohn Fastabend return -EOPNOTSUPP; 598df8ef8f3SJohn Fastabend 599df8ef8f3SJohn Fastabend if (is_unicast_ether_addr(addr)) 600df8ef8f3SJohn Fastabend err = dev_uc_add_excl(dev, addr); 601df8ef8f3SJohn Fastabend else if (is_multicast_ether_addr(addr)) 602df8ef8f3SJohn Fastabend err = dev_mc_add_excl(dev, addr); 603df8ef8f3SJohn Fastabend 604df8ef8f3SJohn Fastabend return err; 605df8ef8f3SJohn Fastabend } 606df8ef8f3SJohn Fastabend 6071690be63SVlad Yasevich static int macvlan_fdb_del(struct ndmsg *ndm, struct nlattr *tb[], 608df8ef8f3SJohn Fastabend struct net_device *dev, 6096b6e2725Sstephen hemminger const unsigned char *addr) 610df8ef8f3SJohn Fastabend { 611df8ef8f3SJohn Fastabend struct macvlan_dev *vlan = netdev_priv(dev); 612df8ef8f3SJohn Fastabend int err = -EINVAL; 613df8ef8f3SJohn Fastabend 614df8ef8f3SJohn Fastabend if (!vlan->port->passthru) 615df8ef8f3SJohn Fastabend return -EOPNOTSUPP; 616df8ef8f3SJohn Fastabend 617df8ef8f3SJohn Fastabend if (is_unicast_ether_addr(addr)) 618df8ef8f3SJohn Fastabend err = dev_uc_del(dev, addr); 619df8ef8f3SJohn Fastabend else if (is_multicast_ether_addr(addr)) 620df8ef8f3SJohn Fastabend err = dev_mc_del(dev, addr); 621df8ef8f3SJohn Fastabend 622df8ef8f3SJohn Fastabend return err; 623df8ef8f3SJohn Fastabend } 624df8ef8f3SJohn Fastabend 625b863ceb7SPatrick McHardy static void macvlan_ethtool_get_drvinfo(struct net_device *dev, 626b863ceb7SPatrick McHardy struct ethtool_drvinfo *drvinfo) 627b863ceb7SPatrick McHardy { 6287826d43fSJiri Pirko strlcpy(drvinfo->driver, "macvlan", sizeof(drvinfo->driver)); 6297826d43fSJiri Pirko strlcpy(drvinfo->version, "0.1", sizeof(drvinfo->version)); 630b863ceb7SPatrick McHardy } 631b863ceb7SPatrick McHardy 6329edb8bb6SStephen Hemminger static int macvlan_ethtool_get_settings(struct net_device *dev, 6339edb8bb6SStephen Hemminger struct ethtool_cmd *cmd) 6349edb8bb6SStephen Hemminger { 6359edb8bb6SStephen Hemminger const struct macvlan_dev *vlan = netdev_priv(dev); 6364bc71cb9SJiri Pirko 6374bc71cb9SJiri Pirko return __ethtool_get_settings(vlan->lowerdev, cmd); 6389edb8bb6SStephen Hemminger } 6399edb8bb6SStephen Hemminger 640b863ceb7SPatrick McHardy static const struct ethtool_ops macvlan_ethtool_ops = { 641b863ceb7SPatrick McHardy .get_link = ethtool_op_get_link, 6429edb8bb6SStephen Hemminger .get_settings = macvlan_ethtool_get_settings, 643b863ceb7SPatrick McHardy .get_drvinfo = macvlan_ethtool_get_drvinfo, 644b863ceb7SPatrick McHardy }; 645b863ceb7SPatrick McHardy 64654a30c97SStephen Hemminger static const struct net_device_ops macvlan_netdev_ops = { 64754a30c97SStephen Hemminger .ndo_init = macvlan_init, 648fccaf710SEric Dumazet .ndo_uninit = macvlan_uninit, 64954a30c97SStephen Hemminger .ndo_open = macvlan_open, 65054a30c97SStephen Hemminger .ndo_stop = macvlan_stop, 65100829823SStephen Hemminger .ndo_start_xmit = macvlan_start_xmit, 65254a30c97SStephen Hemminger .ndo_change_mtu = macvlan_change_mtu, 65354a30c97SStephen Hemminger .ndo_change_rx_flags = macvlan_change_rx_flags, 65454a30c97SStephen Hemminger .ndo_set_mac_address = macvlan_set_mac_address, 655df8ef8f3SJohn Fastabend .ndo_set_rx_mode = macvlan_set_mac_lists, 656bc66154eSEric Dumazet .ndo_get_stats64 = macvlan_dev_get_stats64, 65754a30c97SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 6588d13e670SJohn Fastabend .ndo_vlan_rx_add_vid = macvlan_vlan_rx_add_vid, 6598d13e670SJohn Fastabend .ndo_vlan_rx_kill_vid = macvlan_vlan_rx_kill_vid, 660df8ef8f3SJohn Fastabend .ndo_fdb_add = macvlan_fdb_add, 661df8ef8f3SJohn Fastabend .ndo_fdb_del = macvlan_fdb_del, 662df8ef8f3SJohn Fastabend .ndo_fdb_dump = ndo_dflt_fdb_dump, 66354a30c97SStephen Hemminger }; 66454a30c97SStephen Hemminger 6658a35747aSHerbert Xu void macvlan_common_setup(struct net_device *dev) 666b863ceb7SPatrick McHardy { 667b863ceb7SPatrick McHardy ether_setup(dev); 668b863ceb7SPatrick McHardy 669550fd08cSNeil Horman dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING); 67087ab7f6fSVlad Yasevich dev->priv_flags |= IFF_UNICAST_FLT; 67154a30c97SStephen Hemminger dev->netdev_ops = &macvlan_netdev_ops; 672b863ceb7SPatrick McHardy dev->destructor = free_netdev; 6733b04dddeSStephen Hemminger dev->header_ops = &macvlan_hard_header_ops, 674b863ceb7SPatrick McHardy dev->ethtool_ops = &macvlan_ethtool_ops; 6758a35747aSHerbert Xu } 6768a35747aSHerbert Xu EXPORT_SYMBOL_GPL(macvlan_common_setup); 6778a35747aSHerbert Xu 6788a35747aSHerbert Xu static void macvlan_setup(struct net_device *dev) 6798a35747aSHerbert Xu { 6808a35747aSHerbert Xu macvlan_common_setup(dev); 681b863ceb7SPatrick McHardy dev->tx_queue_len = 0; 682b863ceb7SPatrick McHardy } 683b863ceb7SPatrick McHardy 684b863ceb7SPatrick McHardy static int macvlan_port_create(struct net_device *dev) 685b863ceb7SPatrick McHardy { 686b863ceb7SPatrick McHardy struct macvlan_port *port; 687b863ceb7SPatrick McHardy unsigned int i; 688ab95bfe0SJiri Pirko int err; 689b863ceb7SPatrick McHardy 690b863ceb7SPatrick McHardy if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK) 691b863ceb7SPatrick McHardy return -EINVAL; 692b863ceb7SPatrick McHardy 693b863ceb7SPatrick McHardy port = kzalloc(sizeof(*port), GFP_KERNEL); 694b863ceb7SPatrick McHardy if (port == NULL) 695b863ceb7SPatrick McHardy return -ENOMEM; 696b863ceb7SPatrick McHardy 697eb06acdcSSridhar Samudrala port->passthru = false; 698b863ceb7SPatrick McHardy port->dev = dev; 699b863ceb7SPatrick McHardy INIT_LIST_HEAD(&port->vlans); 700b863ceb7SPatrick McHardy for (i = 0; i < MACVLAN_HASH_SIZE; i++) 701b863ceb7SPatrick McHardy INIT_HLIST_HEAD(&port->vlan_hash[i]); 702ab95bfe0SJiri Pirko 703a35e2c1bSJiri Pirko err = netdev_rx_handler_register(dev, macvlan_handle_frame, port); 704a35e2c1bSJiri Pirko if (err) 705ab95bfe0SJiri Pirko kfree(port); 706d9351561SEric Dumazet else 707a35e2c1bSJiri Pirko dev->priv_flags |= IFF_MACVLAN_PORT; 708ab95bfe0SJiri Pirko return err; 709b863ceb7SPatrick McHardy } 710b863ceb7SPatrick McHardy 711b863ceb7SPatrick McHardy static void macvlan_port_destroy(struct net_device *dev) 712b863ceb7SPatrick McHardy { 713e052f7e6SEric Dumazet struct macvlan_port *port = macvlan_port_get_rtnl(dev); 714b863ceb7SPatrick McHardy 715a35e2c1bSJiri Pirko dev->priv_flags &= ~IFF_MACVLAN_PORT; 716ab95bfe0SJiri Pirko netdev_rx_handler_unregister(dev); 7176e070aecSLai Jiangshan kfree_rcu(port, rcu); 718b863ceb7SPatrick McHardy } 719b863ceb7SPatrick McHardy 720b863ceb7SPatrick McHardy static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[]) 721b863ceb7SPatrick McHardy { 722b863ceb7SPatrick McHardy if (tb[IFLA_ADDRESS]) { 723b863ceb7SPatrick McHardy if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) 724b863ceb7SPatrick McHardy return -EINVAL; 725b863ceb7SPatrick McHardy if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) 726b863ceb7SPatrick McHardy return -EADDRNOTAVAIL; 727b863ceb7SPatrick McHardy } 72827c0b1a8SArnd Bergmann 72927c0b1a8SArnd Bergmann if (data && data[IFLA_MACVLAN_MODE]) { 73027c0b1a8SArnd Bergmann switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) { 73127c0b1a8SArnd Bergmann case MACVLAN_MODE_PRIVATE: 73227c0b1a8SArnd Bergmann case MACVLAN_MODE_VEPA: 73327c0b1a8SArnd Bergmann case MACVLAN_MODE_BRIDGE: 734eb06acdcSSridhar Samudrala case MACVLAN_MODE_PASSTHRU: 73527c0b1a8SArnd Bergmann break; 73627c0b1a8SArnd Bergmann default: 73727c0b1a8SArnd Bergmann return -EINVAL; 73827c0b1a8SArnd Bergmann } 73927c0b1a8SArnd Bergmann } 740b863ceb7SPatrick McHardy return 0; 741b863ceb7SPatrick McHardy } 742b863ceb7SPatrick McHardy 743fc0663d6SArnd Bergmann int macvlan_common_newlink(struct net *src_net, struct net_device *dev, 744fc0663d6SArnd Bergmann struct nlattr *tb[], struct nlattr *data[], 745fc0663d6SArnd Bergmann int (*receive)(struct sk_buff *skb), 746fc0663d6SArnd Bergmann int (*forward)(struct net_device *dev, 747fc0663d6SArnd Bergmann struct sk_buff *skb)) 748b863ceb7SPatrick McHardy { 749b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 750b863ceb7SPatrick McHardy struct macvlan_port *port; 751b863ceb7SPatrick McHardy struct net_device *lowerdev; 752b863ceb7SPatrick McHardy int err; 753b863ceb7SPatrick McHardy 754b863ceb7SPatrick McHardy if (!tb[IFLA_LINK]) 755b863ceb7SPatrick McHardy return -EINVAL; 756b863ceb7SPatrick McHardy 75781adee47SEric W. Biederman lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK])); 758b863ceb7SPatrick McHardy if (lowerdev == NULL) 759b863ceb7SPatrick McHardy return -ENODEV; 760b863ceb7SPatrick McHardy 761b0832a29SEric Biederman /* When creating macvlans on top of other macvlans - use 762b0832a29SEric Biederman * the real device as the lowerdev. 763a6ca5f1dSPatrick McHardy */ 764b0832a29SEric Biederman if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) { 765b0832a29SEric Biederman struct macvlan_dev *lowervlan = netdev_priv(lowerdev); 766b0832a29SEric Biederman lowerdev = lowervlan->lowerdev; 767b0832a29SEric Biederman } 768a6ca5f1dSPatrick McHardy 769b863ceb7SPatrick McHardy if (!tb[IFLA_MTU]) 770b863ceb7SPatrick McHardy dev->mtu = lowerdev->mtu; 771b863ceb7SPatrick McHardy else if (dev->mtu > lowerdev->mtu) 772b863ceb7SPatrick McHardy return -EINVAL; 773b863ceb7SPatrick McHardy 774b863ceb7SPatrick McHardy if (!tb[IFLA_ADDRESS]) 7757ce5d222SDanny Kukawka eth_hw_addr_random(dev); 776b863ceb7SPatrick McHardy 777a35e2c1bSJiri Pirko if (!macvlan_port_exists(lowerdev)) { 778b863ceb7SPatrick McHardy err = macvlan_port_create(lowerdev); 779b863ceb7SPatrick McHardy if (err < 0) 780b863ceb7SPatrick McHardy return err; 781b863ceb7SPatrick McHardy } 782e052f7e6SEric Dumazet port = macvlan_port_get_rtnl(lowerdev); 783b863ceb7SPatrick McHardy 784eb06acdcSSridhar Samudrala /* Only 1 macvlan device can be created in passthru mode */ 785eb06acdcSSridhar Samudrala if (port->passthru) 786eb06acdcSSridhar Samudrala return -EINVAL; 787eb06acdcSSridhar Samudrala 788b863ceb7SPatrick McHardy vlan->lowerdev = lowerdev; 789b863ceb7SPatrick McHardy vlan->dev = dev; 790b863ceb7SPatrick McHardy vlan->port = port; 791fc0663d6SArnd Bergmann vlan->receive = receive; 792fc0663d6SArnd Bergmann vlan->forward = forward; 793b863ceb7SPatrick McHardy 79427c0b1a8SArnd Bergmann vlan->mode = MACVLAN_MODE_VEPA; 79527c0b1a8SArnd Bergmann if (data && data[IFLA_MACVLAN_MODE]) 79627c0b1a8SArnd Bergmann vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]); 79727c0b1a8SArnd Bergmann 798df8ef8f3SJohn Fastabend if (data && data[IFLA_MACVLAN_FLAGS]) 799df8ef8f3SJohn Fastabend vlan->flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]); 800df8ef8f3SJohn Fastabend 801eb06acdcSSridhar Samudrala if (vlan->mode == MACVLAN_MODE_PASSTHRU) { 802d5cd9244SEric W. Biederman if (port->count) 803eb06acdcSSridhar Samudrala return -EINVAL; 804eb06acdcSSridhar Samudrala port->passthru = true; 805eb06acdcSSridhar Samudrala memcpy(dev->dev_addr, lowerdev->dev_addr, ETH_ALEN); 806eb06acdcSSridhar Samudrala } 807eb06acdcSSridhar Samudrala 8087cd43db7SJiri Pirko err = netdev_upper_dev_link(lowerdev, dev); 8097cd43db7SJiri Pirko if (err) 8107cd43db7SJiri Pirko goto destroy_port; 8117cd43db7SJiri Pirko 812d5cd9244SEric W. Biederman port->count += 1; 813b863ceb7SPatrick McHardy err = register_netdevice(dev); 814b863ceb7SPatrick McHardy if (err < 0) 8157cd43db7SJiri Pirko goto upper_dev_unlink; 816b863ceb7SPatrick McHardy 817b863ceb7SPatrick McHardy list_add_tail(&vlan->list, &port->vlans); 818fc4a7489SPatrick Mullaney netif_stacked_transfer_operstate(lowerdev, dev); 819f16d3d57SJiri Pirko 820b863ceb7SPatrick McHardy return 0; 821f16d3d57SJiri Pirko 8227cd43db7SJiri Pirko upper_dev_unlink: 8237cd43db7SJiri Pirko netdev_upper_dev_unlink(lowerdev, dev); 824f16d3d57SJiri Pirko destroy_port: 825d5cd9244SEric W. Biederman port->count -= 1; 826d5cd9244SEric W. Biederman if (!port->count) 827f16d3d57SJiri Pirko macvlan_port_destroy(lowerdev); 828f16d3d57SJiri Pirko 829f16d3d57SJiri Pirko return err; 830b863ceb7SPatrick McHardy } 831fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_common_newlink); 832b863ceb7SPatrick McHardy 833fc0663d6SArnd Bergmann static int macvlan_newlink(struct net *src_net, struct net_device *dev, 834fc0663d6SArnd Bergmann struct nlattr *tb[], struct nlattr *data[]) 835fc0663d6SArnd Bergmann { 836fc0663d6SArnd Bergmann return macvlan_common_newlink(src_net, dev, tb, data, 837fc0663d6SArnd Bergmann netif_rx, 838fc0663d6SArnd Bergmann dev_forward_skb); 839fc0663d6SArnd Bergmann } 840fc0663d6SArnd Bergmann 841fc0663d6SArnd Bergmann void macvlan_dellink(struct net_device *dev, struct list_head *head) 842b863ceb7SPatrick McHardy { 843b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 844b863ceb7SPatrick McHardy 845b863ceb7SPatrick McHardy list_del(&vlan->list); 84623289a37SEric Dumazet unregister_netdevice_queue(dev, head); 8477cd43db7SJiri Pirko netdev_upper_dev_unlink(vlan->lowerdev, dev); 848b863ceb7SPatrick McHardy } 849fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_dellink); 850b863ceb7SPatrick McHardy 85127c0b1a8SArnd Bergmann static int macvlan_changelink(struct net_device *dev, 85227c0b1a8SArnd Bergmann struct nlattr *tb[], struct nlattr *data[]) 85327c0b1a8SArnd Bergmann { 85427c0b1a8SArnd Bergmann struct macvlan_dev *vlan = netdev_priv(dev); 85527c0b1a8SArnd Bergmann if (data && data[IFLA_MACVLAN_MODE]) 85627c0b1a8SArnd Bergmann vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]); 857df8ef8f3SJohn Fastabend if (data && data[IFLA_MACVLAN_FLAGS]) { 858df8ef8f3SJohn Fastabend __u16 flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]); 859df8ef8f3SJohn Fastabend bool promisc = (flags ^ vlan->flags) & MACVLAN_FLAG_NOPROMISC; 860df8ef8f3SJohn Fastabend 861df8ef8f3SJohn Fastabend if (promisc && (flags & MACVLAN_FLAG_NOPROMISC)) 862df8ef8f3SJohn Fastabend dev_set_promiscuity(vlan->lowerdev, -1); 863df8ef8f3SJohn Fastabend else if (promisc && !(flags & MACVLAN_FLAG_NOPROMISC)) 864df8ef8f3SJohn Fastabend dev_set_promiscuity(vlan->lowerdev, 1); 865df8ef8f3SJohn Fastabend vlan->flags = flags; 866df8ef8f3SJohn Fastabend } 86727c0b1a8SArnd Bergmann return 0; 86827c0b1a8SArnd Bergmann } 86927c0b1a8SArnd Bergmann 87027c0b1a8SArnd Bergmann static size_t macvlan_get_size(const struct net_device *dev) 87127c0b1a8SArnd Bergmann { 87201fe944fSEric Dumazet return (0 87301fe944fSEric Dumazet + nla_total_size(4) /* IFLA_MACVLAN_MODE */ 87401fe944fSEric Dumazet + nla_total_size(2) /* IFLA_MACVLAN_FLAGS */ 87501fe944fSEric Dumazet ); 87627c0b1a8SArnd Bergmann } 87727c0b1a8SArnd Bergmann 87827c0b1a8SArnd Bergmann static int macvlan_fill_info(struct sk_buff *skb, 87927c0b1a8SArnd Bergmann const struct net_device *dev) 88027c0b1a8SArnd Bergmann { 88127c0b1a8SArnd Bergmann struct macvlan_dev *vlan = netdev_priv(dev); 88227c0b1a8SArnd Bergmann 883ead9a76cSDavid S. Miller if (nla_put_u32(skb, IFLA_MACVLAN_MODE, vlan->mode)) 884ead9a76cSDavid S. Miller goto nla_put_failure; 885df8ef8f3SJohn Fastabend if (nla_put_u16(skb, IFLA_MACVLAN_FLAGS, vlan->flags)) 886df8ef8f3SJohn Fastabend goto nla_put_failure; 88727c0b1a8SArnd Bergmann return 0; 88827c0b1a8SArnd Bergmann 88927c0b1a8SArnd Bergmann nla_put_failure: 89027c0b1a8SArnd Bergmann return -EMSGSIZE; 89127c0b1a8SArnd Bergmann } 89227c0b1a8SArnd Bergmann 89327c0b1a8SArnd Bergmann static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = { 89427c0b1a8SArnd Bergmann [IFLA_MACVLAN_MODE] = { .type = NLA_U32 }, 895df8ef8f3SJohn Fastabend [IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 }, 89627c0b1a8SArnd Bergmann }; 89727c0b1a8SArnd Bergmann 898fc0663d6SArnd Bergmann int macvlan_link_register(struct rtnl_link_ops *ops) 899fc0663d6SArnd Bergmann { 900fc0663d6SArnd Bergmann /* common fields */ 901fc0663d6SArnd Bergmann ops->priv_size = sizeof(struct macvlan_dev); 902fc0663d6SArnd Bergmann ops->validate = macvlan_validate; 903fc0663d6SArnd Bergmann ops->maxtype = IFLA_MACVLAN_MAX; 904fc0663d6SArnd Bergmann ops->policy = macvlan_policy; 905fc0663d6SArnd Bergmann ops->changelink = macvlan_changelink; 906fc0663d6SArnd Bergmann ops->get_size = macvlan_get_size; 907fc0663d6SArnd Bergmann ops->fill_info = macvlan_fill_info; 908fc0663d6SArnd Bergmann 909fc0663d6SArnd Bergmann return rtnl_link_register(ops); 910fc0663d6SArnd Bergmann }; 911fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_link_register); 912fc0663d6SArnd Bergmann 913fc0663d6SArnd Bergmann static struct rtnl_link_ops macvlan_link_ops = { 914b863ceb7SPatrick McHardy .kind = "macvlan", 9158a35747aSHerbert Xu .setup = macvlan_setup, 916b863ceb7SPatrick McHardy .newlink = macvlan_newlink, 917b863ceb7SPatrick McHardy .dellink = macvlan_dellink, 918b863ceb7SPatrick McHardy }; 919b863ceb7SPatrick McHardy 920b863ceb7SPatrick McHardy static int macvlan_device_event(struct notifier_block *unused, 921b863ceb7SPatrick McHardy unsigned long event, void *ptr) 922b863ceb7SPatrick McHardy { 923b863ceb7SPatrick McHardy struct net_device *dev = ptr; 924b863ceb7SPatrick McHardy struct macvlan_dev *vlan, *next; 925b863ceb7SPatrick McHardy struct macvlan_port *port; 926226bd341SEric Dumazet LIST_HEAD(list_kill); 927b863ceb7SPatrick McHardy 928a35e2c1bSJiri Pirko if (!macvlan_port_exists(dev)) 929b863ceb7SPatrick McHardy return NOTIFY_DONE; 930b863ceb7SPatrick McHardy 931e052f7e6SEric Dumazet port = macvlan_port_get_rtnl(dev); 932a35e2c1bSJiri Pirko 933b863ceb7SPatrick McHardy switch (event) { 934b863ceb7SPatrick McHardy case NETDEV_CHANGE: 935b863ceb7SPatrick McHardy list_for_each_entry(vlan, &port->vlans, list) 936fc4a7489SPatrick Mullaney netif_stacked_transfer_operstate(vlan->lowerdev, 937fc4a7489SPatrick Mullaney vlan->dev); 938b863ceb7SPatrick McHardy break; 939b863ceb7SPatrick McHardy case NETDEV_FEAT_CHANGE: 940b863ceb7SPatrick McHardy list_for_each_entry(vlan, &port->vlans, list) { 941b863ceb7SPatrick McHardy vlan->dev->features = dev->features & MACVLAN_FEATURES; 9428c2acc53SPatrick McHardy vlan->dev->gso_max_size = dev->gso_max_size; 943b863ceb7SPatrick McHardy netdev_features_change(vlan->dev); 944b863ceb7SPatrick McHardy } 945b863ceb7SPatrick McHardy break; 946b863ceb7SPatrick McHardy case NETDEV_UNREGISTER: 9473b27e105SDavid Lamparter /* twiddle thumbs on netns device moves */ 9483b27e105SDavid Lamparter if (dev->reg_state != NETREG_UNREGISTERING) 9493b27e105SDavid Lamparter break; 9503b27e105SDavid Lamparter 951b863ceb7SPatrick McHardy list_for_each_entry_safe(vlan, next, &port->vlans, list) 952226bd341SEric Dumazet vlan->dev->rtnl_link_ops->dellink(vlan->dev, &list_kill); 953226bd341SEric Dumazet unregister_netdevice_many(&list_kill); 954226bd341SEric Dumazet list_del(&list_kill); 955b863ceb7SPatrick McHardy break; 9561c01fe14SJiri Pirko case NETDEV_PRE_TYPE_CHANGE: 9571c01fe14SJiri Pirko /* Forbid underlaying device to change its type. */ 9581c01fe14SJiri Pirko return NOTIFY_BAD; 959b863ceb7SPatrick McHardy } 960b863ceb7SPatrick McHardy return NOTIFY_DONE; 961b863ceb7SPatrick McHardy } 962b863ceb7SPatrick McHardy 963b863ceb7SPatrick McHardy static struct notifier_block macvlan_notifier_block __read_mostly = { 964b863ceb7SPatrick McHardy .notifier_call = macvlan_device_event, 965b863ceb7SPatrick McHardy }; 966b863ceb7SPatrick McHardy 967b863ceb7SPatrick McHardy static int __init macvlan_init_module(void) 968b863ceb7SPatrick McHardy { 969b863ceb7SPatrick McHardy int err; 970b863ceb7SPatrick McHardy 971b863ceb7SPatrick McHardy register_netdevice_notifier(&macvlan_notifier_block); 972b863ceb7SPatrick McHardy 973fc0663d6SArnd Bergmann err = macvlan_link_register(&macvlan_link_ops); 974b863ceb7SPatrick McHardy if (err < 0) 975b863ceb7SPatrick McHardy goto err1; 976b863ceb7SPatrick McHardy return 0; 977b863ceb7SPatrick McHardy err1: 978b863ceb7SPatrick McHardy unregister_netdevice_notifier(&macvlan_notifier_block); 979b863ceb7SPatrick McHardy return err; 980b863ceb7SPatrick McHardy } 981b863ceb7SPatrick McHardy 982b863ceb7SPatrick McHardy static void __exit macvlan_cleanup_module(void) 983b863ceb7SPatrick McHardy { 984b863ceb7SPatrick McHardy rtnl_link_unregister(&macvlan_link_ops); 985b863ceb7SPatrick McHardy unregister_netdevice_notifier(&macvlan_notifier_block); 986b863ceb7SPatrick McHardy } 987b863ceb7SPatrick McHardy 988b863ceb7SPatrick McHardy module_init(macvlan_init_module); 989b863ceb7SPatrick McHardy module_exit(macvlan_cleanup_module); 990b863ceb7SPatrick McHardy 991b863ceb7SPatrick McHardy MODULE_LICENSE("GPL"); 992b863ceb7SPatrick McHardy MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); 993b863ceb7SPatrick McHardy MODULE_DESCRIPTION("Driver for MAC address based VLANs"); 994b863ceb7SPatrick McHardy MODULE_ALIAS_RTNL_LINK("macvlan"); 995