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> 33412ca155SHerbert Xu #include <linux/workqueue.h> 34b863ceb7SPatrick McHardy #include <net/rtnetlink.h> 35618e1b74SArnd Bergmann #include <net/xfrm.h> 36688cea83Sdingtianhong #include <linux/netpoll.h> 37b863ceb7SPatrick McHardy 3879cf79abSMichael Braun #define MACVLAN_HASH_BITS 8 3979cf79abSMichael Braun #define MACVLAN_HASH_SIZE (1<<MACVLAN_HASH_BITS) 4007d92d5cSNicolas Dichtel #define MACVLAN_BC_QUEUE_LEN 1000 41b863ceb7SPatrick McHardy 4243c2d578SVlad Yasevich #define MACVLAN_F_PASSTHRU 1 4318c8c54dSVlad Yasevich #define MACVLAN_F_ADDRCHANGE 2 4443c2d578SVlad Yasevich 45b863ceb7SPatrick McHardy struct macvlan_port { 46b863ceb7SPatrick McHardy struct net_device *dev; 47b863ceb7SPatrick McHardy struct hlist_head vlan_hash[MACVLAN_HASH_SIZE]; 48b863ceb7SPatrick McHardy struct list_head vlans; 49412ca155SHerbert Xu struct sk_buff_head bc_queue; 50412ca155SHerbert Xu struct work_struct bc_work; 5143c2d578SVlad Yasevich u32 flags; 525e3c516bSDavid S. Miller int count; 5379cf79abSMichael Braun struct hlist_head vlan_source_hash[MACVLAN_HASH_SIZE]; 549c127a01SHerbert Xu DECLARE_BITMAP(mc_filter, MACVLAN_MC_FILTER_SZ); 5518c8c54dSVlad Yasevich unsigned char perm_addr[ETH_ALEN]; 5679cf79abSMichael Braun }; 5779cf79abSMichael Braun 5879cf79abSMichael Braun struct macvlan_source_entry { 5979cf79abSMichael Braun struct hlist_node hlist; 6079cf79abSMichael Braun struct macvlan_dev *vlan; 6179cf79abSMichael Braun unsigned char addr[6+2] __aligned(sizeof(u16)); 6279cf79abSMichael Braun struct rcu_head rcu; 63b863ceb7SPatrick McHardy }; 64b863ceb7SPatrick McHardy 65412ca155SHerbert Xu struct macvlan_skb_cb { 66412ca155SHerbert Xu const struct macvlan_dev *src; 67412ca155SHerbert Xu }; 68412ca155SHerbert Xu 69412ca155SHerbert Xu #define MACVLAN_SKB_CB(__skb) ((struct macvlan_skb_cb *)&((__skb)->cb[0])) 70412ca155SHerbert Xu 71d5cd9244SEric W. Biederman static void macvlan_port_destroy(struct net_device *dev); 72d5cd9244SEric W. Biederman 7343c2d578SVlad Yasevich static inline bool macvlan_passthru(const struct macvlan_port *port) 7443c2d578SVlad Yasevich { 7543c2d578SVlad Yasevich return port->flags & MACVLAN_F_PASSTHRU; 7643c2d578SVlad Yasevich } 7743c2d578SVlad Yasevich 7843c2d578SVlad Yasevich static inline void macvlan_set_passthru(struct macvlan_port *port) 7943c2d578SVlad Yasevich { 8043c2d578SVlad Yasevich port->flags |= MACVLAN_F_PASSTHRU; 8143c2d578SVlad Yasevich } 8243c2d578SVlad Yasevich 8318c8c54dSVlad Yasevich static inline bool macvlan_addr_change(const struct macvlan_port *port) 8418c8c54dSVlad Yasevich { 8518c8c54dSVlad Yasevich return port->flags & MACVLAN_F_ADDRCHANGE; 8618c8c54dSVlad Yasevich } 8718c8c54dSVlad Yasevich 8818c8c54dSVlad Yasevich static inline void macvlan_set_addr_change(struct macvlan_port *port) 8918c8c54dSVlad Yasevich { 9018c8c54dSVlad Yasevich port->flags |= MACVLAN_F_ADDRCHANGE; 9118c8c54dSVlad Yasevich } 9218c8c54dSVlad Yasevich 9318c8c54dSVlad Yasevich static inline void macvlan_clear_addr_change(struct macvlan_port *port) 9418c8c54dSVlad Yasevich { 9518c8c54dSVlad Yasevich port->flags &= ~MACVLAN_F_ADDRCHANGE; 9618c8c54dSVlad Yasevich } 9718c8c54dSVlad Yasevich 9879cf79abSMichael Braun /* Hash Ethernet address */ 9979cf79abSMichael Braun static u32 macvlan_eth_hash(const unsigned char *addr) 10079cf79abSMichael Braun { 10179cf79abSMichael Braun u64 value = get_unaligned((u64 *)addr); 10279cf79abSMichael Braun 10379cf79abSMichael Braun /* only want 6 bytes */ 10479cf79abSMichael Braun #ifdef __BIG_ENDIAN 10579cf79abSMichael Braun value >>= 16; 10679cf79abSMichael Braun #else 10779cf79abSMichael Braun value <<= 16; 10879cf79abSMichael Braun #endif 10979cf79abSMichael Braun return hash_64(value, MACVLAN_HASH_BITS); 11079cf79abSMichael Braun } 11179cf79abSMichael Braun 112e052f7e6SEric Dumazet static struct macvlan_port *macvlan_port_get_rcu(const struct net_device *dev) 113e052f7e6SEric Dumazet { 114e052f7e6SEric Dumazet return rcu_dereference(dev->rx_handler_data); 115e052f7e6SEric Dumazet } 116e052f7e6SEric Dumazet 117e052f7e6SEric Dumazet static struct macvlan_port *macvlan_port_get_rtnl(const struct net_device *dev) 118e052f7e6SEric Dumazet { 119e052f7e6SEric Dumazet return rtnl_dereference(dev->rx_handler_data); 120e052f7e6SEric Dumazet } 121e052f7e6SEric Dumazet 122a35e2c1bSJiri Pirko #define macvlan_port_exists(dev) (dev->priv_flags & IFF_MACVLAN_PORT) 123a35e2c1bSJiri Pirko 124b863ceb7SPatrick McHardy static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port, 125b863ceb7SPatrick McHardy const unsigned char *addr) 126b863ceb7SPatrick McHardy { 127b863ceb7SPatrick McHardy struct macvlan_dev *vlan; 12879cf79abSMichael Braun u32 idx = macvlan_eth_hash(addr); 129b863ceb7SPatrick McHardy 13079cf79abSMichael Braun hlist_for_each_entry_rcu(vlan, &port->vlan_hash[idx], hlist) { 131a6700db1SJoe Perches if (ether_addr_equal_64bits(vlan->dev->dev_addr, addr)) 132b863ceb7SPatrick McHardy return vlan; 133b863ceb7SPatrick McHardy } 134b863ceb7SPatrick McHardy return NULL; 135b863ceb7SPatrick McHardy } 136b863ceb7SPatrick McHardy 13779cf79abSMichael Braun static struct macvlan_source_entry *macvlan_hash_lookup_source( 13879cf79abSMichael Braun const struct macvlan_dev *vlan, 13979cf79abSMichael Braun const unsigned char *addr) 14079cf79abSMichael Braun { 14179cf79abSMichael Braun struct macvlan_source_entry *entry; 14279cf79abSMichael Braun u32 idx = macvlan_eth_hash(addr); 14379cf79abSMichael Braun struct hlist_head *h = &vlan->port->vlan_source_hash[idx]; 14479cf79abSMichael Braun 14579cf79abSMichael Braun hlist_for_each_entry_rcu(entry, h, hlist) { 14679cf79abSMichael Braun if (ether_addr_equal_64bits(entry->addr, addr) && 14779cf79abSMichael Braun entry->vlan == vlan) 14879cf79abSMichael Braun return entry; 14979cf79abSMichael Braun } 15079cf79abSMichael Braun return NULL; 15179cf79abSMichael Braun } 15279cf79abSMichael Braun 15379cf79abSMichael Braun static int macvlan_hash_add_source(struct macvlan_dev *vlan, 15479cf79abSMichael Braun const unsigned char *addr) 15579cf79abSMichael Braun { 15679cf79abSMichael Braun struct macvlan_port *port = vlan->port; 15779cf79abSMichael Braun struct macvlan_source_entry *entry; 15879cf79abSMichael Braun struct hlist_head *h; 15979cf79abSMichael Braun 16079cf79abSMichael Braun entry = macvlan_hash_lookup_source(vlan, addr); 16179cf79abSMichael Braun if (entry) 16279cf79abSMichael Braun return 0; 16379cf79abSMichael Braun 16479cf79abSMichael Braun entry = kmalloc(sizeof(*entry), GFP_KERNEL); 16579cf79abSMichael Braun if (!entry) 16679cf79abSMichael Braun return -ENOMEM; 16779cf79abSMichael Braun 16879cf79abSMichael Braun ether_addr_copy(entry->addr, addr); 16979cf79abSMichael Braun entry->vlan = vlan; 17079cf79abSMichael Braun h = &port->vlan_source_hash[macvlan_eth_hash(addr)]; 17179cf79abSMichael Braun hlist_add_head_rcu(&entry->hlist, h); 17279cf79abSMichael Braun vlan->macaddr_count++; 17379cf79abSMichael Braun 17479cf79abSMichael Braun return 0; 17579cf79abSMichael Braun } 17679cf79abSMichael Braun 177f9ac30f0SEric Biederman static void macvlan_hash_add(struct macvlan_dev *vlan) 178f9ac30f0SEric Biederman { 179f9ac30f0SEric Biederman struct macvlan_port *port = vlan->port; 180f9ac30f0SEric Biederman const unsigned char *addr = vlan->dev->dev_addr; 18179cf79abSMichael Braun u32 idx = macvlan_eth_hash(addr); 182f9ac30f0SEric Biederman 18379cf79abSMichael Braun hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[idx]); 18479cf79abSMichael Braun } 18579cf79abSMichael Braun 18679cf79abSMichael Braun static void macvlan_hash_del_source(struct macvlan_source_entry *entry) 18779cf79abSMichael Braun { 18879cf79abSMichael Braun hlist_del_rcu(&entry->hlist); 18979cf79abSMichael Braun kfree_rcu(entry, rcu); 190f9ac30f0SEric Biederman } 191f9ac30f0SEric Biederman 192449f4544SEric Dumazet static void macvlan_hash_del(struct macvlan_dev *vlan, bool sync) 193f9ac30f0SEric Biederman { 194f9ac30f0SEric Biederman hlist_del_rcu(&vlan->hlist); 195449f4544SEric Dumazet if (sync) 196f9ac30f0SEric Biederman synchronize_rcu(); 197f9ac30f0SEric Biederman } 198f9ac30f0SEric Biederman 199f9ac30f0SEric Biederman static void macvlan_hash_change_addr(struct macvlan_dev *vlan, 200f9ac30f0SEric Biederman const unsigned char *addr) 201f9ac30f0SEric Biederman { 202449f4544SEric Dumazet macvlan_hash_del(vlan, true); 203f9ac30f0SEric Biederman /* Now that we are unhashed it is safe to change the device 204f9ac30f0SEric Biederman * address without confusing packet delivery. 205f9ac30f0SEric Biederman */ 206f9ac30f0SEric Biederman memcpy(vlan->dev->dev_addr, addr, ETH_ALEN); 207f9ac30f0SEric Biederman macvlan_hash_add(vlan); 208f9ac30f0SEric Biederman } 209f9ac30f0SEric Biederman 210d94d0254SGao Feng static bool macvlan_addr_busy(const struct macvlan_port *port, 211f9ac30f0SEric Biederman const unsigned char *addr) 212f9ac30f0SEric Biederman { 21318c8c54dSVlad Yasevich /* Test to see if the specified address is 214f9ac30f0SEric Biederman * currently in use by the underlying device or 215f9ac30f0SEric Biederman * another macvlan. 216f9ac30f0SEric Biederman */ 21718c8c54dSVlad Yasevich if (!macvlan_passthru(port) && !macvlan_addr_change(port) && 218e696cda7SVlad Yasevich ether_addr_equal_64bits(port->dev->dev_addr, addr)) 219d94d0254SGao Feng return true; 220f9ac30f0SEric Biederman 221f9ac30f0SEric Biederman if (macvlan_hash_lookup(port, addr)) 222d94d0254SGao Feng return true; 223f9ac30f0SEric Biederman 224d94d0254SGao Feng return false; 225f9ac30f0SEric Biederman } 226f9ac30f0SEric Biederman 227a1e514c5SArnd Bergmann 228fc0663d6SArnd Bergmann static int macvlan_broadcast_one(struct sk_buff *skb, 229fc0663d6SArnd Bergmann const struct macvlan_dev *vlan, 230618e1b74SArnd Bergmann const struct ethhdr *eth, bool local) 231a1e514c5SArnd Bergmann { 232fc0663d6SArnd Bergmann struct net_device *dev = vlan->dev; 233a1e514c5SArnd Bergmann 234618e1b74SArnd Bergmann if (local) 235412ca155SHerbert Xu return __dev_forward_skb(dev, skb); 236618e1b74SArnd Bergmann 237a1e514c5SArnd Bergmann skb->dev = dev; 238a6700db1SJoe Perches if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast)) 239a1e514c5SArnd Bergmann skb->pkt_type = PACKET_BROADCAST; 240a1e514c5SArnd Bergmann else 241a1e514c5SArnd Bergmann skb->pkt_type = PACKET_MULTICAST; 242a1e514c5SArnd Bergmann 243412ca155SHerbert Xu return 0; 244a1e514c5SArnd Bergmann } 245a1e514c5SArnd Bergmann 2463807ff58SEric Dumazet static u32 macvlan_hash_mix(const struct macvlan_dev *vlan) 2473807ff58SEric Dumazet { 2483807ff58SEric Dumazet return (u32)(((unsigned long)vlan) >> L1_CACHE_SHIFT); 2493807ff58SEric Dumazet } 2503807ff58SEric Dumazet 2513807ff58SEric Dumazet 2523807ff58SEric Dumazet static unsigned int mc_hash(const struct macvlan_dev *vlan, 2533807ff58SEric Dumazet const unsigned char *addr) 254cd431e73SEric Dumazet { 255cd431e73SEric Dumazet u32 val = __get_unaligned_cpu32(addr + 2); 256cd431e73SEric Dumazet 2573807ff58SEric Dumazet val ^= macvlan_hash_mix(vlan); 258cd431e73SEric Dumazet return hash_32(val, MACVLAN_MC_FILTER_BITS); 259cd431e73SEric Dumazet } 260cd431e73SEric Dumazet 261b863ceb7SPatrick McHardy static void macvlan_broadcast(struct sk_buff *skb, 262618e1b74SArnd Bergmann const struct macvlan_port *port, 263618e1b74SArnd Bergmann struct net_device *src, 264618e1b74SArnd Bergmann enum macvlan_mode mode) 265b863ceb7SPatrick McHardy { 266b863ceb7SPatrick McHardy const struct ethhdr *eth = eth_hdr(skb); 267b863ceb7SPatrick McHardy const struct macvlan_dev *vlan; 268b863ceb7SPatrick McHardy struct sk_buff *nskb; 269b863ceb7SPatrick McHardy unsigned int i; 270a1e514c5SArnd Bergmann int err; 2713807ff58SEric Dumazet unsigned int hash; 272b863ceb7SPatrick McHardy 273efbbced3SPatrick McHardy if (skb->protocol == htons(ETH_P_PAUSE)) 274efbbced3SPatrick McHardy return; 275efbbced3SPatrick McHardy 276b863ceb7SPatrick McHardy for (i = 0; i < MACVLAN_HASH_SIZE; i++) { 277b67bfe0dSSasha Levin hlist_for_each_entry_rcu(vlan, &port->vlan_hash[i], hlist) { 278618e1b74SArnd Bergmann if (vlan->dev == src || !(vlan->mode & mode)) 279618e1b74SArnd Bergmann continue; 280618e1b74SArnd Bergmann 2813807ff58SEric Dumazet hash = mc_hash(vlan, eth->h_dest); 282cd431e73SEric Dumazet if (!test_bit(hash, vlan->mc_filter)) 283cd431e73SEric Dumazet continue; 284de9e8f3fSHerbert Xu 285de9e8f3fSHerbert Xu err = NET_RX_DROP; 286b863ceb7SPatrick McHardy nskb = skb_clone(skb, GFP_ATOMIC); 287de9e8f3fSHerbert Xu if (likely(nskb)) 288de9e8f3fSHerbert Xu err = macvlan_broadcast_one( 289de9e8f3fSHerbert Xu nskb, vlan, eth, 290412ca155SHerbert Xu mode == MACVLAN_MODE_BRIDGE) ?: 291412ca155SHerbert Xu netif_rx_ni(nskb); 292a1e514c5SArnd Bergmann macvlan_count_rx(vlan, skb->len + ETH_HLEN, 2934c979935Sjbaron@akamai.com err == NET_RX_SUCCESS, true); 294b863ceb7SPatrick McHardy } 295b863ceb7SPatrick McHardy } 296b863ceb7SPatrick McHardy } 297b863ceb7SPatrick McHardy 298412ca155SHerbert Xu static void macvlan_process_broadcast(struct work_struct *w) 299412ca155SHerbert Xu { 300412ca155SHerbert Xu struct macvlan_port *port = container_of(w, struct macvlan_port, 301412ca155SHerbert Xu bc_work); 302412ca155SHerbert Xu struct sk_buff *skb; 303412ca155SHerbert Xu struct sk_buff_head list; 304412ca155SHerbert Xu 305fe0ca732SEric Dumazet __skb_queue_head_init(&list); 306412ca155SHerbert Xu 307412ca155SHerbert Xu spin_lock_bh(&port->bc_queue.lock); 308412ca155SHerbert Xu skb_queue_splice_tail_init(&port->bc_queue, &list); 309412ca155SHerbert Xu spin_unlock_bh(&port->bc_queue.lock); 310412ca155SHerbert Xu 311412ca155SHerbert Xu while ((skb = __skb_dequeue(&list))) { 312412ca155SHerbert Xu const struct macvlan_dev *src = MACVLAN_SKB_CB(skb)->src; 313412ca155SHerbert Xu 314412ca155SHerbert Xu rcu_read_lock(); 315412ca155SHerbert Xu 316412ca155SHerbert Xu if (!src) 317412ca155SHerbert Xu /* frame comes from an external address */ 318412ca155SHerbert Xu macvlan_broadcast(skb, port, NULL, 319412ca155SHerbert Xu MACVLAN_MODE_PRIVATE | 320412ca155SHerbert Xu MACVLAN_MODE_VEPA | 321412ca155SHerbert Xu MACVLAN_MODE_PASSTHRU| 322412ca155SHerbert Xu MACVLAN_MODE_BRIDGE); 323412ca155SHerbert Xu else if (src->mode == MACVLAN_MODE_VEPA) 324412ca155SHerbert Xu /* flood to everyone except source */ 325412ca155SHerbert Xu macvlan_broadcast(skb, port, src->dev, 326412ca155SHerbert Xu MACVLAN_MODE_VEPA | 327412ca155SHerbert Xu MACVLAN_MODE_BRIDGE); 328412ca155SHerbert Xu else 329412ca155SHerbert Xu /* 330412ca155SHerbert Xu * flood only to VEPA ports, bridge ports 331412ca155SHerbert Xu * already saw the frame on the way out. 332412ca155SHerbert Xu */ 333412ca155SHerbert Xu macvlan_broadcast(skb, port, src->dev, 334412ca155SHerbert Xu MACVLAN_MODE_VEPA); 335412ca155SHerbert Xu 336412ca155SHerbert Xu rcu_read_unlock(); 337412ca155SHerbert Xu 338260916dfSHerbert Xu if (src) 339260916dfSHerbert Xu dev_put(src->dev); 340412ca155SHerbert Xu kfree_skb(skb); 341412ca155SHerbert Xu } 342412ca155SHerbert Xu } 343412ca155SHerbert Xu 344412ca155SHerbert Xu static void macvlan_broadcast_enqueue(struct macvlan_port *port, 345260916dfSHerbert Xu const struct macvlan_dev *src, 346412ca155SHerbert Xu struct sk_buff *skb) 347412ca155SHerbert Xu { 348e676f197SHerbert Xu struct sk_buff *nskb; 349412ca155SHerbert Xu int err = -ENOMEM; 350412ca155SHerbert Xu 351e676f197SHerbert Xu nskb = skb_clone(skb, GFP_ATOMIC); 352e676f197SHerbert Xu if (!nskb) 353412ca155SHerbert Xu goto err; 354412ca155SHerbert Xu 355260916dfSHerbert Xu MACVLAN_SKB_CB(nskb)->src = src; 356260916dfSHerbert Xu 357412ca155SHerbert Xu spin_lock(&port->bc_queue.lock); 35807d92d5cSNicolas Dichtel if (skb_queue_len(&port->bc_queue) < MACVLAN_BC_QUEUE_LEN) { 359260916dfSHerbert Xu if (src) 360260916dfSHerbert Xu dev_hold(src->dev); 361e676f197SHerbert Xu __skb_queue_tail(&port->bc_queue, nskb); 362412ca155SHerbert Xu err = 0; 363412ca155SHerbert Xu } 364412ca155SHerbert Xu spin_unlock(&port->bc_queue.lock); 365412ca155SHerbert Xu 366412ca155SHerbert Xu if (err) 367e676f197SHerbert Xu goto free_nskb; 368412ca155SHerbert Xu 369412ca155SHerbert Xu schedule_work(&port->bc_work); 370412ca155SHerbert Xu return; 371412ca155SHerbert Xu 372e676f197SHerbert Xu free_nskb: 373e676f197SHerbert Xu kfree_skb(nskb); 374412ca155SHerbert Xu err: 375412ca155SHerbert Xu atomic_long_inc(&skb->dev->rx_dropped); 376412ca155SHerbert Xu } 377412ca155SHerbert Xu 37879cf79abSMichael Braun static void macvlan_flush_sources(struct macvlan_port *port, 37979cf79abSMichael Braun struct macvlan_dev *vlan) 38079cf79abSMichael Braun { 38179cf79abSMichael Braun int i; 38279cf79abSMichael Braun 38379cf79abSMichael Braun for (i = 0; i < MACVLAN_HASH_SIZE; i++) { 38479cf79abSMichael Braun struct hlist_node *h, *n; 38579cf79abSMichael Braun 38679cf79abSMichael Braun hlist_for_each_safe(h, n, &port->vlan_source_hash[i]) { 38779cf79abSMichael Braun struct macvlan_source_entry *entry; 38879cf79abSMichael Braun 38979cf79abSMichael Braun entry = hlist_entry(h, struct macvlan_source_entry, 39079cf79abSMichael Braun hlist); 39179cf79abSMichael Braun if (entry->vlan == vlan) 39279cf79abSMichael Braun macvlan_hash_del_source(entry); 39379cf79abSMichael Braun } 39479cf79abSMichael Braun } 39579cf79abSMichael Braun vlan->macaddr_count = 0; 39679cf79abSMichael Braun } 39779cf79abSMichael Braun 39879cf79abSMichael Braun static void macvlan_forward_source_one(struct sk_buff *skb, 39979cf79abSMichael Braun struct macvlan_dev *vlan) 40079cf79abSMichael Braun { 40179cf79abSMichael Braun struct sk_buff *nskb; 40279cf79abSMichael Braun struct net_device *dev; 40379cf79abSMichael Braun int len; 40479cf79abSMichael Braun int ret; 40579cf79abSMichael Braun 40679cf79abSMichael Braun dev = vlan->dev; 40779cf79abSMichael Braun if (unlikely(!(dev->flags & IFF_UP))) 40879cf79abSMichael Braun return; 40979cf79abSMichael Braun 41079cf79abSMichael Braun nskb = skb_clone(skb, GFP_ATOMIC); 41179cf79abSMichael Braun if (!nskb) 41279cf79abSMichael Braun return; 41379cf79abSMichael Braun 41479cf79abSMichael Braun len = nskb->len + ETH_HLEN; 41579cf79abSMichael Braun nskb->dev = dev; 416*c8c41ea1SAlexander Duyck 417*c8c41ea1SAlexander Duyck if (ether_addr_equal_64bits(eth_hdr(skb)->h_dest, dev->dev_addr)) 41879cf79abSMichael Braun nskb->pkt_type = PACKET_HOST; 41979cf79abSMichael Braun 42079cf79abSMichael Braun ret = netif_rx(nskb); 4214c979935Sjbaron@akamai.com macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, false); 42279cf79abSMichael Braun } 42379cf79abSMichael Braun 42479cf79abSMichael Braun static void macvlan_forward_source(struct sk_buff *skb, 42579cf79abSMichael Braun struct macvlan_port *port, 42679cf79abSMichael Braun const unsigned char *addr) 42779cf79abSMichael Braun { 42879cf79abSMichael Braun struct macvlan_source_entry *entry; 42979cf79abSMichael Braun u32 idx = macvlan_eth_hash(addr); 43079cf79abSMichael Braun struct hlist_head *h = &port->vlan_source_hash[idx]; 43179cf79abSMichael Braun 43279cf79abSMichael Braun hlist_for_each_entry_rcu(entry, h, hlist) { 43379cf79abSMichael Braun if (ether_addr_equal_64bits(entry->addr, addr)) 43479cf79abSMichael Braun macvlan_forward_source_one(skb, entry->vlan); 43579cf79abSMichael Braun } 43679cf79abSMichael Braun } 43779cf79abSMichael Braun 438b863ceb7SPatrick McHardy /* called under rcu_read_lock() from netif_receive_skb */ 4398a4eb573SJiri Pirko static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb) 440b863ceb7SPatrick McHardy { 441ab95bfe0SJiri Pirko struct macvlan_port *port; 4428a4eb573SJiri Pirko struct sk_buff *skb = *pskb; 443b863ceb7SPatrick McHardy const struct ethhdr *eth = eth_hdr(skb); 444b863ceb7SPatrick McHardy const struct macvlan_dev *vlan; 445618e1b74SArnd Bergmann const struct macvlan_dev *src; 446b863ceb7SPatrick McHardy struct net_device *dev; 447ba01877fSSridhar Samudrala unsigned int len = 0; 448d1dd9119Sjbaron@akamai.com int ret; 449d1dd9119Sjbaron@akamai.com rx_handler_result_t handle_res; 450b863ceb7SPatrick McHardy 451a35e2c1bSJiri Pirko port = macvlan_port_get_rcu(skb->dev); 452b863ceb7SPatrick McHardy if (is_multicast_ether_addr(eth->h_dest)) { 4539c127a01SHerbert Xu unsigned int hash; 4549c127a01SHerbert Xu 45519bcf9f2SEric W. Biederman skb = ip_check_defrag(dev_net(skb->dev), skb, IP_DEFRAG_MACVLAN); 456bc416d97SEric Dumazet if (!skb) 457bc416d97SEric Dumazet return RX_HANDLER_CONSUMED; 458e639b8d8SSabrina Dubroca *pskb = skb; 4594ec7ac12SEric Dumazet eth = eth_hdr(skb); 46079cf79abSMichael Braun macvlan_forward_source(skb, port, eth->h_source); 461618e1b74SArnd Bergmann src = macvlan_hash_lookup(port, eth->h_source); 462412ca155SHerbert Xu if (src && src->mode != MACVLAN_MODE_VEPA && 463412ca155SHerbert Xu src->mode != MACVLAN_MODE_BRIDGE) { 464729e72a1Sstephen hemminger /* forward to original port. */ 465729e72a1Sstephen hemminger vlan = src; 466412ca155SHerbert Xu ret = macvlan_broadcast_one(skb, vlan, eth, 0) ?: 467412ca155SHerbert Xu netif_rx(skb); 468d1dd9119Sjbaron@akamai.com handle_res = RX_HANDLER_CONSUMED; 469729e72a1Sstephen hemminger goto out; 470729e72a1Sstephen hemminger } 471729e72a1Sstephen hemminger 4729c127a01SHerbert Xu hash = mc_hash(NULL, eth->h_dest); 4739c127a01SHerbert Xu if (test_bit(hash, port->mc_filter)) 474260916dfSHerbert Xu macvlan_broadcast_enqueue(port, src, skb); 475412ca155SHerbert Xu 4768a4eb573SJiri Pirko return RX_HANDLER_PASS; 477b863ceb7SPatrick McHardy } 478b863ceb7SPatrick McHardy 47979cf79abSMichael Braun macvlan_forward_source(skb, port, eth->h_source); 48043c2d578SVlad Yasevich if (macvlan_passthru(port)) 481233c7df0SJiri Pirko vlan = list_first_or_null_rcu(&port->vlans, 482233c7df0SJiri Pirko struct macvlan_dev, list); 483eb06acdcSSridhar Samudrala else 484b863ceb7SPatrick McHardy vlan = macvlan_hash_lookup(port, eth->h_dest); 485dd6b9c2cSAlexander Duyck if (!vlan || vlan->mode == MACVLAN_MODE_SOURCE) 4868a4eb573SJiri Pirko return RX_HANDLER_PASS; 487b863ceb7SPatrick McHardy 488b863ceb7SPatrick McHardy dev = vlan->dev; 489b863ceb7SPatrick McHardy if (unlikely(!(dev->flags & IFF_UP))) { 490b863ceb7SPatrick McHardy kfree_skb(skb); 4918a4eb573SJiri Pirko return RX_HANDLER_CONSUMED; 492b863ceb7SPatrick McHardy } 493a1e514c5SArnd Bergmann len = skb->len + ETH_HLEN; 494b863ceb7SPatrick McHardy skb = skb_share_check(skb, GFP_ATOMIC); 495d1dd9119Sjbaron@akamai.com if (!skb) { 496d1dd9119Sjbaron@akamai.com ret = NET_RX_DROP; 497d1dd9119Sjbaron@akamai.com handle_res = RX_HANDLER_CONSUMED; 498ba01877fSSridhar Samudrala goto out; 499d1dd9119Sjbaron@akamai.com } 500b863ceb7SPatrick McHardy 501e639b8d8SSabrina Dubroca *pskb = skb; 502b863ceb7SPatrick McHardy skb->dev = dev; 503b863ceb7SPatrick McHardy skb->pkt_type = PACKET_HOST; 504b863ceb7SPatrick McHardy 505d1dd9119Sjbaron@akamai.com ret = NET_RX_SUCCESS; 506d1dd9119Sjbaron@akamai.com handle_res = RX_HANDLER_ANOTHER; 507ba01877fSSridhar Samudrala out: 5084c979935Sjbaron@akamai.com macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, false); 509d1dd9119Sjbaron@akamai.com return handle_res; 510b863ceb7SPatrick McHardy } 511b863ceb7SPatrick McHardy 512618e1b74SArnd Bergmann static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev) 513618e1b74SArnd Bergmann { 514618e1b74SArnd Bergmann const struct macvlan_dev *vlan = netdev_priv(dev); 515618e1b74SArnd Bergmann const struct macvlan_port *port = vlan->port; 516618e1b74SArnd Bergmann const struct macvlan_dev *dest; 517618e1b74SArnd Bergmann 518618e1b74SArnd Bergmann if (vlan->mode == MACVLAN_MODE_BRIDGE) { 519618e1b74SArnd Bergmann const struct ethhdr *eth = (void *)skb->data; 520618e1b74SArnd Bergmann 521618e1b74SArnd Bergmann /* send to other bridge ports directly */ 522618e1b74SArnd Bergmann if (is_multicast_ether_addr(eth->h_dest)) { 523618e1b74SArnd Bergmann macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE); 524618e1b74SArnd Bergmann goto xmit_world; 525618e1b74SArnd Bergmann } 526618e1b74SArnd Bergmann 527618e1b74SArnd Bergmann dest = macvlan_hash_lookup(port, eth->h_dest); 528618e1b74SArnd Bergmann if (dest && dest->mode == MACVLAN_MODE_BRIDGE) { 529a37dd333SDavid Ward /* send to lowerdev first for its network taps */ 530cb2d0f3eSDavid Ward dev_forward_skb(vlan->lowerdev, skb); 531618e1b74SArnd Bergmann 532618e1b74SArnd Bergmann return NET_XMIT_SUCCESS; 533618e1b74SArnd Bergmann } 534618e1b74SArnd Bergmann } 535618e1b74SArnd Bergmann 536618e1b74SArnd Bergmann xmit_world: 53759b9997bSDavid S. Miller skb->dev = vlan->lowerdev; 538618e1b74SArnd Bergmann return dev_queue_xmit(skb); 539618e1b74SArnd Bergmann } 540618e1b74SArnd Bergmann 541688cea83Sdingtianhong static inline netdev_tx_t macvlan_netpoll_send_skb(struct macvlan_dev *vlan, struct sk_buff *skb) 542688cea83Sdingtianhong { 543688cea83Sdingtianhong #ifdef CONFIG_NET_POLL_CONTROLLER 544688cea83Sdingtianhong if (vlan->netpoll) 545688cea83Sdingtianhong netpoll_send_skb(vlan->netpoll, skb); 546688cea83Sdingtianhong #else 547688cea83Sdingtianhong BUG(); 548688cea83Sdingtianhong #endif 549688cea83Sdingtianhong return NETDEV_TX_OK; 550688cea83Sdingtianhong } 551688cea83Sdingtianhong 5520db901bdSstephen hemminger static netdev_tx_t macvlan_start_xmit(struct sk_buff *skb, 553424efe9cSStephen Hemminger struct net_device *dev) 554b863ceb7SPatrick McHardy { 555b863ceb7SPatrick McHardy unsigned int len = skb->len; 556b863ceb7SPatrick McHardy int ret; 557688cea83Sdingtianhong struct macvlan_dev *vlan = netdev_priv(dev); 558688cea83Sdingtianhong 559688cea83Sdingtianhong if (unlikely(netpoll_tx_running(dev))) 560688cea83Sdingtianhong return macvlan_netpoll_send_skb(vlan, skb); 561b863ceb7SPatrick McHardy 562a6cc0cfaSJohn Fastabend if (vlan->fwd_priv) { 563a6cc0cfaSJohn Fastabend skb->dev = vlan->lowerdev; 564f663dd9aSJason Wang ret = dev_queue_xmit_accel(skb, vlan->fwd_priv); 565a6cc0cfaSJohn Fastabend } else { 566618e1b74SArnd Bergmann ret = macvlan_queue_xmit(skb, dev); 567a6cc0cfaSJohn Fastabend } 568a6cc0cfaSJohn Fastabend 5692d6c9ffcSEric Dumazet if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) { 570cdf3e274SLi RongQing struct vlan_pcpu_stats *pcpu_stats; 5712c114553SEric Dumazet 5728ffab51bSEric Dumazet pcpu_stats = this_cpu_ptr(vlan->pcpu_stats); 5738ffab51bSEric Dumazet u64_stats_update_begin(&pcpu_stats->syncp); 5748ffab51bSEric Dumazet pcpu_stats->tx_packets++; 5758ffab51bSEric Dumazet pcpu_stats->tx_bytes += len; 5768ffab51bSEric Dumazet u64_stats_update_end(&pcpu_stats->syncp); 5778ffab51bSEric Dumazet } else { 5788ffab51bSEric Dumazet this_cpu_inc(vlan->pcpu_stats->tx_dropped); 5798ffab51bSEric Dumazet } 580cbbef5e1SPatrick McHardy return ret; 581b863ceb7SPatrick McHardy } 582b863ceb7SPatrick McHardy 583b863ceb7SPatrick McHardy static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev, 5843b04dddeSStephen Hemminger unsigned short type, const void *daddr, 5853b04dddeSStephen Hemminger const void *saddr, unsigned len) 586b863ceb7SPatrick McHardy { 587b863ceb7SPatrick McHardy const struct macvlan_dev *vlan = netdev_priv(dev); 588b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 589b863ceb7SPatrick McHardy 5900c4e8581SStephen Hemminger return dev_hard_header(skb, lowerdev, type, daddr, 591b863ceb7SPatrick McHardy saddr ? : dev->dev_addr, len); 592b863ceb7SPatrick McHardy } 593b863ceb7SPatrick McHardy 5943b04dddeSStephen Hemminger static const struct header_ops macvlan_hard_header_ops = { 5953b04dddeSStephen Hemminger .create = macvlan_hard_header, 5963b04dddeSStephen Hemminger .parse = eth_header_parse, 5973b04dddeSStephen Hemminger .cache = eth_header_cache, 5983b04dddeSStephen Hemminger .cache_update = eth_header_cache_update, 5993b04dddeSStephen Hemminger }; 6003b04dddeSStephen Hemminger 601b13ba1b8SJason Wang static struct rtnl_link_ops macvlan_link_ops; 602b13ba1b8SJason Wang 603b863ceb7SPatrick McHardy static int macvlan_open(struct net_device *dev) 604b863ceb7SPatrick McHardy { 605b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 606b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 607b863ceb7SPatrick McHardy int err; 608b863ceb7SPatrick McHardy 60943c2d578SVlad Yasevich if (macvlan_passthru(vlan->port)) { 61078738141SMichael S. Tsirkin if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) { 61178738141SMichael S. Tsirkin err = dev_set_promiscuity(lowerdev, 1); 61278738141SMichael S. Tsirkin if (err < 0) 61378738141SMichael S. Tsirkin goto out; 61478738141SMichael S. Tsirkin } 615eb06acdcSSridhar Samudrala goto hash_add; 616eb06acdcSSridhar Samudrala } 617eb06acdcSSridhar Samudrala 618b13ba1b8SJason Wang if (lowerdev->features & NETIF_F_HW_L2FW_DOFFLOAD && 619b13ba1b8SJason Wang dev->rtnl_link_ops == &macvlan_link_ops) { 620a6cc0cfaSJohn Fastabend vlan->fwd_priv = 621a6cc0cfaSJohn Fastabend lowerdev->netdev_ops->ndo_dfwd_add_station(lowerdev, dev); 622a6cc0cfaSJohn Fastabend 623a6cc0cfaSJohn Fastabend /* If we get a NULL pointer back, or if we get an error 624a6cc0cfaSJohn Fastabend * then we should just fall through to the non accelerated path 625a6cc0cfaSJohn Fastabend */ 626a6cc0cfaSJohn Fastabend if (IS_ERR_OR_NULL(vlan->fwd_priv)) { 627a6cc0cfaSJohn Fastabend vlan->fwd_priv = NULL; 628f663dd9aSJason Wang } else 629a6cc0cfaSJohn Fastabend return 0; 630a6cc0cfaSJohn Fastabend } 631a6cc0cfaSJohn Fastabend 632f9ac30f0SEric Biederman err = -EBUSY; 633f9ac30f0SEric Biederman if (macvlan_addr_busy(vlan->port, dev->dev_addr)) 634f9ac30f0SEric Biederman goto out; 635f9ac30f0SEric Biederman 636a748ee24SJiri Pirko err = dev_uc_add(lowerdev, dev->dev_addr); 637b863ceb7SPatrick McHardy if (err < 0) 638b89fb7daSWang Chen goto out; 639b89fb7daSWang Chen if (dev->flags & IFF_ALLMULTI) { 640b89fb7daSWang Chen err = dev_set_allmulti(lowerdev, 1); 641b89fb7daSWang Chen if (err < 0) 642b89fb7daSWang Chen goto del_unicast; 643b89fb7daSWang Chen } 644eb06acdcSSridhar Samudrala 645efdbd2b3SVlad Yasevich if (dev->flags & IFF_PROMISC) { 646efdbd2b3SVlad Yasevich err = dev_set_promiscuity(lowerdev, 1); 647efdbd2b3SVlad Yasevich if (err < 0) 648efdbd2b3SVlad Yasevich goto clear_multi; 649efdbd2b3SVlad Yasevich } 650efdbd2b3SVlad Yasevich 651eb06acdcSSridhar Samudrala hash_add: 652f9ac30f0SEric Biederman macvlan_hash_add(vlan); 653b863ceb7SPatrick McHardy return 0; 654b89fb7daSWang Chen 655efdbd2b3SVlad Yasevich clear_multi: 656c3891fa2SGao Feng if (dev->flags & IFF_ALLMULTI) 657efdbd2b3SVlad Yasevich dev_set_allmulti(lowerdev, -1); 658b89fb7daSWang Chen del_unicast: 659a748ee24SJiri Pirko dev_uc_del(lowerdev, dev->dev_addr); 660b89fb7daSWang Chen out: 661a6cc0cfaSJohn Fastabend if (vlan->fwd_priv) { 662a6cc0cfaSJohn Fastabend lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev, 663a6cc0cfaSJohn Fastabend vlan->fwd_priv); 664a6cc0cfaSJohn Fastabend vlan->fwd_priv = NULL; 665a6cc0cfaSJohn Fastabend } 666b89fb7daSWang Chen return err; 667b863ceb7SPatrick McHardy } 668b863ceb7SPatrick McHardy 669b863ceb7SPatrick McHardy static int macvlan_stop(struct net_device *dev) 670b863ceb7SPatrick McHardy { 671b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 672b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 673b863ceb7SPatrick McHardy 674a6cc0cfaSJohn Fastabend if (vlan->fwd_priv) { 675a6cc0cfaSJohn Fastabend lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev, 676a6cc0cfaSJohn Fastabend vlan->fwd_priv); 677a6cc0cfaSJohn Fastabend vlan->fwd_priv = NULL; 678a6cc0cfaSJohn Fastabend return 0; 679a6cc0cfaSJohn Fastabend } 680a6cc0cfaSJohn Fastabend 681df8ef8f3SJohn Fastabend dev_uc_unsync(lowerdev, dev); 682df8ef8f3SJohn Fastabend dev_mc_unsync(lowerdev, dev); 683df8ef8f3SJohn Fastabend 68443c2d578SVlad Yasevich if (macvlan_passthru(vlan->port)) { 685df8ef8f3SJohn Fastabend if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) 686eb06acdcSSridhar Samudrala dev_set_promiscuity(lowerdev, -1); 687eb06acdcSSridhar Samudrala goto hash_del; 688eb06acdcSSridhar Samudrala } 689eb06acdcSSridhar Samudrala 690b863ceb7SPatrick McHardy if (dev->flags & IFF_ALLMULTI) 691b863ceb7SPatrick McHardy dev_set_allmulti(lowerdev, -1); 692b863ceb7SPatrick McHardy 693efdbd2b3SVlad Yasevich if (dev->flags & IFF_PROMISC) 694efdbd2b3SVlad Yasevich dev_set_promiscuity(lowerdev, -1); 695efdbd2b3SVlad Yasevich 696a748ee24SJiri Pirko dev_uc_del(lowerdev, dev->dev_addr); 697b863ceb7SPatrick McHardy 698eb06acdcSSridhar Samudrala hash_del: 699449f4544SEric Dumazet macvlan_hash_del(vlan, !dev->dismantle); 700b863ceb7SPatrick McHardy return 0; 701b863ceb7SPatrick McHardy } 702b863ceb7SPatrick McHardy 703e289fd28Sdingtianhong static int macvlan_sync_address(struct net_device *dev, unsigned char *addr) 704ad5d20a6SPatrick McHardy { 705ad5d20a6SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 706ad5d20a6SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 70718c8c54dSVlad Yasevich struct macvlan_port *port = vlan->port; 708ad5d20a6SPatrick McHardy int err; 709ad5d20a6SPatrick McHardy 710f9ac30f0SEric Biederman if (!(dev->flags & IFF_UP)) { 711f9ac30f0SEric Biederman /* Just copy in the new address */ 712e289fd28Sdingtianhong ether_addr_copy(dev->dev_addr, addr); 713f9ac30f0SEric Biederman } else { 714f9ac30f0SEric Biederman /* Rehash and update the device filters */ 715e289fd28Sdingtianhong if (macvlan_addr_busy(vlan->port, addr)) 716f9ac30f0SEric Biederman return -EBUSY; 717ad5d20a6SPatrick McHardy 71818c8c54dSVlad Yasevich if (!macvlan_passthru(port)) { 719e289fd28Sdingtianhong err = dev_uc_add(lowerdev, addr); 720ccffad25SJiri Pirko if (err) 721ad5d20a6SPatrick McHardy return err; 722f9ac30f0SEric Biederman 723a748ee24SJiri Pirko dev_uc_del(lowerdev, dev->dev_addr); 724e289fd28Sdingtianhong } 725ad5d20a6SPatrick McHardy 726e289fd28Sdingtianhong macvlan_hash_change_addr(vlan, addr); 727f9ac30f0SEric Biederman } 72818c8c54dSVlad Yasevich if (macvlan_passthru(port) && !macvlan_addr_change(port)) { 72918c8c54dSVlad Yasevich /* Since addr_change isn't set, we are here due to lower 73018c8c54dSVlad Yasevich * device change. Save the lower-dev address so we can 73118c8c54dSVlad Yasevich * restore it later. 73218c8c54dSVlad Yasevich */ 73318c8c54dSVlad Yasevich ether_addr_copy(vlan->port->perm_addr, 73418c8c54dSVlad Yasevich lowerdev->dev_addr); 73518c8c54dSVlad Yasevich } 73618c8c54dSVlad Yasevich macvlan_clear_addr_change(port); 737ad5d20a6SPatrick McHardy return 0; 738ad5d20a6SPatrick McHardy } 739ad5d20a6SPatrick McHardy 740e289fd28Sdingtianhong static int macvlan_set_mac_address(struct net_device *dev, void *p) 741e289fd28Sdingtianhong { 742e289fd28Sdingtianhong struct macvlan_dev *vlan = netdev_priv(dev); 743e289fd28Sdingtianhong struct sockaddr *addr = p; 744e289fd28Sdingtianhong 745e289fd28Sdingtianhong if (!is_valid_ether_addr(addr->sa_data)) 746e289fd28Sdingtianhong return -EADDRNOTAVAIL; 747e289fd28Sdingtianhong 748e26f43faSVlad Yasevich /* If the addresses are the same, this is a no-op */ 749e26f43faSVlad Yasevich if (ether_addr_equal(dev->dev_addr, addr->sa_data)) 750e26f43faSVlad Yasevich return 0; 751e26f43faSVlad Yasevich 752e289fd28Sdingtianhong if (vlan->mode == MACVLAN_MODE_PASSTHRU) { 75318c8c54dSVlad Yasevich macvlan_set_addr_change(vlan->port); 754a88e2676SZhang Shengju return dev_set_mac_address(vlan->lowerdev, addr); 755e289fd28Sdingtianhong } 756e289fd28Sdingtianhong 757e289fd28Sdingtianhong return macvlan_sync_address(dev, addr->sa_data); 758e289fd28Sdingtianhong } 759e289fd28Sdingtianhong 760b863ceb7SPatrick McHardy static void macvlan_change_rx_flags(struct net_device *dev, int change) 761b863ceb7SPatrick McHardy { 762b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 763b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 764b863ceb7SPatrick McHardy 765bbeb0eadSPeter Christensen if (dev->flags & IFF_UP) { 766b863ceb7SPatrick McHardy if (change & IFF_ALLMULTI) 767b863ceb7SPatrick McHardy dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1); 768efdbd2b3SVlad Yasevich if (change & IFF_PROMISC) 769efdbd2b3SVlad Yasevich dev_set_promiscuity(lowerdev, 770efdbd2b3SVlad Yasevich dev->flags & IFF_PROMISC ? 1 : -1); 771efdbd2b3SVlad Yasevich 772b863ceb7SPatrick McHardy } 773bbeb0eadSPeter Christensen } 774b863ceb7SPatrick McHardy 7759c127a01SHerbert Xu static void macvlan_compute_filter(unsigned long *mc_filter, 7769c127a01SHerbert Xu struct net_device *dev, 7779c127a01SHerbert Xu struct macvlan_dev *vlan) 778b863ceb7SPatrick McHardy { 779cd431e73SEric Dumazet if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) { 7809c127a01SHerbert Xu bitmap_fill(mc_filter, MACVLAN_MC_FILTER_SZ); 781cd431e73SEric Dumazet } else { 782cd431e73SEric Dumazet struct netdev_hw_addr *ha; 783cd431e73SEric Dumazet DECLARE_BITMAP(filter, MACVLAN_MC_FILTER_SZ); 784cd431e73SEric Dumazet 785cd431e73SEric Dumazet bitmap_zero(filter, MACVLAN_MC_FILTER_SZ); 786cd431e73SEric Dumazet netdev_for_each_mc_addr(ha, dev) { 7873807ff58SEric Dumazet __set_bit(mc_hash(vlan, ha->addr), filter); 788cd431e73SEric Dumazet } 789d5270430SEric Dumazet 7903807ff58SEric Dumazet __set_bit(mc_hash(vlan, dev->broadcast), filter); 791d5270430SEric Dumazet 7929c127a01SHerbert Xu bitmap_copy(mc_filter, filter, MACVLAN_MC_FILTER_SZ); 793cd431e73SEric Dumazet } 7949c127a01SHerbert Xu } 7959c127a01SHerbert Xu 7969c127a01SHerbert Xu static void macvlan_set_mac_lists(struct net_device *dev) 7979c127a01SHerbert Xu { 7989c127a01SHerbert Xu struct macvlan_dev *vlan = netdev_priv(dev); 7999c127a01SHerbert Xu 8009c127a01SHerbert Xu macvlan_compute_filter(vlan->mc_filter, dev, vlan); 8019c127a01SHerbert Xu 802df8ef8f3SJohn Fastabend dev_uc_sync(vlan->lowerdev, dev); 803b863ceb7SPatrick McHardy dev_mc_sync(vlan->lowerdev, dev); 8049c127a01SHerbert Xu 8059c127a01SHerbert Xu /* This is slightly inaccurate as we're including the subscription 8069c127a01SHerbert Xu * list of vlan->lowerdev too. 8079c127a01SHerbert Xu * 8089c127a01SHerbert Xu * Bug alert: This only works if everyone has the same broadcast 8099c127a01SHerbert Xu * address as lowerdev. As soon as someone changes theirs this 8109c127a01SHerbert Xu * will break. 8119c127a01SHerbert Xu * 8129c127a01SHerbert Xu * However, this is already broken as when you change your broadcast 8139c127a01SHerbert Xu * address we don't get called. 8149c127a01SHerbert Xu * 8159c127a01SHerbert Xu * The solution is to maintain a list of broadcast addresses like 8169c127a01SHerbert Xu * we do for uc/mc, if you care. 8179c127a01SHerbert Xu */ 8189c127a01SHerbert Xu macvlan_compute_filter(vlan->port->mc_filter, vlan->lowerdev, NULL); 819b863ceb7SPatrick McHardy } 820b863ceb7SPatrick McHardy 821b863ceb7SPatrick McHardy static int macvlan_change_mtu(struct net_device *dev, int new_mtu) 822b863ceb7SPatrick McHardy { 823b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 824b863ceb7SPatrick McHardy 82591572088SJarod Wilson if (vlan->lowerdev->mtu < new_mtu) 826b863ceb7SPatrick McHardy return -EINVAL; 827b863ceb7SPatrick McHardy dev->mtu = new_mtu; 828b863ceb7SPatrick McHardy return 0; 829b863ceb7SPatrick McHardy } 830b863ceb7SPatrick McHardy 831b863ceb7SPatrick McHardy /* 832b863ceb7SPatrick McHardy * macvlan network devices have devices nesting below it and are a special 833b863ceb7SPatrick McHardy * "super class" of normal network devices; split their locks off into a 834b863ceb7SPatrick McHardy * separate class since they always nest. 835b863ceb7SPatrick McHardy */ 836cf508b12SDavid S. Miller static struct lock_class_key macvlan_netdev_addr_lock_key; 837b863ceb7SPatrick McHardy 83870957eaeSVlad Yasevich #define ALWAYS_ON_OFFLOADS \ 83970957eaeSVlad Yasevich (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE | \ 840f21e5077SDimitris Michailidis NETIF_F_GSO_ROBUST | NETIF_F_GSO_ENCAP_ALL) 8418b4703e9SVlad Yasevich 84270957eaeSVlad Yasevich #define ALWAYS_ON_FEATURES (ALWAYS_ON_OFFLOADS | NETIF_F_LLTX) 84370957eaeSVlad Yasevich 844b863ceb7SPatrick McHardy #define MACVLAN_FEATURES \ 845a188222bSTom Herbert (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \ 846fb652fdfSDavid S. Miller NETIF_F_GSO | NETIF_F_TSO | NETIF_F_LRO | \ 8478d13e670SJohn Fastabend NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \ 84828d2b136SPatrick McHardy NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER) 849b863ceb7SPatrick McHardy 850b863ceb7SPatrick McHardy #define MACVLAN_STATE_MASK \ 851b863ceb7SPatrick McHardy ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT)) 852b863ceb7SPatrick McHardy 853c674ac30SVlad Yasevich static int macvlan_get_nest_level(struct net_device *dev) 854c674ac30SVlad Yasevich { 855c674ac30SVlad Yasevich return ((struct macvlan_dev *)netdev_priv(dev))->nest_level; 856c674ac30SVlad Yasevich } 857c674ac30SVlad Yasevich 858c773e847SDavid S. Miller static void macvlan_set_lockdep_class(struct net_device *dev) 859c773e847SDavid S. Miller { 86024ffd752SEric Dumazet netdev_lockdep_set_classes(dev); 861c674ac30SVlad Yasevich lockdep_set_class_and_subclass(&dev->addr_list_lock, 862c674ac30SVlad Yasevich &macvlan_netdev_addr_lock_key, 863c674ac30SVlad Yasevich macvlan_get_nest_level(dev)); 864c773e847SDavid S. Miller } 865c773e847SDavid S. Miller 866b863ceb7SPatrick McHardy static int macvlan_init(struct net_device *dev) 867b863ceb7SPatrick McHardy { 868b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 869b863ceb7SPatrick McHardy const struct net_device *lowerdev = vlan->lowerdev; 87030837960SFrancesco Ruggeri struct macvlan_port *port = vlan->port; 871b863ceb7SPatrick McHardy 872b863ceb7SPatrick McHardy dev->state = (dev->state & ~MACVLAN_STATE_MASK) | 873b863ceb7SPatrick McHardy (lowerdev->state & MACVLAN_STATE_MASK); 874b863ceb7SPatrick McHardy dev->features = lowerdev->features & MACVLAN_FEATURES; 8758b4703e9SVlad Yasevich dev->features |= ALWAYS_ON_FEATURES; 87662dbe830SMichal Kubeček dev->hw_features |= NETIF_F_LRO; 877081e83a7SVlad Yasevich dev->vlan_features = lowerdev->vlan_features & MACVLAN_FEATURES; 87870957eaeSVlad Yasevich dev->vlan_features |= ALWAYS_ON_OFFLOADS; 879f21e5077SDimitris Michailidis dev->hw_enc_features |= dev->features; 8808c2acc53SPatrick McHardy dev->gso_max_size = lowerdev->gso_max_size; 881f6773c5eSEric Dumazet dev->gso_max_segs = lowerdev->gso_max_segs; 882ef5c8996Ssg.tweak@gmail.com dev->hard_header_len = lowerdev->hard_header_len; 883b863ceb7SPatrick McHardy 884c773e847SDavid S. Miller macvlan_set_lockdep_class(dev); 885c773e847SDavid S. Miller 8861c213bd2SWANG Cong vlan->pcpu_stats = netdev_alloc_pcpu_stats(struct vlan_pcpu_stats); 8878ffab51bSEric Dumazet if (!vlan->pcpu_stats) 888fccaf710SEric Dumazet return -ENOMEM; 889fccaf710SEric Dumazet 89030837960SFrancesco Ruggeri port->count += 1; 89130837960SFrancesco Ruggeri 892b863ceb7SPatrick McHardy return 0; 893b863ceb7SPatrick McHardy } 894b863ceb7SPatrick McHardy 895fccaf710SEric Dumazet static void macvlan_uninit(struct net_device *dev) 896fccaf710SEric Dumazet { 897fccaf710SEric Dumazet struct macvlan_dev *vlan = netdev_priv(dev); 898d5cd9244SEric W. Biederman struct macvlan_port *port = vlan->port; 899fccaf710SEric Dumazet 9008ffab51bSEric Dumazet free_percpu(vlan->pcpu_stats); 901d5cd9244SEric W. Biederman 90279cf79abSMichael Braun macvlan_flush_sources(port, vlan); 9035e3c516bSDavid S. Miller port->count -= 1; 9045e3c516bSDavid S. Miller if (!port->count) 905d5cd9244SEric W. Biederman macvlan_port_destroy(port->dev); 906fccaf710SEric Dumazet } 907fccaf710SEric Dumazet 908bc1f4470Sstephen hemminger static void macvlan_dev_get_stats64(struct net_device *dev, 90928172739SEric Dumazet struct rtnl_link_stats64 *stats) 910fccaf710SEric Dumazet { 911fccaf710SEric Dumazet struct macvlan_dev *vlan = netdev_priv(dev); 912fccaf710SEric Dumazet 9138ffab51bSEric Dumazet if (vlan->pcpu_stats) { 914cdf3e274SLi RongQing struct vlan_pcpu_stats *p; 9158ffab51bSEric Dumazet u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes; 9168ffab51bSEric Dumazet u32 rx_errors = 0, tx_dropped = 0; 917bc66154eSEric Dumazet unsigned int start; 918fccaf710SEric Dumazet int i; 919fccaf710SEric Dumazet 920fccaf710SEric Dumazet for_each_possible_cpu(i) { 9218ffab51bSEric Dumazet p = per_cpu_ptr(vlan->pcpu_stats, i); 922bc66154eSEric Dumazet do { 92357a7744eSEric W. Biederman start = u64_stats_fetch_begin_irq(&p->syncp); 924bc66154eSEric Dumazet rx_packets = p->rx_packets; 925bc66154eSEric Dumazet rx_bytes = p->rx_bytes; 926bc66154eSEric Dumazet rx_multicast = p->rx_multicast; 9278ffab51bSEric Dumazet tx_packets = p->tx_packets; 9288ffab51bSEric Dumazet tx_bytes = p->tx_bytes; 92957a7744eSEric W. Biederman } while (u64_stats_fetch_retry_irq(&p->syncp, start)); 9308ffab51bSEric Dumazet 9318ffab51bSEric Dumazet stats->rx_packets += rx_packets; 9328ffab51bSEric Dumazet stats->rx_bytes += rx_bytes; 9338ffab51bSEric Dumazet stats->multicast += rx_multicast; 9348ffab51bSEric Dumazet stats->tx_packets += tx_packets; 9358ffab51bSEric Dumazet stats->tx_bytes += tx_bytes; 9368ffab51bSEric Dumazet /* rx_errors & tx_dropped are u32, updated 9378ffab51bSEric Dumazet * without syncp protection. 9388ffab51bSEric Dumazet */ 9398ffab51bSEric Dumazet rx_errors += p->rx_errors; 9408ffab51bSEric Dumazet tx_dropped += p->tx_dropped; 941fccaf710SEric Dumazet } 9428ffab51bSEric Dumazet stats->rx_errors = rx_errors; 9438ffab51bSEric Dumazet stats->rx_dropped = rx_errors; 9448ffab51bSEric Dumazet stats->tx_dropped = tx_dropped; 945fccaf710SEric Dumazet } 946fccaf710SEric Dumazet } 947fccaf710SEric Dumazet 9488e586137SJiri Pirko static int macvlan_vlan_rx_add_vid(struct net_device *dev, 94980d5c368SPatrick McHardy __be16 proto, u16 vid) 9508d13e670SJohn Fastabend { 9518d13e670SJohn Fastabend struct macvlan_dev *vlan = netdev_priv(dev); 9528d13e670SJohn Fastabend struct net_device *lowerdev = vlan->lowerdev; 9538d13e670SJohn Fastabend 95480d5c368SPatrick McHardy return vlan_vid_add(lowerdev, proto, vid); 9558d13e670SJohn Fastabend } 9568d13e670SJohn Fastabend 9578e586137SJiri Pirko static int macvlan_vlan_rx_kill_vid(struct net_device *dev, 95880d5c368SPatrick McHardy __be16 proto, u16 vid) 9598d13e670SJohn Fastabend { 9608d13e670SJohn Fastabend struct macvlan_dev *vlan = netdev_priv(dev); 9618d13e670SJohn Fastabend struct net_device *lowerdev = vlan->lowerdev; 9628d13e670SJohn Fastabend 96380d5c368SPatrick McHardy vlan_vid_del(lowerdev, proto, vid); 9648e586137SJiri Pirko return 0; 9658d13e670SJohn Fastabend } 9668d13e670SJohn Fastabend 967edc7d573Sstephen hemminger static int macvlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], 968df8ef8f3SJohn Fastabend struct net_device *dev, 969f6f6424bSJiri Pirko const unsigned char *addr, u16 vid, 970df8ef8f3SJohn Fastabend u16 flags) 971df8ef8f3SJohn Fastabend { 972df8ef8f3SJohn Fastabend struct macvlan_dev *vlan = netdev_priv(dev); 973df8ef8f3SJohn Fastabend int err = -EINVAL; 974df8ef8f3SJohn Fastabend 9758a50f11cSVlad Yasevich /* Support unicast filter only on passthru devices. 9768a50f11cSVlad Yasevich * Multicast filter should be allowed on all devices. 9778a50f11cSVlad Yasevich */ 97843c2d578SVlad Yasevich if (!macvlan_passthru(vlan->port) && is_unicast_ether_addr(addr)) 979df8ef8f3SJohn Fastabend return -EOPNOTSUPP; 980df8ef8f3SJohn Fastabend 981ab2cfbb2SThomas Richter if (flags & NLM_F_REPLACE) 982ab2cfbb2SThomas Richter return -EOPNOTSUPP; 983ab2cfbb2SThomas Richter 984df8ef8f3SJohn Fastabend if (is_unicast_ether_addr(addr)) 985df8ef8f3SJohn Fastabend err = dev_uc_add_excl(dev, addr); 986df8ef8f3SJohn Fastabend else if (is_multicast_ether_addr(addr)) 987df8ef8f3SJohn Fastabend err = dev_mc_add_excl(dev, addr); 988df8ef8f3SJohn Fastabend 989df8ef8f3SJohn Fastabend return err; 990df8ef8f3SJohn Fastabend } 991df8ef8f3SJohn Fastabend 9921690be63SVlad Yasevich static int macvlan_fdb_del(struct ndmsg *ndm, struct nlattr *tb[], 993df8ef8f3SJohn Fastabend struct net_device *dev, 994f6f6424bSJiri Pirko const unsigned char *addr, u16 vid) 995df8ef8f3SJohn Fastabend { 996df8ef8f3SJohn Fastabend struct macvlan_dev *vlan = netdev_priv(dev); 997df8ef8f3SJohn Fastabend int err = -EINVAL; 998df8ef8f3SJohn Fastabend 9998a50f11cSVlad Yasevich /* Support unicast filter only on passthru devices. 10008a50f11cSVlad Yasevich * Multicast filter should be allowed on all devices. 10018a50f11cSVlad Yasevich */ 100243c2d578SVlad Yasevich if (!macvlan_passthru(vlan->port) && is_unicast_ether_addr(addr)) 1003df8ef8f3SJohn Fastabend return -EOPNOTSUPP; 1004df8ef8f3SJohn Fastabend 1005df8ef8f3SJohn Fastabend if (is_unicast_ether_addr(addr)) 1006df8ef8f3SJohn Fastabend err = dev_uc_del(dev, addr); 1007df8ef8f3SJohn Fastabend else if (is_multicast_ether_addr(addr)) 1008df8ef8f3SJohn Fastabend err = dev_mc_del(dev, addr); 1009df8ef8f3SJohn Fastabend 1010df8ef8f3SJohn Fastabend return err; 1011df8ef8f3SJohn Fastabend } 1012df8ef8f3SJohn Fastabend 1013b863ceb7SPatrick McHardy static void macvlan_ethtool_get_drvinfo(struct net_device *dev, 1014b863ceb7SPatrick McHardy struct ethtool_drvinfo *drvinfo) 1015b863ceb7SPatrick McHardy { 10167826d43fSJiri Pirko strlcpy(drvinfo->driver, "macvlan", sizeof(drvinfo->driver)); 10177826d43fSJiri Pirko strlcpy(drvinfo->version, "0.1", sizeof(drvinfo->version)); 1018b863ceb7SPatrick McHardy } 1019b863ceb7SPatrick McHardy 102085f95819SDavid Decotigny static int macvlan_ethtool_get_link_ksettings(struct net_device *dev, 102185f95819SDavid Decotigny struct ethtool_link_ksettings *cmd) 10229edb8bb6SStephen Hemminger { 10239edb8bb6SStephen Hemminger const struct macvlan_dev *vlan = netdev_priv(dev); 10244bc71cb9SJiri Pirko 102585f95819SDavid Decotigny return __ethtool_get_link_ksettings(vlan->lowerdev, cmd); 10269edb8bb6SStephen Hemminger } 10279edb8bb6SStephen Hemminger 10282be5c767SVlad Yasevich static netdev_features_t macvlan_fix_features(struct net_device *dev, 10292be5c767SVlad Yasevich netdev_features_t features) 10302be5c767SVlad Yasevich { 10312be5c767SVlad Yasevich struct macvlan_dev *vlan = netdev_priv(dev); 103262dbe830SMichal Kubeček netdev_features_t lowerdev_features = vlan->lowerdev->features; 1033797f87f8SFlorian Westphal netdev_features_t mask; 10342be5c767SVlad Yasevich 1035797f87f8SFlorian Westphal features |= NETIF_F_ALL_FOR_ALL; 1036797f87f8SFlorian Westphal features &= (vlan->set_features | ~MACVLAN_FEATURES); 1037797f87f8SFlorian Westphal mask = features; 1038797f87f8SFlorian Westphal 103962dbe830SMichal Kubeček lowerdev_features &= (features | ~NETIF_F_LRO); 104062dbe830SMichal Kubeček features = netdev_increment_features(lowerdev_features, features, mask); 10418b4703e9SVlad Yasevich features |= ALWAYS_ON_FEATURES; 10420d0162e7SFrancesco Ruggeri features &= ~NETIF_F_NETNS_LOCAL; 1043797f87f8SFlorian Westphal 1044797f87f8SFlorian Westphal return features; 10452be5c767SVlad Yasevich } 10462be5c767SVlad Yasevich 1047688cea83Sdingtianhong #ifdef CONFIG_NET_POLL_CONTROLLER 1048688cea83Sdingtianhong static void macvlan_dev_poll_controller(struct net_device *dev) 1049688cea83Sdingtianhong { 1050688cea83Sdingtianhong return; 1051688cea83Sdingtianhong } 1052688cea83Sdingtianhong 1053688cea83Sdingtianhong static int macvlan_dev_netpoll_setup(struct net_device *dev, struct netpoll_info *npinfo) 1054688cea83Sdingtianhong { 1055688cea83Sdingtianhong struct macvlan_dev *vlan = netdev_priv(dev); 1056688cea83Sdingtianhong struct net_device *real_dev = vlan->lowerdev; 1057688cea83Sdingtianhong struct netpoll *netpoll; 1058688cea83Sdingtianhong int err = 0; 1059688cea83Sdingtianhong 1060688cea83Sdingtianhong netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL); 1061688cea83Sdingtianhong err = -ENOMEM; 1062688cea83Sdingtianhong if (!netpoll) 1063688cea83Sdingtianhong goto out; 1064688cea83Sdingtianhong 1065688cea83Sdingtianhong err = __netpoll_setup(netpoll, real_dev); 1066688cea83Sdingtianhong if (err) { 1067688cea83Sdingtianhong kfree(netpoll); 1068688cea83Sdingtianhong goto out; 1069688cea83Sdingtianhong } 1070688cea83Sdingtianhong 1071688cea83Sdingtianhong vlan->netpoll = netpoll; 1072688cea83Sdingtianhong 1073688cea83Sdingtianhong out: 1074688cea83Sdingtianhong return err; 1075688cea83Sdingtianhong } 1076688cea83Sdingtianhong 1077688cea83Sdingtianhong static void macvlan_dev_netpoll_cleanup(struct net_device *dev) 1078688cea83Sdingtianhong { 1079688cea83Sdingtianhong struct macvlan_dev *vlan = netdev_priv(dev); 1080688cea83Sdingtianhong struct netpoll *netpoll = vlan->netpoll; 1081688cea83Sdingtianhong 1082688cea83Sdingtianhong if (!netpoll) 1083688cea83Sdingtianhong return; 1084688cea83Sdingtianhong 1085688cea83Sdingtianhong vlan->netpoll = NULL; 1086688cea83Sdingtianhong 1087688cea83Sdingtianhong __netpoll_free_async(netpoll); 1088688cea83Sdingtianhong } 1089688cea83Sdingtianhong #endif /* CONFIG_NET_POLL_CONTROLLER */ 1090688cea83Sdingtianhong 1091ef5fa6bcSNicolas Dichtel static int macvlan_dev_get_iflink(const struct net_device *dev) 1092ef5fa6bcSNicolas Dichtel { 1093ef5fa6bcSNicolas Dichtel struct macvlan_dev *vlan = netdev_priv(dev); 1094ef5fa6bcSNicolas Dichtel 1095ef5fa6bcSNicolas Dichtel return vlan->lowerdev->ifindex; 1096ef5fa6bcSNicolas Dichtel } 1097ef5fa6bcSNicolas Dichtel 1098b863ceb7SPatrick McHardy static const struct ethtool_ops macvlan_ethtool_ops = { 1099b863ceb7SPatrick McHardy .get_link = ethtool_op_get_link, 110085f95819SDavid Decotigny .get_link_ksettings = macvlan_ethtool_get_link_ksettings, 1101b863ceb7SPatrick McHardy .get_drvinfo = macvlan_ethtool_get_drvinfo, 1102b863ceb7SPatrick McHardy }; 1103b863ceb7SPatrick McHardy 110454a30c97SStephen Hemminger static const struct net_device_ops macvlan_netdev_ops = { 110554a30c97SStephen Hemminger .ndo_init = macvlan_init, 1106fccaf710SEric Dumazet .ndo_uninit = macvlan_uninit, 110754a30c97SStephen Hemminger .ndo_open = macvlan_open, 110854a30c97SStephen Hemminger .ndo_stop = macvlan_stop, 110900829823SStephen Hemminger .ndo_start_xmit = macvlan_start_xmit, 111054a30c97SStephen Hemminger .ndo_change_mtu = macvlan_change_mtu, 11112be5c767SVlad Yasevich .ndo_fix_features = macvlan_fix_features, 111254a30c97SStephen Hemminger .ndo_change_rx_flags = macvlan_change_rx_flags, 111354a30c97SStephen Hemminger .ndo_set_mac_address = macvlan_set_mac_address, 1114df8ef8f3SJohn Fastabend .ndo_set_rx_mode = macvlan_set_mac_lists, 1115bc66154eSEric Dumazet .ndo_get_stats64 = macvlan_dev_get_stats64, 111654a30c97SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 11178d13e670SJohn Fastabend .ndo_vlan_rx_add_vid = macvlan_vlan_rx_add_vid, 11188d13e670SJohn Fastabend .ndo_vlan_rx_kill_vid = macvlan_vlan_rx_kill_vid, 1119df8ef8f3SJohn Fastabend .ndo_fdb_add = macvlan_fdb_add, 1120df8ef8f3SJohn Fastabend .ndo_fdb_del = macvlan_fdb_del, 1121df8ef8f3SJohn Fastabend .ndo_fdb_dump = ndo_dflt_fdb_dump, 1122c674ac30SVlad Yasevich .ndo_get_lock_subclass = macvlan_get_nest_level, 1123688cea83Sdingtianhong #ifdef CONFIG_NET_POLL_CONTROLLER 1124688cea83Sdingtianhong .ndo_poll_controller = macvlan_dev_poll_controller, 1125688cea83Sdingtianhong .ndo_netpoll_setup = macvlan_dev_netpoll_setup, 1126688cea83Sdingtianhong .ndo_netpoll_cleanup = macvlan_dev_netpoll_cleanup, 1127688cea83Sdingtianhong #endif 1128ef5fa6bcSNicolas Dichtel .ndo_get_iflink = macvlan_dev_get_iflink, 1129f56e67b5SToshiaki Makita .ndo_features_check = passthru_features_check, 113054a30c97SStephen Hemminger }; 113154a30c97SStephen Hemminger 11328a35747aSHerbert Xu void macvlan_common_setup(struct net_device *dev) 1133b863ceb7SPatrick McHardy { 1134b863ceb7SPatrick McHardy ether_setup(dev); 1135b863ceb7SPatrick McHardy 113691572088SJarod Wilson dev->min_mtu = 0; 113791572088SJarod Wilson dev->max_mtu = ETH_MAX_MTU; 113802875878SEric Dumazet dev->priv_flags &= ~IFF_TX_SKB_SHARING; 113902875878SEric Dumazet netif_keep_dst(dev); 114087ab7f6fSVlad Yasevich dev->priv_flags |= IFF_UNICAST_FLT; 114154a30c97SStephen Hemminger dev->netdev_ops = &macvlan_netdev_ops; 1142cf124db5SDavid S. Miller dev->needs_free_netdev = true; 114371740129SLutz Jaenicke dev->header_ops = &macvlan_hard_header_ops; 1144b863ceb7SPatrick McHardy dev->ethtool_ops = &macvlan_ethtool_ops; 11458a35747aSHerbert Xu } 11468a35747aSHerbert Xu EXPORT_SYMBOL_GPL(macvlan_common_setup); 11478a35747aSHerbert Xu 11488a35747aSHerbert Xu static void macvlan_setup(struct net_device *dev) 11498a35747aSHerbert Xu { 11508a35747aSHerbert Xu macvlan_common_setup(dev); 11517009212bSZhang Shengju dev->priv_flags |= IFF_NO_QUEUE; 1152b863ceb7SPatrick McHardy } 1153b863ceb7SPatrick McHardy 1154b863ceb7SPatrick McHardy static int macvlan_port_create(struct net_device *dev) 1155b863ceb7SPatrick McHardy { 1156b863ceb7SPatrick McHardy struct macvlan_port *port; 1157b863ceb7SPatrick McHardy unsigned int i; 1158ab95bfe0SJiri Pirko int err; 1159b863ceb7SPatrick McHardy 1160b863ceb7SPatrick McHardy if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK) 1161b863ceb7SPatrick McHardy return -EINVAL; 1162b863ceb7SPatrick McHardy 1163322dc6e0SMahesh Bandewar if (netdev_is_rx_handler_busy(dev)) 1164d6b00fecSMahesh Bandewar return -EBUSY; 1165d6b00fecSMahesh Bandewar 1166b863ceb7SPatrick McHardy port = kzalloc(sizeof(*port), GFP_KERNEL); 1167b863ceb7SPatrick McHardy if (port == NULL) 1168b863ceb7SPatrick McHardy return -ENOMEM; 1169b863ceb7SPatrick McHardy 1170b863ceb7SPatrick McHardy port->dev = dev; 117118c8c54dSVlad Yasevich ether_addr_copy(port->perm_addr, dev->dev_addr); 1172b863ceb7SPatrick McHardy INIT_LIST_HEAD(&port->vlans); 1173b863ceb7SPatrick McHardy for (i = 0; i < MACVLAN_HASH_SIZE; i++) 1174b863ceb7SPatrick McHardy INIT_HLIST_HEAD(&port->vlan_hash[i]); 117579cf79abSMichael Braun for (i = 0; i < MACVLAN_HASH_SIZE; i++) 117679cf79abSMichael Braun INIT_HLIST_HEAD(&port->vlan_source_hash[i]); 1177ab95bfe0SJiri Pirko 1178412ca155SHerbert Xu skb_queue_head_init(&port->bc_queue); 1179412ca155SHerbert Xu INIT_WORK(&port->bc_work, macvlan_process_broadcast); 1180412ca155SHerbert Xu 1181a35e2c1bSJiri Pirko err = netdev_rx_handler_register(dev, macvlan_handle_frame, port); 1182a35e2c1bSJiri Pirko if (err) 1183ab95bfe0SJiri Pirko kfree(port); 1184d9351561SEric Dumazet else 1185a35e2c1bSJiri Pirko dev->priv_flags |= IFF_MACVLAN_PORT; 1186ab95bfe0SJiri Pirko return err; 1187b863ceb7SPatrick McHardy } 1188b863ceb7SPatrick McHardy 1189b863ceb7SPatrick McHardy static void macvlan_port_destroy(struct net_device *dev) 1190b863ceb7SPatrick McHardy { 1191e052f7e6SEric Dumazet struct macvlan_port *port = macvlan_port_get_rtnl(dev); 1192f6478218SHerbert Xu struct sk_buff *skb; 1193b863ceb7SPatrick McHardy 1194a35e2c1bSJiri Pirko dev->priv_flags &= ~IFF_MACVLAN_PORT; 1195ab95bfe0SJiri Pirko netdev_rx_handler_unregister(dev); 1196fe0ca732SEric Dumazet 1197fe0ca732SEric Dumazet /* After this point, no packet can schedule bc_work anymore, 1198fe0ca732SEric Dumazet * but we need to cancel it and purge left skbs if any. 1199fe0ca732SEric Dumazet */ 1200fe0ca732SEric Dumazet cancel_work_sync(&port->bc_work); 1201f6478218SHerbert Xu 1202f6478218SHerbert Xu while ((skb = __skb_dequeue(&port->bc_queue))) { 1203f6478218SHerbert Xu const struct macvlan_dev *src = MACVLAN_SKB_CB(skb)->src; 1204f6478218SHerbert Xu 1205f6478218SHerbert Xu if (src) 1206f6478218SHerbert Xu dev_put(src->dev); 1207f6478218SHerbert Xu 1208f6478218SHerbert Xu kfree_skb(skb); 1209f6478218SHerbert Xu } 1210fe0ca732SEric Dumazet 121118c8c54dSVlad Yasevich /* If the lower device address has been changed by passthru 121218c8c54dSVlad Yasevich * macvlan, put it back. 121318c8c54dSVlad Yasevich */ 121418c8c54dSVlad Yasevich if (macvlan_passthru(port) && 121518c8c54dSVlad Yasevich !ether_addr_equal(port->dev->dev_addr, port->perm_addr)) { 121618c8c54dSVlad Yasevich struct sockaddr sa; 121718c8c54dSVlad Yasevich 121818c8c54dSVlad Yasevich sa.sa_family = port->dev->type; 121918c8c54dSVlad Yasevich memcpy(&sa.sa_data, port->perm_addr, port->dev->addr_len); 122018c8c54dSVlad Yasevich dev_set_mac_address(port->dev, &sa); 122118c8c54dSVlad Yasevich } 122218c8c54dSVlad Yasevich 1223a1f5315cSGao Feng kfree(port); 1224b863ceb7SPatrick McHardy } 1225b863ceb7SPatrick McHardy 1226a8b8a889SMatthias Schiffer static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[], 1227a8b8a889SMatthias Schiffer struct netlink_ext_ack *extack) 1228b863ceb7SPatrick McHardy { 1229b863ceb7SPatrick McHardy if (tb[IFLA_ADDRESS]) { 1230b863ceb7SPatrick McHardy if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) 1231b863ceb7SPatrick McHardy return -EINVAL; 1232b863ceb7SPatrick McHardy if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) 1233b863ceb7SPatrick McHardy return -EADDRNOTAVAIL; 1234b863ceb7SPatrick McHardy } 123527c0b1a8SArnd Bergmann 123607850a4fSZhang Shengju if (!data) 123707850a4fSZhang Shengju return 0; 123807850a4fSZhang Shengju 123907850a4fSZhang Shengju if (data[IFLA_MACVLAN_FLAGS] && 124015127478SMichael S. Tsirkin nla_get_u16(data[IFLA_MACVLAN_FLAGS]) & ~MACVLAN_FLAG_NOPROMISC) 124115127478SMichael S. Tsirkin return -EINVAL; 124215127478SMichael S. Tsirkin 124307850a4fSZhang Shengju if (data[IFLA_MACVLAN_MODE]) { 124427c0b1a8SArnd Bergmann switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) { 124527c0b1a8SArnd Bergmann case MACVLAN_MODE_PRIVATE: 124627c0b1a8SArnd Bergmann case MACVLAN_MODE_VEPA: 124727c0b1a8SArnd Bergmann case MACVLAN_MODE_BRIDGE: 1248eb06acdcSSridhar Samudrala case MACVLAN_MODE_PASSTHRU: 124979cf79abSMichael Braun case MACVLAN_MODE_SOURCE: 125027c0b1a8SArnd Bergmann break; 125127c0b1a8SArnd Bergmann default: 125227c0b1a8SArnd Bergmann return -EINVAL; 125327c0b1a8SArnd Bergmann } 125427c0b1a8SArnd Bergmann } 125579cf79abSMichael Braun 125607850a4fSZhang Shengju if (data[IFLA_MACVLAN_MACADDR_MODE]) { 125779cf79abSMichael Braun switch (nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE])) { 125879cf79abSMichael Braun case MACVLAN_MACADDR_ADD: 125979cf79abSMichael Braun case MACVLAN_MACADDR_DEL: 126079cf79abSMichael Braun case MACVLAN_MACADDR_FLUSH: 126179cf79abSMichael Braun case MACVLAN_MACADDR_SET: 126279cf79abSMichael Braun break; 126379cf79abSMichael Braun default: 126479cf79abSMichael Braun return -EINVAL; 126579cf79abSMichael Braun } 126679cf79abSMichael Braun } 126779cf79abSMichael Braun 126807850a4fSZhang Shengju if (data[IFLA_MACVLAN_MACADDR]) { 126979cf79abSMichael Braun if (nla_len(data[IFLA_MACVLAN_MACADDR]) != ETH_ALEN) 127079cf79abSMichael Braun return -EINVAL; 127179cf79abSMichael Braun 127279cf79abSMichael Braun if (!is_valid_ether_addr(nla_data(data[IFLA_MACVLAN_MACADDR]))) 127379cf79abSMichael Braun return -EADDRNOTAVAIL; 127479cf79abSMichael Braun } 127579cf79abSMichael Braun 127607850a4fSZhang Shengju if (data[IFLA_MACVLAN_MACADDR_COUNT]) 127779cf79abSMichael Braun return -EINVAL; 127879cf79abSMichael Braun 127979cf79abSMichael Braun return 0; 128079cf79abSMichael Braun } 128179cf79abSMichael Braun 128279cf79abSMichael Braun /** 128379cf79abSMichael Braun * reconfigure list of remote source mac address 128479cf79abSMichael Braun * (only for macvlan devices in source mode) 128579cf79abSMichael Braun * Note regarding alignment: all netlink data is aligned to 4 Byte, which 128679cf79abSMichael Braun * suffices for both ether_addr_copy and ether_addr_equal_64bits usage. 128779cf79abSMichael Braun */ 128879cf79abSMichael Braun static int macvlan_changelink_sources(struct macvlan_dev *vlan, u32 mode, 128979cf79abSMichael Braun struct nlattr *data[]) 129079cf79abSMichael Braun { 129179cf79abSMichael Braun char *addr = NULL; 129279cf79abSMichael Braun int ret, rem, len; 129379cf79abSMichael Braun struct nlattr *nla, *head; 129479cf79abSMichael Braun struct macvlan_source_entry *entry; 129579cf79abSMichael Braun 129679cf79abSMichael Braun if (data[IFLA_MACVLAN_MACADDR]) 129779cf79abSMichael Braun addr = nla_data(data[IFLA_MACVLAN_MACADDR]); 129879cf79abSMichael Braun 129979cf79abSMichael Braun if (mode == MACVLAN_MACADDR_ADD) { 130079cf79abSMichael Braun if (!addr) 130179cf79abSMichael Braun return -EINVAL; 130279cf79abSMichael Braun 130379cf79abSMichael Braun return macvlan_hash_add_source(vlan, addr); 130479cf79abSMichael Braun 130579cf79abSMichael Braun } else if (mode == MACVLAN_MACADDR_DEL) { 130679cf79abSMichael Braun if (!addr) 130779cf79abSMichael Braun return -EINVAL; 130879cf79abSMichael Braun 130979cf79abSMichael Braun entry = macvlan_hash_lookup_source(vlan, addr); 131079cf79abSMichael Braun if (entry) { 131179cf79abSMichael Braun macvlan_hash_del_source(entry); 131279cf79abSMichael Braun vlan->macaddr_count--; 131379cf79abSMichael Braun } 131479cf79abSMichael Braun } else if (mode == MACVLAN_MACADDR_FLUSH) { 131579cf79abSMichael Braun macvlan_flush_sources(vlan->port, vlan); 131679cf79abSMichael Braun } else if (mode == MACVLAN_MACADDR_SET) { 131779cf79abSMichael Braun macvlan_flush_sources(vlan->port, vlan); 131879cf79abSMichael Braun 131979cf79abSMichael Braun if (addr) { 132079cf79abSMichael Braun ret = macvlan_hash_add_source(vlan, addr); 132179cf79abSMichael Braun if (ret) 132279cf79abSMichael Braun return ret; 132379cf79abSMichael Braun } 132479cf79abSMichael Braun 132579cf79abSMichael Braun if (!data || !data[IFLA_MACVLAN_MACADDR_DATA]) 132679cf79abSMichael Braun return 0; 132779cf79abSMichael Braun 132879cf79abSMichael Braun head = nla_data(data[IFLA_MACVLAN_MACADDR_DATA]); 132979cf79abSMichael Braun len = nla_len(data[IFLA_MACVLAN_MACADDR_DATA]); 133079cf79abSMichael Braun 133179cf79abSMichael Braun nla_for_each_attr(nla, head, len, rem) { 133279cf79abSMichael Braun if (nla_type(nla) != IFLA_MACVLAN_MACADDR || 133379cf79abSMichael Braun nla_len(nla) != ETH_ALEN) 133479cf79abSMichael Braun continue; 133579cf79abSMichael Braun 133679cf79abSMichael Braun addr = nla_data(nla); 133779cf79abSMichael Braun ret = macvlan_hash_add_source(vlan, addr); 133879cf79abSMichael Braun if (ret) 133979cf79abSMichael Braun return ret; 134079cf79abSMichael Braun } 134179cf79abSMichael Braun } else { 134279cf79abSMichael Braun return -EINVAL; 134379cf79abSMichael Braun } 134479cf79abSMichael Braun 1345b863ceb7SPatrick McHardy return 0; 1346b863ceb7SPatrick McHardy } 1347b863ceb7SPatrick McHardy 1348fc0663d6SArnd Bergmann int macvlan_common_newlink(struct net *src_net, struct net_device *dev, 134942ab19eeSDavid Ahern struct nlattr *tb[], struct nlattr *data[], 135042ab19eeSDavid Ahern struct netlink_ext_ack *extack) 1351b863ceb7SPatrick McHardy { 1352b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 1353b863ceb7SPatrick McHardy struct macvlan_port *port; 1354b863ceb7SPatrick McHardy struct net_device *lowerdev; 1355b863ceb7SPatrick McHardy int err; 135679cf79abSMichael Braun int macmode; 1357aa5fd0fbSGao Feng bool create = false; 1358b863ceb7SPatrick McHardy 1359b863ceb7SPatrick McHardy if (!tb[IFLA_LINK]) 1360b863ceb7SPatrick McHardy return -EINVAL; 1361b863ceb7SPatrick McHardy 136281adee47SEric W. Biederman lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK])); 1363b863ceb7SPatrick McHardy if (lowerdev == NULL) 1364b863ceb7SPatrick McHardy return -ENODEV; 1365b863ceb7SPatrick McHardy 1366d70f2cf5SKevin Wallace /* When creating macvlans or macvtaps on top of other macvlans - use 1367b0832a29SEric Biederman * the real device as the lowerdev. 1368a6ca5f1dSPatrick McHardy */ 1369d70f2cf5SKevin Wallace if (netif_is_macvlan(lowerdev)) 1370d70f2cf5SKevin Wallace lowerdev = macvlan_dev_real_dev(lowerdev); 1371a6ca5f1dSPatrick McHardy 1372b863ceb7SPatrick McHardy if (!tb[IFLA_MTU]) 1373b863ceb7SPatrick McHardy dev->mtu = lowerdev->mtu; 1374b863ceb7SPatrick McHardy else if (dev->mtu > lowerdev->mtu) 1375b863ceb7SPatrick McHardy return -EINVAL; 1376b863ceb7SPatrick McHardy 137791572088SJarod Wilson /* MTU range: 68 - lowerdev->max_mtu */ 137891572088SJarod Wilson dev->min_mtu = ETH_MIN_MTU; 137991572088SJarod Wilson dev->max_mtu = lowerdev->max_mtu; 138091572088SJarod Wilson 1381b863ceb7SPatrick McHardy if (!tb[IFLA_ADDRESS]) 13827ce5d222SDanny Kukawka eth_hw_addr_random(dev); 1383b863ceb7SPatrick McHardy 1384a35e2c1bSJiri Pirko if (!macvlan_port_exists(lowerdev)) { 1385b863ceb7SPatrick McHardy err = macvlan_port_create(lowerdev); 1386b863ceb7SPatrick McHardy if (err < 0) 1387b863ceb7SPatrick McHardy return err; 1388aa5fd0fbSGao Feng create = true; 1389b863ceb7SPatrick McHardy } 1390e052f7e6SEric Dumazet port = macvlan_port_get_rtnl(lowerdev); 1391b863ceb7SPatrick McHardy 1392eb06acdcSSridhar Samudrala /* Only 1 macvlan device can be created in passthru mode */ 139343c2d578SVlad Yasevich if (macvlan_passthru(port)) { 1394aa5fd0fbSGao Feng /* The macvlan port must be not created this time, 1395aa5fd0fbSGao Feng * still goto destroy_macvlan_port for readability. 1396aa5fd0fbSGao Feng */ 1397aa5fd0fbSGao Feng err = -EINVAL; 1398aa5fd0fbSGao Feng goto destroy_macvlan_port; 1399aa5fd0fbSGao Feng } 1400eb06acdcSSridhar Samudrala 1401b863ceb7SPatrick McHardy vlan->lowerdev = lowerdev; 1402b863ceb7SPatrick McHardy vlan->dev = dev; 1403b863ceb7SPatrick McHardy vlan->port = port; 14042be5c767SVlad Yasevich vlan->set_features = MACVLAN_FEATURES; 1405952fcfd0SSabrina Dubroca vlan->nest_level = dev_get_nest_level(lowerdev) + 1; 1406b863ceb7SPatrick McHardy 140727c0b1a8SArnd Bergmann vlan->mode = MACVLAN_MODE_VEPA; 140827c0b1a8SArnd Bergmann if (data && data[IFLA_MACVLAN_MODE]) 140927c0b1a8SArnd Bergmann vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]); 141027c0b1a8SArnd Bergmann 1411df8ef8f3SJohn Fastabend if (data && data[IFLA_MACVLAN_FLAGS]) 1412df8ef8f3SJohn Fastabend vlan->flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]); 1413df8ef8f3SJohn Fastabend 1414eb06acdcSSridhar Samudrala if (vlan->mode == MACVLAN_MODE_PASSTHRU) { 1415aa5fd0fbSGao Feng if (port->count) { 1416aa5fd0fbSGao Feng err = -EINVAL; 1417aa5fd0fbSGao Feng goto destroy_macvlan_port; 1418aa5fd0fbSGao Feng } 141943c2d578SVlad Yasevich macvlan_set_passthru(port); 14208b98604eSBjørn Mork eth_hw_addr_inherit(dev, lowerdev); 1421eb06acdcSSridhar Samudrala } 1422eb06acdcSSridhar Samudrala 142379cf79abSMichael Braun if (data && data[IFLA_MACVLAN_MACADDR_MODE]) { 1424aa5fd0fbSGao Feng if (vlan->mode != MACVLAN_MODE_SOURCE) { 1425aa5fd0fbSGao Feng err = -EINVAL; 1426aa5fd0fbSGao Feng goto destroy_macvlan_port; 1427aa5fd0fbSGao Feng } 142879cf79abSMichael Braun macmode = nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE]); 142979cf79abSMichael Braun err = macvlan_changelink_sources(vlan, macmode, data); 143079cf79abSMichael Braun if (err) 1431aa5fd0fbSGao Feng goto destroy_macvlan_port; 143279cf79abSMichael Braun } 143379cf79abSMichael Braun 143447d4ab91SJohn Fastabend err = register_netdevice(dev); 143547d4ab91SJohn Fastabend if (err < 0) 1436aa5fd0fbSGao Feng goto destroy_macvlan_port; 143747d4ab91SJohn Fastabend 1438a6cc0cfaSJohn Fastabend dev->priv_flags |= IFF_MACVLAN; 143942ab19eeSDavid Ahern err = netdev_upper_dev_link(lowerdev, dev, extack); 14407cd43db7SJiri Pirko if (err) 1441da37705cSCong Wang goto unregister_netdev; 1442b863ceb7SPatrick McHardy 1443233c7df0SJiri Pirko list_add_tail_rcu(&vlan->list, &port->vlans); 1444fc4a7489SPatrick Mullaney netif_stacked_transfer_operstate(lowerdev, dev); 1445de7d244dSNikolay Aleksandrov linkwatch_fire_event(dev); 1446f16d3d57SJiri Pirko 1447b863ceb7SPatrick McHardy return 0; 1448f16d3d57SJiri Pirko 1449da37705cSCong Wang unregister_netdev: 1450da37705cSCong Wang unregister_netdevice(dev); 1451aa5fd0fbSGao Feng destroy_macvlan_port: 1452aa5fd0fbSGao Feng if (create) 1453aa5fd0fbSGao Feng macvlan_port_destroy(port->dev); 1454f16d3d57SJiri Pirko return err; 1455b863ceb7SPatrick McHardy } 1456fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_common_newlink); 1457b863ceb7SPatrick McHardy 1458fc0663d6SArnd Bergmann static int macvlan_newlink(struct net *src_net, struct net_device *dev, 14597a3f4a18SMatthias Schiffer struct nlattr *tb[], struct nlattr *data[], 14607a3f4a18SMatthias Schiffer struct netlink_ext_ack *extack) 1461fc0663d6SArnd Bergmann { 146242ab19eeSDavid Ahern return macvlan_common_newlink(src_net, dev, tb, data, extack); 1463fc0663d6SArnd Bergmann } 1464fc0663d6SArnd Bergmann 1465fc0663d6SArnd Bergmann void macvlan_dellink(struct net_device *dev, struct list_head *head) 1466b863ceb7SPatrick McHardy { 1467b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 1468b863ceb7SPatrick McHardy 146979cf79abSMichael Braun if (vlan->mode == MACVLAN_MODE_SOURCE) 147079cf79abSMichael Braun macvlan_flush_sources(vlan->port, vlan); 1471233c7df0SJiri Pirko list_del_rcu(&vlan->list); 147223289a37SEric Dumazet unregister_netdevice_queue(dev, head); 14737cd43db7SJiri Pirko netdev_upper_dev_unlink(vlan->lowerdev, dev); 1474b863ceb7SPatrick McHardy } 1475fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_dellink); 1476b863ceb7SPatrick McHardy 147727c0b1a8SArnd Bergmann static int macvlan_changelink(struct net_device *dev, 1478ad744b22SMatthias Schiffer struct nlattr *tb[], struct nlattr *data[], 1479ad744b22SMatthias Schiffer struct netlink_ext_ack *extack) 148027c0b1a8SArnd Bergmann { 148127c0b1a8SArnd Bergmann struct macvlan_dev *vlan = netdev_priv(dev); 1482266e8347SMichael S. Tsirkin enum macvlan_mode mode; 1483266e8347SMichael S. Tsirkin bool set_mode = false; 148479cf79abSMichael Braun enum macvlan_macaddr_mode macmode; 148579cf79abSMichael Braun int ret; 1486266e8347SMichael S. Tsirkin 1487266e8347SMichael S. Tsirkin /* Validate mode, but don't set yet: setting flags may fail. */ 1488266e8347SMichael S. Tsirkin if (data && data[IFLA_MACVLAN_MODE]) { 1489266e8347SMichael S. Tsirkin set_mode = true; 1490266e8347SMichael S. Tsirkin mode = nla_get_u32(data[IFLA_MACVLAN_MODE]); 1491266e8347SMichael S. Tsirkin /* Passthrough mode can't be set or cleared dynamically */ 1492266e8347SMichael S. Tsirkin if ((mode == MACVLAN_MODE_PASSTHRU) != 1493266e8347SMichael S. Tsirkin (vlan->mode == MACVLAN_MODE_PASSTHRU)) 1494266e8347SMichael S. Tsirkin return -EINVAL; 149579cf79abSMichael Braun if (vlan->mode == MACVLAN_MODE_SOURCE && 149679cf79abSMichael Braun vlan->mode != mode) 149779cf79abSMichael Braun macvlan_flush_sources(vlan->port, vlan); 1498266e8347SMichael S. Tsirkin } 149999ffc3e7SMichael S. Tsirkin 1500df8ef8f3SJohn Fastabend if (data && data[IFLA_MACVLAN_FLAGS]) { 1501df8ef8f3SJohn Fastabend __u16 flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]); 1502df8ef8f3SJohn Fastabend bool promisc = (flags ^ vlan->flags) & MACVLAN_FLAG_NOPROMISC; 150343c2d578SVlad Yasevich if (macvlan_passthru(vlan->port) && promisc) { 150499ffc3e7SMichael S. Tsirkin int err; 1505df8ef8f3SJohn Fastabend 150699ffc3e7SMichael S. Tsirkin if (flags & MACVLAN_FLAG_NOPROMISC) 150799ffc3e7SMichael S. Tsirkin err = dev_set_promiscuity(vlan->lowerdev, -1); 150899ffc3e7SMichael S. Tsirkin else 150999ffc3e7SMichael S. Tsirkin err = dev_set_promiscuity(vlan->lowerdev, 1); 151099ffc3e7SMichael S. Tsirkin if (err < 0) 151199ffc3e7SMichael S. Tsirkin return err; 151299ffc3e7SMichael S. Tsirkin } 1513df8ef8f3SJohn Fastabend vlan->flags = flags; 1514df8ef8f3SJohn Fastabend } 1515266e8347SMichael S. Tsirkin if (set_mode) 1516266e8347SMichael S. Tsirkin vlan->mode = mode; 151779cf79abSMichael Braun if (data && data[IFLA_MACVLAN_MACADDR_MODE]) { 151879cf79abSMichael Braun if (vlan->mode != MACVLAN_MODE_SOURCE) 151979cf79abSMichael Braun return -EINVAL; 152079cf79abSMichael Braun macmode = nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE]); 152179cf79abSMichael Braun ret = macvlan_changelink_sources(vlan, macmode, data); 152279cf79abSMichael Braun if (ret) 152379cf79abSMichael Braun return ret; 152479cf79abSMichael Braun } 152527c0b1a8SArnd Bergmann return 0; 152627c0b1a8SArnd Bergmann } 152727c0b1a8SArnd Bergmann 152879cf79abSMichael Braun static size_t macvlan_get_size_mac(const struct macvlan_dev *vlan) 152979cf79abSMichael Braun { 153079cf79abSMichael Braun if (vlan->macaddr_count == 0) 153179cf79abSMichael Braun return 0; 153279cf79abSMichael Braun return nla_total_size(0) /* IFLA_MACVLAN_MACADDR_DATA */ 153379cf79abSMichael Braun + vlan->macaddr_count * nla_total_size(sizeof(u8) * ETH_ALEN); 153479cf79abSMichael Braun } 153579cf79abSMichael Braun 153627c0b1a8SArnd Bergmann static size_t macvlan_get_size(const struct net_device *dev) 153727c0b1a8SArnd Bergmann { 153879cf79abSMichael Braun struct macvlan_dev *vlan = netdev_priv(dev); 153979cf79abSMichael Braun 154001fe944fSEric Dumazet return (0 154101fe944fSEric Dumazet + nla_total_size(4) /* IFLA_MACVLAN_MODE */ 154201fe944fSEric Dumazet + nla_total_size(2) /* IFLA_MACVLAN_FLAGS */ 154379cf79abSMichael Braun + nla_total_size(4) /* IFLA_MACVLAN_MACADDR_COUNT */ 154479cf79abSMichael Braun + macvlan_get_size_mac(vlan) /* IFLA_MACVLAN_MACADDR */ 154501fe944fSEric Dumazet ); 154627c0b1a8SArnd Bergmann } 154727c0b1a8SArnd Bergmann 154879cf79abSMichael Braun static int macvlan_fill_info_macaddr(struct sk_buff *skb, 154979cf79abSMichael Braun const struct macvlan_dev *vlan, 155079cf79abSMichael Braun const int i) 155179cf79abSMichael Braun { 155279cf79abSMichael Braun struct hlist_head *h = &vlan->port->vlan_source_hash[i]; 155379cf79abSMichael Braun struct macvlan_source_entry *entry; 155479cf79abSMichael Braun 155579cf79abSMichael Braun hlist_for_each_entry_rcu(entry, h, hlist) { 155679cf79abSMichael Braun if (entry->vlan != vlan) 155779cf79abSMichael Braun continue; 155879cf79abSMichael Braun if (nla_put(skb, IFLA_MACVLAN_MACADDR, ETH_ALEN, entry->addr)) 155979cf79abSMichael Braun return 1; 156079cf79abSMichael Braun } 156179cf79abSMichael Braun return 0; 156279cf79abSMichael Braun } 156379cf79abSMichael Braun 156427c0b1a8SArnd Bergmann static int macvlan_fill_info(struct sk_buff *skb, 156527c0b1a8SArnd Bergmann const struct net_device *dev) 156627c0b1a8SArnd Bergmann { 156727c0b1a8SArnd Bergmann struct macvlan_dev *vlan = netdev_priv(dev); 156879cf79abSMichael Braun int i; 156979cf79abSMichael Braun struct nlattr *nest; 157027c0b1a8SArnd Bergmann 1571ead9a76cSDavid S. Miller if (nla_put_u32(skb, IFLA_MACVLAN_MODE, vlan->mode)) 1572ead9a76cSDavid S. Miller goto nla_put_failure; 1573df8ef8f3SJohn Fastabend if (nla_put_u16(skb, IFLA_MACVLAN_FLAGS, vlan->flags)) 1574df8ef8f3SJohn Fastabend goto nla_put_failure; 157579cf79abSMichael Braun if (nla_put_u32(skb, IFLA_MACVLAN_MACADDR_COUNT, vlan->macaddr_count)) 157679cf79abSMichael Braun goto nla_put_failure; 157779cf79abSMichael Braun if (vlan->macaddr_count > 0) { 157879cf79abSMichael Braun nest = nla_nest_start(skb, IFLA_MACVLAN_MACADDR_DATA); 157979cf79abSMichael Braun if (nest == NULL) 158079cf79abSMichael Braun goto nla_put_failure; 158179cf79abSMichael Braun 158279cf79abSMichael Braun for (i = 0; i < MACVLAN_HASH_SIZE; i++) { 158379cf79abSMichael Braun if (macvlan_fill_info_macaddr(skb, vlan, i)) 158479cf79abSMichael Braun goto nla_put_failure; 158579cf79abSMichael Braun } 158679cf79abSMichael Braun nla_nest_end(skb, nest); 158779cf79abSMichael Braun } 158827c0b1a8SArnd Bergmann return 0; 158927c0b1a8SArnd Bergmann 159027c0b1a8SArnd Bergmann nla_put_failure: 159127c0b1a8SArnd Bergmann return -EMSGSIZE; 159227c0b1a8SArnd Bergmann } 159327c0b1a8SArnd Bergmann 159427c0b1a8SArnd Bergmann static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = { 159527c0b1a8SArnd Bergmann [IFLA_MACVLAN_MODE] = { .type = NLA_U32 }, 1596df8ef8f3SJohn Fastabend [IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 }, 159779cf79abSMichael Braun [IFLA_MACVLAN_MACADDR_MODE] = { .type = NLA_U32 }, 159879cf79abSMichael Braun [IFLA_MACVLAN_MACADDR] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 159979cf79abSMichael Braun [IFLA_MACVLAN_MACADDR_DATA] = { .type = NLA_NESTED }, 160079cf79abSMichael Braun [IFLA_MACVLAN_MACADDR_COUNT] = { .type = NLA_U32 }, 160127c0b1a8SArnd Bergmann }; 160227c0b1a8SArnd Bergmann 1603fc0663d6SArnd Bergmann int macvlan_link_register(struct rtnl_link_ops *ops) 1604fc0663d6SArnd Bergmann { 1605fc0663d6SArnd Bergmann /* common fields */ 1606fc0663d6SArnd Bergmann ops->validate = macvlan_validate; 1607fc0663d6SArnd Bergmann ops->maxtype = IFLA_MACVLAN_MAX; 1608fc0663d6SArnd Bergmann ops->policy = macvlan_policy; 1609fc0663d6SArnd Bergmann ops->changelink = macvlan_changelink; 1610fc0663d6SArnd Bergmann ops->get_size = macvlan_get_size; 1611fc0663d6SArnd Bergmann ops->fill_info = macvlan_fill_info; 1612fc0663d6SArnd Bergmann 1613fc0663d6SArnd Bergmann return rtnl_link_register(ops); 1614fc0663d6SArnd Bergmann }; 1615fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_link_register); 1616fc0663d6SArnd Bergmann 1617eaca400fSNicolas Dichtel static struct net *macvlan_get_link_net(const struct net_device *dev) 1618eaca400fSNicolas Dichtel { 1619eaca400fSNicolas Dichtel return dev_net(macvlan_dev_real_dev(dev)); 1620eaca400fSNicolas Dichtel } 1621eaca400fSNicolas Dichtel 1622fc0663d6SArnd Bergmann static struct rtnl_link_ops macvlan_link_ops = { 1623b863ceb7SPatrick McHardy .kind = "macvlan", 16248a35747aSHerbert Xu .setup = macvlan_setup, 1625b863ceb7SPatrick McHardy .newlink = macvlan_newlink, 1626b863ceb7SPatrick McHardy .dellink = macvlan_dellink, 1627eaca400fSNicolas Dichtel .get_link_net = macvlan_get_link_net, 16286fe3faf8SSainath Grandhi .priv_size = sizeof(struct macvlan_dev), 1629b863ceb7SPatrick McHardy }; 1630b863ceb7SPatrick McHardy 1631b863ceb7SPatrick McHardy static int macvlan_device_event(struct notifier_block *unused, 1632b863ceb7SPatrick McHardy unsigned long event, void *ptr) 1633b863ceb7SPatrick McHardy { 1634351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 1635b863ceb7SPatrick McHardy struct macvlan_dev *vlan, *next; 1636b863ceb7SPatrick McHardy struct macvlan_port *port; 1637226bd341SEric Dumazet LIST_HEAD(list_kill); 1638b863ceb7SPatrick McHardy 1639a35e2c1bSJiri Pirko if (!macvlan_port_exists(dev)) 1640b863ceb7SPatrick McHardy return NOTIFY_DONE; 1641b863ceb7SPatrick McHardy 1642e052f7e6SEric Dumazet port = macvlan_port_get_rtnl(dev); 1643a35e2c1bSJiri Pirko 1644b863ceb7SPatrick McHardy switch (event) { 1645de7d244dSNikolay Aleksandrov case NETDEV_UP: 1646b863ceb7SPatrick McHardy case NETDEV_CHANGE: 1647b863ceb7SPatrick McHardy list_for_each_entry(vlan, &port->vlans, list) 1648fc4a7489SPatrick Mullaney netif_stacked_transfer_operstate(vlan->lowerdev, 1649fc4a7489SPatrick Mullaney vlan->dev); 1650b863ceb7SPatrick McHardy break; 1651b863ceb7SPatrick McHardy case NETDEV_FEAT_CHANGE: 1652b863ceb7SPatrick McHardy list_for_each_entry(vlan, &port->vlans, list) { 16538c2acc53SPatrick McHardy vlan->dev->gso_max_size = dev->gso_max_size; 1654f6773c5eSEric Dumazet vlan->dev->gso_max_segs = dev->gso_max_segs; 1655797f87f8SFlorian Westphal netdev_update_features(vlan->dev); 1656b863ceb7SPatrick McHardy } 1657b863ceb7SPatrick McHardy break; 16583763e7efSdingtianhong case NETDEV_CHANGEMTU: 16593763e7efSdingtianhong list_for_each_entry(vlan, &port->vlans, list) { 16603763e7efSdingtianhong if (vlan->dev->mtu <= dev->mtu) 16613763e7efSdingtianhong continue; 16623763e7efSdingtianhong dev_set_mtu(vlan->dev, dev->mtu); 16633763e7efSdingtianhong } 16643763e7efSdingtianhong break; 1665e289fd28Sdingtianhong case NETDEV_CHANGEADDR: 166643c2d578SVlad Yasevich if (!macvlan_passthru(port)) 1667e289fd28Sdingtianhong return NOTIFY_DONE; 1668e289fd28Sdingtianhong 1669e289fd28Sdingtianhong vlan = list_first_entry_or_null(&port->vlans, 1670e289fd28Sdingtianhong struct macvlan_dev, 1671e289fd28Sdingtianhong list); 1672e289fd28Sdingtianhong 1673e289fd28Sdingtianhong if (macvlan_sync_address(vlan->dev, dev->dev_addr)) 1674e289fd28Sdingtianhong return NOTIFY_BAD; 1675e289fd28Sdingtianhong 1676e289fd28Sdingtianhong break; 1677b863ceb7SPatrick McHardy case NETDEV_UNREGISTER: 16783b27e105SDavid Lamparter /* twiddle thumbs on netns device moves */ 16793b27e105SDavid Lamparter if (dev->reg_state != NETREG_UNREGISTERING) 16803b27e105SDavid Lamparter break; 16813b27e105SDavid Lamparter 1682b863ceb7SPatrick McHardy list_for_each_entry_safe(vlan, next, &port->vlans, list) 1683226bd341SEric Dumazet vlan->dev->rtnl_link_ops->dellink(vlan->dev, &list_kill); 1684226bd341SEric Dumazet unregister_netdevice_many(&list_kill); 1685b863ceb7SPatrick McHardy break; 16861c01fe14SJiri Pirko case NETDEV_PRE_TYPE_CHANGE: 16871c01fe14SJiri Pirko /* Forbid underlaying device to change its type. */ 16881c01fe14SJiri Pirko return NOTIFY_BAD; 16894c991255SVlad Yasevich 16904c991255SVlad Yasevich case NETDEV_NOTIFY_PEERS: 16914c991255SVlad Yasevich case NETDEV_BONDING_FAILOVER: 16924c991255SVlad Yasevich case NETDEV_RESEND_IGMP: 16934c991255SVlad Yasevich /* Propagate to all vlans */ 16944c991255SVlad Yasevich list_for_each_entry(vlan, &port->vlans, list) 16954c991255SVlad Yasevich call_netdevice_notifiers(event, vlan->dev); 1696b863ceb7SPatrick McHardy } 1697b863ceb7SPatrick McHardy return NOTIFY_DONE; 1698b863ceb7SPatrick McHardy } 1699b863ceb7SPatrick McHardy 1700b863ceb7SPatrick McHardy static struct notifier_block macvlan_notifier_block __read_mostly = { 1701b863ceb7SPatrick McHardy .notifier_call = macvlan_device_event, 1702b863ceb7SPatrick McHardy }; 1703b863ceb7SPatrick McHardy 1704b863ceb7SPatrick McHardy static int __init macvlan_init_module(void) 1705b863ceb7SPatrick McHardy { 1706b863ceb7SPatrick McHardy int err; 1707b863ceb7SPatrick McHardy 1708b863ceb7SPatrick McHardy register_netdevice_notifier(&macvlan_notifier_block); 1709b863ceb7SPatrick McHardy 1710fc0663d6SArnd Bergmann err = macvlan_link_register(&macvlan_link_ops); 1711b863ceb7SPatrick McHardy if (err < 0) 1712b863ceb7SPatrick McHardy goto err1; 1713b863ceb7SPatrick McHardy return 0; 1714b863ceb7SPatrick McHardy err1: 1715b863ceb7SPatrick McHardy unregister_netdevice_notifier(&macvlan_notifier_block); 1716b863ceb7SPatrick McHardy return err; 1717b863ceb7SPatrick McHardy } 1718b863ceb7SPatrick McHardy 1719b863ceb7SPatrick McHardy static void __exit macvlan_cleanup_module(void) 1720b863ceb7SPatrick McHardy { 1721b863ceb7SPatrick McHardy rtnl_link_unregister(&macvlan_link_ops); 1722b863ceb7SPatrick McHardy unregister_netdevice_notifier(&macvlan_notifier_block); 1723b863ceb7SPatrick McHardy } 1724b863ceb7SPatrick McHardy 1725b863ceb7SPatrick McHardy module_init(macvlan_init_module); 1726b863ceb7SPatrick McHardy module_exit(macvlan_cleanup_module); 1727b863ceb7SPatrick McHardy 1728b863ceb7SPatrick McHardy MODULE_LICENSE("GPL"); 1729b863ceb7SPatrick McHardy MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); 1730b863ceb7SPatrick McHardy MODULE_DESCRIPTION("Driver for MAC address based VLANs"); 1731b863ceb7SPatrick McHardy MODULE_ALIAS_RTNL_LINK("macvlan"); 1732