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 42b863ceb7SPatrick McHardy struct macvlan_port { 43b863ceb7SPatrick McHardy struct net_device *dev; 44b863ceb7SPatrick McHardy struct hlist_head vlan_hash[MACVLAN_HASH_SIZE]; 45b863ceb7SPatrick McHardy struct list_head vlans; 468b37ef0aSJiri Pirko struct rcu_head rcu; 47412ca155SHerbert Xu struct sk_buff_head bc_queue; 48412ca155SHerbert Xu struct work_struct bc_work; 49eb06acdcSSridhar Samudrala bool passthru; 505e3c516bSDavid S. Miller int count; 5179cf79abSMichael Braun struct hlist_head vlan_source_hash[MACVLAN_HASH_SIZE]; 5279cf79abSMichael Braun }; 5379cf79abSMichael Braun 5479cf79abSMichael Braun struct macvlan_source_entry { 5579cf79abSMichael Braun struct hlist_node hlist; 5679cf79abSMichael Braun struct macvlan_dev *vlan; 5779cf79abSMichael Braun unsigned char addr[6+2] __aligned(sizeof(u16)); 5879cf79abSMichael Braun struct rcu_head rcu; 59b863ceb7SPatrick McHardy }; 60b863ceb7SPatrick McHardy 61412ca155SHerbert Xu struct macvlan_skb_cb { 62412ca155SHerbert Xu const struct macvlan_dev *src; 63412ca155SHerbert Xu }; 64412ca155SHerbert Xu 65412ca155SHerbert Xu #define MACVLAN_SKB_CB(__skb) ((struct macvlan_skb_cb *)&((__skb)->cb[0])) 66412ca155SHerbert Xu 67d5cd9244SEric W. Biederman static void macvlan_port_destroy(struct net_device *dev); 68d5cd9244SEric W. Biederman 6979cf79abSMichael Braun /* Hash Ethernet address */ 7079cf79abSMichael Braun static u32 macvlan_eth_hash(const unsigned char *addr) 7179cf79abSMichael Braun { 7279cf79abSMichael Braun u64 value = get_unaligned((u64 *)addr); 7379cf79abSMichael Braun 7479cf79abSMichael Braun /* only want 6 bytes */ 7579cf79abSMichael Braun #ifdef __BIG_ENDIAN 7679cf79abSMichael Braun value >>= 16; 7779cf79abSMichael Braun #else 7879cf79abSMichael Braun value <<= 16; 7979cf79abSMichael Braun #endif 8079cf79abSMichael Braun return hash_64(value, MACVLAN_HASH_BITS); 8179cf79abSMichael Braun } 8279cf79abSMichael Braun 83e052f7e6SEric Dumazet static struct macvlan_port *macvlan_port_get_rcu(const struct net_device *dev) 84e052f7e6SEric Dumazet { 85e052f7e6SEric Dumazet return rcu_dereference(dev->rx_handler_data); 86e052f7e6SEric Dumazet } 87e052f7e6SEric Dumazet 88e052f7e6SEric Dumazet static struct macvlan_port *macvlan_port_get_rtnl(const struct net_device *dev) 89e052f7e6SEric Dumazet { 90e052f7e6SEric Dumazet return rtnl_dereference(dev->rx_handler_data); 91e052f7e6SEric Dumazet } 92e052f7e6SEric Dumazet 93a35e2c1bSJiri Pirko #define macvlan_port_exists(dev) (dev->priv_flags & IFF_MACVLAN_PORT) 94a35e2c1bSJiri Pirko 95b863ceb7SPatrick McHardy static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port, 96b863ceb7SPatrick McHardy const unsigned char *addr) 97b863ceb7SPatrick McHardy { 98b863ceb7SPatrick McHardy struct macvlan_dev *vlan; 9979cf79abSMichael Braun u32 idx = macvlan_eth_hash(addr); 100b863ceb7SPatrick McHardy 10179cf79abSMichael Braun hlist_for_each_entry_rcu(vlan, &port->vlan_hash[idx], hlist) { 102a6700db1SJoe Perches if (ether_addr_equal_64bits(vlan->dev->dev_addr, addr)) 103b863ceb7SPatrick McHardy return vlan; 104b863ceb7SPatrick McHardy } 105b863ceb7SPatrick McHardy return NULL; 106b863ceb7SPatrick McHardy } 107b863ceb7SPatrick McHardy 10879cf79abSMichael Braun static struct macvlan_source_entry *macvlan_hash_lookup_source( 10979cf79abSMichael Braun const struct macvlan_dev *vlan, 11079cf79abSMichael Braun const unsigned char *addr) 11179cf79abSMichael Braun { 11279cf79abSMichael Braun struct macvlan_source_entry *entry; 11379cf79abSMichael Braun u32 idx = macvlan_eth_hash(addr); 11479cf79abSMichael Braun struct hlist_head *h = &vlan->port->vlan_source_hash[idx]; 11579cf79abSMichael Braun 11679cf79abSMichael Braun hlist_for_each_entry_rcu(entry, h, hlist) { 11779cf79abSMichael Braun if (ether_addr_equal_64bits(entry->addr, addr) && 11879cf79abSMichael Braun entry->vlan == vlan) 11979cf79abSMichael Braun return entry; 12079cf79abSMichael Braun } 12179cf79abSMichael Braun return NULL; 12279cf79abSMichael Braun } 12379cf79abSMichael Braun 12479cf79abSMichael Braun static int macvlan_hash_add_source(struct macvlan_dev *vlan, 12579cf79abSMichael Braun const unsigned char *addr) 12679cf79abSMichael Braun { 12779cf79abSMichael Braun struct macvlan_port *port = vlan->port; 12879cf79abSMichael Braun struct macvlan_source_entry *entry; 12979cf79abSMichael Braun struct hlist_head *h; 13079cf79abSMichael Braun 13179cf79abSMichael Braun entry = macvlan_hash_lookup_source(vlan, addr); 13279cf79abSMichael Braun if (entry) 13379cf79abSMichael Braun return 0; 13479cf79abSMichael Braun 13579cf79abSMichael Braun entry = kmalloc(sizeof(*entry), GFP_KERNEL); 13679cf79abSMichael Braun if (!entry) 13779cf79abSMichael Braun return -ENOMEM; 13879cf79abSMichael Braun 13979cf79abSMichael Braun ether_addr_copy(entry->addr, addr); 14079cf79abSMichael Braun entry->vlan = vlan; 14179cf79abSMichael Braun h = &port->vlan_source_hash[macvlan_eth_hash(addr)]; 14279cf79abSMichael Braun hlist_add_head_rcu(&entry->hlist, h); 14379cf79abSMichael Braun vlan->macaddr_count++; 14479cf79abSMichael Braun 14579cf79abSMichael Braun return 0; 14679cf79abSMichael Braun } 14779cf79abSMichael Braun 148f9ac30f0SEric Biederman static void macvlan_hash_add(struct macvlan_dev *vlan) 149f9ac30f0SEric Biederman { 150f9ac30f0SEric Biederman struct macvlan_port *port = vlan->port; 151f9ac30f0SEric Biederman const unsigned char *addr = vlan->dev->dev_addr; 15279cf79abSMichael Braun u32 idx = macvlan_eth_hash(addr); 153f9ac30f0SEric Biederman 15479cf79abSMichael Braun hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[idx]); 15579cf79abSMichael Braun } 15679cf79abSMichael Braun 15779cf79abSMichael Braun static void macvlan_hash_del_source(struct macvlan_source_entry *entry) 15879cf79abSMichael Braun { 15979cf79abSMichael Braun hlist_del_rcu(&entry->hlist); 16079cf79abSMichael Braun kfree_rcu(entry, rcu); 161f9ac30f0SEric Biederman } 162f9ac30f0SEric Biederman 163449f4544SEric Dumazet static void macvlan_hash_del(struct macvlan_dev *vlan, bool sync) 164f9ac30f0SEric Biederman { 165f9ac30f0SEric Biederman hlist_del_rcu(&vlan->hlist); 166449f4544SEric Dumazet if (sync) 167f9ac30f0SEric Biederman synchronize_rcu(); 168f9ac30f0SEric Biederman } 169f9ac30f0SEric Biederman 170f9ac30f0SEric Biederman static void macvlan_hash_change_addr(struct macvlan_dev *vlan, 171f9ac30f0SEric Biederman const unsigned char *addr) 172f9ac30f0SEric Biederman { 173449f4544SEric Dumazet macvlan_hash_del(vlan, true); 174f9ac30f0SEric Biederman /* Now that we are unhashed it is safe to change the device 175f9ac30f0SEric Biederman * address without confusing packet delivery. 176f9ac30f0SEric Biederman */ 177f9ac30f0SEric Biederman memcpy(vlan->dev->dev_addr, addr, ETH_ALEN); 178f9ac30f0SEric Biederman macvlan_hash_add(vlan); 179f9ac30f0SEric Biederman } 180f9ac30f0SEric Biederman 181f9ac30f0SEric Biederman static int macvlan_addr_busy(const struct macvlan_port *port, 182f9ac30f0SEric Biederman const unsigned char *addr) 183f9ac30f0SEric Biederman { 184f9ac30f0SEric Biederman /* Test to see if the specified multicast address is 185f9ac30f0SEric Biederman * currently in use by the underlying device or 186f9ac30f0SEric Biederman * another macvlan. 187f9ac30f0SEric Biederman */ 188a6700db1SJoe Perches if (ether_addr_equal_64bits(port->dev->dev_addr, addr)) 189f9ac30f0SEric Biederman return 1; 190f9ac30f0SEric Biederman 191f9ac30f0SEric Biederman if (macvlan_hash_lookup(port, addr)) 192f9ac30f0SEric Biederman return 1; 193f9ac30f0SEric Biederman 194f9ac30f0SEric Biederman return 0; 195f9ac30f0SEric Biederman } 196f9ac30f0SEric Biederman 197a1e514c5SArnd Bergmann 198fc0663d6SArnd Bergmann static int macvlan_broadcast_one(struct sk_buff *skb, 199fc0663d6SArnd Bergmann const struct macvlan_dev *vlan, 200618e1b74SArnd Bergmann const struct ethhdr *eth, bool local) 201a1e514c5SArnd Bergmann { 202fc0663d6SArnd Bergmann struct net_device *dev = vlan->dev; 203a1e514c5SArnd Bergmann 204618e1b74SArnd Bergmann if (local) 205412ca155SHerbert Xu return __dev_forward_skb(dev, skb); 206618e1b74SArnd Bergmann 207a1e514c5SArnd Bergmann skb->dev = dev; 208a6700db1SJoe Perches if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast)) 209a1e514c5SArnd Bergmann skb->pkt_type = PACKET_BROADCAST; 210a1e514c5SArnd Bergmann else 211a1e514c5SArnd Bergmann skb->pkt_type = PACKET_MULTICAST; 212a1e514c5SArnd Bergmann 213412ca155SHerbert Xu return 0; 214a1e514c5SArnd Bergmann } 215a1e514c5SArnd Bergmann 2163807ff58SEric Dumazet static u32 macvlan_hash_mix(const struct macvlan_dev *vlan) 2173807ff58SEric Dumazet { 2183807ff58SEric Dumazet return (u32)(((unsigned long)vlan) >> L1_CACHE_SHIFT); 2193807ff58SEric Dumazet } 2203807ff58SEric Dumazet 2213807ff58SEric Dumazet 2223807ff58SEric Dumazet static unsigned int mc_hash(const struct macvlan_dev *vlan, 2233807ff58SEric Dumazet const unsigned char *addr) 224cd431e73SEric Dumazet { 225cd431e73SEric Dumazet u32 val = __get_unaligned_cpu32(addr + 2); 226cd431e73SEric Dumazet 2273807ff58SEric Dumazet val ^= macvlan_hash_mix(vlan); 228cd431e73SEric Dumazet return hash_32(val, MACVLAN_MC_FILTER_BITS); 229cd431e73SEric Dumazet } 230cd431e73SEric Dumazet 231b863ceb7SPatrick McHardy static void macvlan_broadcast(struct sk_buff *skb, 232618e1b74SArnd Bergmann const struct macvlan_port *port, 233618e1b74SArnd Bergmann struct net_device *src, 234618e1b74SArnd Bergmann enum macvlan_mode mode) 235b863ceb7SPatrick McHardy { 236b863ceb7SPatrick McHardy const struct ethhdr *eth = eth_hdr(skb); 237b863ceb7SPatrick McHardy const struct macvlan_dev *vlan; 238b863ceb7SPatrick McHardy struct sk_buff *nskb; 239b863ceb7SPatrick McHardy unsigned int i; 240a1e514c5SArnd Bergmann int err; 2413807ff58SEric Dumazet unsigned int hash; 242b863ceb7SPatrick McHardy 243efbbced3SPatrick McHardy if (skb->protocol == htons(ETH_P_PAUSE)) 244efbbced3SPatrick McHardy return; 245efbbced3SPatrick McHardy 246b863ceb7SPatrick McHardy for (i = 0; i < MACVLAN_HASH_SIZE; i++) { 247b67bfe0dSSasha Levin hlist_for_each_entry_rcu(vlan, &port->vlan_hash[i], hlist) { 248618e1b74SArnd Bergmann if (vlan->dev == src || !(vlan->mode & mode)) 249618e1b74SArnd Bergmann continue; 250618e1b74SArnd Bergmann 2513807ff58SEric Dumazet hash = mc_hash(vlan, eth->h_dest); 252cd431e73SEric Dumazet if (!test_bit(hash, vlan->mc_filter)) 253cd431e73SEric Dumazet continue; 254de9e8f3fSHerbert Xu 255de9e8f3fSHerbert Xu err = NET_RX_DROP; 256b863ceb7SPatrick McHardy nskb = skb_clone(skb, GFP_ATOMIC); 257de9e8f3fSHerbert Xu if (likely(nskb)) 258de9e8f3fSHerbert Xu err = macvlan_broadcast_one( 259de9e8f3fSHerbert Xu nskb, vlan, eth, 260412ca155SHerbert Xu mode == MACVLAN_MODE_BRIDGE) ?: 261412ca155SHerbert Xu netif_rx_ni(nskb); 262a1e514c5SArnd Bergmann macvlan_count_rx(vlan, skb->len + ETH_HLEN, 2634c979935Sjbaron@akamai.com err == NET_RX_SUCCESS, true); 264b863ceb7SPatrick McHardy } 265b863ceb7SPatrick McHardy } 266b863ceb7SPatrick McHardy } 267b863ceb7SPatrick McHardy 268412ca155SHerbert Xu static void macvlan_process_broadcast(struct work_struct *w) 269412ca155SHerbert Xu { 270412ca155SHerbert Xu struct macvlan_port *port = container_of(w, struct macvlan_port, 271412ca155SHerbert Xu bc_work); 272412ca155SHerbert Xu struct sk_buff *skb; 273412ca155SHerbert Xu struct sk_buff_head list; 274412ca155SHerbert Xu 275*fe0ca732SEric Dumazet __skb_queue_head_init(&list); 276412ca155SHerbert Xu 277412ca155SHerbert Xu spin_lock_bh(&port->bc_queue.lock); 278412ca155SHerbert Xu skb_queue_splice_tail_init(&port->bc_queue, &list); 279412ca155SHerbert Xu spin_unlock_bh(&port->bc_queue.lock); 280412ca155SHerbert Xu 281412ca155SHerbert Xu while ((skb = __skb_dequeue(&list))) { 282412ca155SHerbert Xu const struct macvlan_dev *src = MACVLAN_SKB_CB(skb)->src; 283412ca155SHerbert Xu 284412ca155SHerbert Xu rcu_read_lock(); 285412ca155SHerbert Xu 286412ca155SHerbert Xu if (!src) 287412ca155SHerbert Xu /* frame comes from an external address */ 288412ca155SHerbert Xu macvlan_broadcast(skb, port, NULL, 289412ca155SHerbert Xu MACVLAN_MODE_PRIVATE | 290412ca155SHerbert Xu MACVLAN_MODE_VEPA | 291412ca155SHerbert Xu MACVLAN_MODE_PASSTHRU| 292412ca155SHerbert Xu MACVLAN_MODE_BRIDGE); 293412ca155SHerbert Xu else if (src->mode == MACVLAN_MODE_VEPA) 294412ca155SHerbert Xu /* flood to everyone except source */ 295412ca155SHerbert Xu macvlan_broadcast(skb, port, src->dev, 296412ca155SHerbert Xu MACVLAN_MODE_VEPA | 297412ca155SHerbert Xu MACVLAN_MODE_BRIDGE); 298412ca155SHerbert Xu else 299412ca155SHerbert Xu /* 300412ca155SHerbert Xu * flood only to VEPA ports, bridge ports 301412ca155SHerbert Xu * already saw the frame on the way out. 302412ca155SHerbert Xu */ 303412ca155SHerbert Xu macvlan_broadcast(skb, port, src->dev, 304412ca155SHerbert Xu MACVLAN_MODE_VEPA); 305412ca155SHerbert Xu 306412ca155SHerbert Xu rcu_read_unlock(); 307412ca155SHerbert Xu 308412ca155SHerbert Xu kfree_skb(skb); 309412ca155SHerbert Xu } 310412ca155SHerbert Xu } 311412ca155SHerbert Xu 312412ca155SHerbert Xu static void macvlan_broadcast_enqueue(struct macvlan_port *port, 313412ca155SHerbert Xu struct sk_buff *skb) 314412ca155SHerbert Xu { 315e676f197SHerbert Xu struct sk_buff *nskb; 316412ca155SHerbert Xu int err = -ENOMEM; 317412ca155SHerbert Xu 318e676f197SHerbert Xu nskb = skb_clone(skb, GFP_ATOMIC); 319e676f197SHerbert Xu if (!nskb) 320412ca155SHerbert Xu goto err; 321412ca155SHerbert Xu 322412ca155SHerbert Xu spin_lock(&port->bc_queue.lock); 32307d92d5cSNicolas Dichtel if (skb_queue_len(&port->bc_queue) < MACVLAN_BC_QUEUE_LEN) { 324e676f197SHerbert Xu __skb_queue_tail(&port->bc_queue, nskb); 325412ca155SHerbert Xu err = 0; 326412ca155SHerbert Xu } 327412ca155SHerbert Xu spin_unlock(&port->bc_queue.lock); 328412ca155SHerbert Xu 329412ca155SHerbert Xu if (err) 330e676f197SHerbert Xu goto free_nskb; 331412ca155SHerbert Xu 332412ca155SHerbert Xu schedule_work(&port->bc_work); 333412ca155SHerbert Xu return; 334412ca155SHerbert Xu 335e676f197SHerbert Xu free_nskb: 336e676f197SHerbert Xu kfree_skb(nskb); 337412ca155SHerbert Xu err: 338412ca155SHerbert Xu atomic_long_inc(&skb->dev->rx_dropped); 339412ca155SHerbert Xu } 340412ca155SHerbert Xu 34179cf79abSMichael Braun static void macvlan_flush_sources(struct macvlan_port *port, 34279cf79abSMichael Braun struct macvlan_dev *vlan) 34379cf79abSMichael Braun { 34479cf79abSMichael Braun int i; 34579cf79abSMichael Braun 34679cf79abSMichael Braun for (i = 0; i < MACVLAN_HASH_SIZE; i++) { 34779cf79abSMichael Braun struct hlist_node *h, *n; 34879cf79abSMichael Braun 34979cf79abSMichael Braun hlist_for_each_safe(h, n, &port->vlan_source_hash[i]) { 35079cf79abSMichael Braun struct macvlan_source_entry *entry; 35179cf79abSMichael Braun 35279cf79abSMichael Braun entry = hlist_entry(h, struct macvlan_source_entry, 35379cf79abSMichael Braun hlist); 35479cf79abSMichael Braun if (entry->vlan == vlan) 35579cf79abSMichael Braun macvlan_hash_del_source(entry); 35679cf79abSMichael Braun } 35779cf79abSMichael Braun } 35879cf79abSMichael Braun vlan->macaddr_count = 0; 35979cf79abSMichael Braun } 36079cf79abSMichael Braun 36179cf79abSMichael Braun static void macvlan_forward_source_one(struct sk_buff *skb, 36279cf79abSMichael Braun struct macvlan_dev *vlan) 36379cf79abSMichael Braun { 36479cf79abSMichael Braun struct sk_buff *nskb; 36579cf79abSMichael Braun struct net_device *dev; 36679cf79abSMichael Braun int len; 36779cf79abSMichael Braun int ret; 36879cf79abSMichael Braun 36979cf79abSMichael Braun dev = vlan->dev; 37079cf79abSMichael Braun if (unlikely(!(dev->flags & IFF_UP))) 37179cf79abSMichael Braun return; 37279cf79abSMichael Braun 37379cf79abSMichael Braun nskb = skb_clone(skb, GFP_ATOMIC); 37479cf79abSMichael Braun if (!nskb) 37579cf79abSMichael Braun return; 37679cf79abSMichael Braun 37779cf79abSMichael Braun len = nskb->len + ETH_HLEN; 37879cf79abSMichael Braun nskb->dev = dev; 37979cf79abSMichael Braun nskb->pkt_type = PACKET_HOST; 38079cf79abSMichael Braun 38179cf79abSMichael Braun ret = netif_rx(nskb); 3824c979935Sjbaron@akamai.com macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, false); 38379cf79abSMichael Braun } 38479cf79abSMichael Braun 38579cf79abSMichael Braun static void macvlan_forward_source(struct sk_buff *skb, 38679cf79abSMichael Braun struct macvlan_port *port, 38779cf79abSMichael Braun const unsigned char *addr) 38879cf79abSMichael Braun { 38979cf79abSMichael Braun struct macvlan_source_entry *entry; 39079cf79abSMichael Braun u32 idx = macvlan_eth_hash(addr); 39179cf79abSMichael Braun struct hlist_head *h = &port->vlan_source_hash[idx]; 39279cf79abSMichael Braun 39379cf79abSMichael Braun hlist_for_each_entry_rcu(entry, h, hlist) { 39479cf79abSMichael Braun if (ether_addr_equal_64bits(entry->addr, addr)) 39579cf79abSMichael Braun if (entry->vlan->dev->flags & IFF_UP) 39679cf79abSMichael Braun macvlan_forward_source_one(skb, entry->vlan); 39779cf79abSMichael Braun } 39879cf79abSMichael Braun } 39979cf79abSMichael Braun 400b863ceb7SPatrick McHardy /* called under rcu_read_lock() from netif_receive_skb */ 4018a4eb573SJiri Pirko static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb) 402b863ceb7SPatrick McHardy { 403ab95bfe0SJiri Pirko struct macvlan_port *port; 4048a4eb573SJiri Pirko struct sk_buff *skb = *pskb; 405b863ceb7SPatrick McHardy const struct ethhdr *eth = eth_hdr(skb); 406b863ceb7SPatrick McHardy const struct macvlan_dev *vlan; 407618e1b74SArnd Bergmann const struct macvlan_dev *src; 408b863ceb7SPatrick McHardy struct net_device *dev; 409ba01877fSSridhar Samudrala unsigned int len = 0; 410d1dd9119Sjbaron@akamai.com int ret; 411d1dd9119Sjbaron@akamai.com rx_handler_result_t handle_res; 412b863ceb7SPatrick McHardy 413a35e2c1bSJiri Pirko port = macvlan_port_get_rcu(skb->dev); 414b863ceb7SPatrick McHardy if (is_multicast_ether_addr(eth->h_dest)) { 415bc416d97SEric Dumazet skb = ip_check_defrag(skb, IP_DEFRAG_MACVLAN); 416bc416d97SEric Dumazet if (!skb) 417bc416d97SEric Dumazet return RX_HANDLER_CONSUMED; 4184ec7ac12SEric Dumazet eth = eth_hdr(skb); 41979cf79abSMichael Braun macvlan_forward_source(skb, port, eth->h_source); 420618e1b74SArnd Bergmann src = macvlan_hash_lookup(port, eth->h_source); 421412ca155SHerbert Xu if (src && src->mode != MACVLAN_MODE_VEPA && 422412ca155SHerbert Xu src->mode != MACVLAN_MODE_BRIDGE) { 423729e72a1Sstephen hemminger /* forward to original port. */ 424729e72a1Sstephen hemminger vlan = src; 425412ca155SHerbert Xu ret = macvlan_broadcast_one(skb, vlan, eth, 0) ?: 426412ca155SHerbert Xu netif_rx(skb); 427d1dd9119Sjbaron@akamai.com handle_res = RX_HANDLER_CONSUMED; 428729e72a1Sstephen hemminger goto out; 429729e72a1Sstephen hemminger } 430729e72a1Sstephen hemminger 431412ca155SHerbert Xu MACVLAN_SKB_CB(skb)->src = src; 432412ca155SHerbert Xu macvlan_broadcast_enqueue(port, skb); 433412ca155SHerbert Xu 4348a4eb573SJiri Pirko return RX_HANDLER_PASS; 435b863ceb7SPatrick McHardy } 436b863ceb7SPatrick McHardy 43779cf79abSMichael Braun macvlan_forward_source(skb, port, eth->h_source); 438eb06acdcSSridhar Samudrala if (port->passthru) 439233c7df0SJiri Pirko vlan = list_first_or_null_rcu(&port->vlans, 440233c7df0SJiri Pirko struct macvlan_dev, list); 441eb06acdcSSridhar Samudrala else 442b863ceb7SPatrick McHardy vlan = macvlan_hash_lookup(port, eth->h_dest); 443b863ceb7SPatrick McHardy if (vlan == NULL) 4448a4eb573SJiri Pirko return RX_HANDLER_PASS; 445b863ceb7SPatrick McHardy 446b863ceb7SPatrick McHardy dev = vlan->dev; 447b863ceb7SPatrick McHardy if (unlikely(!(dev->flags & IFF_UP))) { 448b863ceb7SPatrick McHardy kfree_skb(skb); 4498a4eb573SJiri Pirko return RX_HANDLER_CONSUMED; 450b863ceb7SPatrick McHardy } 451a1e514c5SArnd Bergmann len = skb->len + ETH_HLEN; 452b863ceb7SPatrick McHardy skb = skb_share_check(skb, GFP_ATOMIC); 453d1dd9119Sjbaron@akamai.com if (!skb) { 454d1dd9119Sjbaron@akamai.com ret = NET_RX_DROP; 455d1dd9119Sjbaron@akamai.com handle_res = RX_HANDLER_CONSUMED; 456ba01877fSSridhar Samudrala goto out; 457d1dd9119Sjbaron@akamai.com } 458b863ceb7SPatrick McHardy 459b863ceb7SPatrick McHardy skb->dev = dev; 460b863ceb7SPatrick McHardy skb->pkt_type = PACKET_HOST; 461b863ceb7SPatrick McHardy 462d1dd9119Sjbaron@akamai.com ret = NET_RX_SUCCESS; 463d1dd9119Sjbaron@akamai.com handle_res = RX_HANDLER_ANOTHER; 464ba01877fSSridhar Samudrala out: 4654c979935Sjbaron@akamai.com macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, false); 466d1dd9119Sjbaron@akamai.com return handle_res; 467b863ceb7SPatrick McHardy } 468b863ceb7SPatrick McHardy 469618e1b74SArnd Bergmann static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev) 470618e1b74SArnd Bergmann { 471618e1b74SArnd Bergmann const struct macvlan_dev *vlan = netdev_priv(dev); 472618e1b74SArnd Bergmann const struct macvlan_port *port = vlan->port; 473618e1b74SArnd Bergmann const struct macvlan_dev *dest; 474618e1b74SArnd Bergmann 475618e1b74SArnd Bergmann if (vlan->mode == MACVLAN_MODE_BRIDGE) { 476618e1b74SArnd Bergmann const struct ethhdr *eth = (void *)skb->data; 477618e1b74SArnd Bergmann 478618e1b74SArnd Bergmann /* send to other bridge ports directly */ 479618e1b74SArnd Bergmann if (is_multicast_ether_addr(eth->h_dest)) { 480618e1b74SArnd Bergmann macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE); 481618e1b74SArnd Bergmann goto xmit_world; 482618e1b74SArnd Bergmann } 483618e1b74SArnd Bergmann 484618e1b74SArnd Bergmann dest = macvlan_hash_lookup(port, eth->h_dest); 485618e1b74SArnd Bergmann if (dest && dest->mode == MACVLAN_MODE_BRIDGE) { 486a37dd333SDavid Ward /* send to lowerdev first for its network taps */ 487cb2d0f3eSDavid Ward dev_forward_skb(vlan->lowerdev, skb); 488618e1b74SArnd Bergmann 489618e1b74SArnd Bergmann return NET_XMIT_SUCCESS; 490618e1b74SArnd Bergmann } 491618e1b74SArnd Bergmann } 492618e1b74SArnd Bergmann 493618e1b74SArnd Bergmann xmit_world: 49459b9997bSDavid S. Miller skb->dev = vlan->lowerdev; 495618e1b74SArnd Bergmann return dev_queue_xmit(skb); 496618e1b74SArnd Bergmann } 497618e1b74SArnd Bergmann 498688cea83Sdingtianhong static inline netdev_tx_t macvlan_netpoll_send_skb(struct macvlan_dev *vlan, struct sk_buff *skb) 499688cea83Sdingtianhong { 500688cea83Sdingtianhong #ifdef CONFIG_NET_POLL_CONTROLLER 501688cea83Sdingtianhong if (vlan->netpoll) 502688cea83Sdingtianhong netpoll_send_skb(vlan->netpoll, skb); 503688cea83Sdingtianhong #else 504688cea83Sdingtianhong BUG(); 505688cea83Sdingtianhong #endif 506688cea83Sdingtianhong return NETDEV_TX_OK; 507688cea83Sdingtianhong } 508688cea83Sdingtianhong 5090db901bdSstephen hemminger static netdev_tx_t macvlan_start_xmit(struct sk_buff *skb, 510424efe9cSStephen Hemminger struct net_device *dev) 511b863ceb7SPatrick McHardy { 512b863ceb7SPatrick McHardy unsigned int len = skb->len; 513b863ceb7SPatrick McHardy int ret; 514688cea83Sdingtianhong struct macvlan_dev *vlan = netdev_priv(dev); 515688cea83Sdingtianhong 516688cea83Sdingtianhong if (unlikely(netpoll_tx_running(dev))) 517688cea83Sdingtianhong return macvlan_netpoll_send_skb(vlan, skb); 518b863ceb7SPatrick McHardy 519a6cc0cfaSJohn Fastabend if (vlan->fwd_priv) { 520a6cc0cfaSJohn Fastabend skb->dev = vlan->lowerdev; 521f663dd9aSJason Wang ret = dev_queue_xmit_accel(skb, vlan->fwd_priv); 522a6cc0cfaSJohn Fastabend } else { 523618e1b74SArnd Bergmann ret = macvlan_queue_xmit(skb, dev); 524a6cc0cfaSJohn Fastabend } 525a6cc0cfaSJohn Fastabend 5262d6c9ffcSEric Dumazet if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) { 527cdf3e274SLi RongQing struct vlan_pcpu_stats *pcpu_stats; 5282c114553SEric Dumazet 5298ffab51bSEric Dumazet pcpu_stats = this_cpu_ptr(vlan->pcpu_stats); 5308ffab51bSEric Dumazet u64_stats_update_begin(&pcpu_stats->syncp); 5318ffab51bSEric Dumazet pcpu_stats->tx_packets++; 5328ffab51bSEric Dumazet pcpu_stats->tx_bytes += len; 5338ffab51bSEric Dumazet u64_stats_update_end(&pcpu_stats->syncp); 5348ffab51bSEric Dumazet } else { 5358ffab51bSEric Dumazet this_cpu_inc(vlan->pcpu_stats->tx_dropped); 5368ffab51bSEric Dumazet } 537cbbef5e1SPatrick McHardy return ret; 538b863ceb7SPatrick McHardy } 539b863ceb7SPatrick McHardy 540b863ceb7SPatrick McHardy static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev, 5413b04dddeSStephen Hemminger unsigned short type, const void *daddr, 5423b04dddeSStephen Hemminger const void *saddr, unsigned len) 543b863ceb7SPatrick McHardy { 544b863ceb7SPatrick McHardy const struct macvlan_dev *vlan = netdev_priv(dev); 545b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 546b863ceb7SPatrick McHardy 5470c4e8581SStephen Hemminger return dev_hard_header(skb, lowerdev, type, daddr, 548b863ceb7SPatrick McHardy saddr ? : dev->dev_addr, len); 549b863ceb7SPatrick McHardy } 550b863ceb7SPatrick McHardy 5513b04dddeSStephen Hemminger static const struct header_ops macvlan_hard_header_ops = { 5523b04dddeSStephen Hemminger .create = macvlan_hard_header, 5533b04dddeSStephen Hemminger .rebuild = eth_rebuild_header, 5543b04dddeSStephen Hemminger .parse = eth_header_parse, 5553b04dddeSStephen Hemminger .cache = eth_header_cache, 5563b04dddeSStephen Hemminger .cache_update = eth_header_cache_update, 5573b04dddeSStephen Hemminger }; 5583b04dddeSStephen Hemminger 559b13ba1b8SJason Wang static struct rtnl_link_ops macvlan_link_ops; 560b13ba1b8SJason Wang 561b863ceb7SPatrick McHardy static int macvlan_open(struct net_device *dev) 562b863ceb7SPatrick McHardy { 563b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 564b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 565b863ceb7SPatrick McHardy int err; 566b863ceb7SPatrick McHardy 567eb06acdcSSridhar Samudrala if (vlan->port->passthru) { 56878738141SMichael S. Tsirkin if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) { 56978738141SMichael S. Tsirkin err = dev_set_promiscuity(lowerdev, 1); 57078738141SMichael S. Tsirkin if (err < 0) 57178738141SMichael S. Tsirkin goto out; 57278738141SMichael S. Tsirkin } 573eb06acdcSSridhar Samudrala goto hash_add; 574eb06acdcSSridhar Samudrala } 575eb06acdcSSridhar Samudrala 576b13ba1b8SJason Wang if (lowerdev->features & NETIF_F_HW_L2FW_DOFFLOAD && 577b13ba1b8SJason Wang dev->rtnl_link_ops == &macvlan_link_ops) { 578a6cc0cfaSJohn Fastabend vlan->fwd_priv = 579a6cc0cfaSJohn Fastabend lowerdev->netdev_ops->ndo_dfwd_add_station(lowerdev, dev); 580a6cc0cfaSJohn Fastabend 581a6cc0cfaSJohn Fastabend /* If we get a NULL pointer back, or if we get an error 582a6cc0cfaSJohn Fastabend * then we should just fall through to the non accelerated path 583a6cc0cfaSJohn Fastabend */ 584a6cc0cfaSJohn Fastabend if (IS_ERR_OR_NULL(vlan->fwd_priv)) { 585a6cc0cfaSJohn Fastabend vlan->fwd_priv = NULL; 586f663dd9aSJason Wang } else 587a6cc0cfaSJohn Fastabend return 0; 588a6cc0cfaSJohn Fastabend } 589a6cc0cfaSJohn Fastabend 590f9ac30f0SEric Biederman err = -EBUSY; 591f9ac30f0SEric Biederman if (macvlan_addr_busy(vlan->port, dev->dev_addr)) 592f9ac30f0SEric Biederman goto out; 593f9ac30f0SEric Biederman 594a748ee24SJiri Pirko err = dev_uc_add(lowerdev, dev->dev_addr); 595b863ceb7SPatrick McHardy if (err < 0) 596b89fb7daSWang Chen goto out; 597b89fb7daSWang Chen if (dev->flags & IFF_ALLMULTI) { 598b89fb7daSWang Chen err = dev_set_allmulti(lowerdev, 1); 599b89fb7daSWang Chen if (err < 0) 600b89fb7daSWang Chen goto del_unicast; 601b89fb7daSWang Chen } 602eb06acdcSSridhar Samudrala 603eb06acdcSSridhar Samudrala hash_add: 604f9ac30f0SEric Biederman macvlan_hash_add(vlan); 605b863ceb7SPatrick McHardy return 0; 606b89fb7daSWang Chen 607b89fb7daSWang Chen del_unicast: 608a748ee24SJiri Pirko dev_uc_del(lowerdev, dev->dev_addr); 609b89fb7daSWang Chen out: 610a6cc0cfaSJohn Fastabend if (vlan->fwd_priv) { 611a6cc0cfaSJohn Fastabend lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev, 612a6cc0cfaSJohn Fastabend vlan->fwd_priv); 613a6cc0cfaSJohn Fastabend vlan->fwd_priv = NULL; 614a6cc0cfaSJohn Fastabend } 615b89fb7daSWang Chen return err; 616b863ceb7SPatrick McHardy } 617b863ceb7SPatrick McHardy 618b863ceb7SPatrick McHardy static int macvlan_stop(struct net_device *dev) 619b863ceb7SPatrick McHardy { 620b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 621b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 622b863ceb7SPatrick McHardy 623a6cc0cfaSJohn Fastabend if (vlan->fwd_priv) { 624a6cc0cfaSJohn Fastabend lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev, 625a6cc0cfaSJohn Fastabend vlan->fwd_priv); 626a6cc0cfaSJohn Fastabend vlan->fwd_priv = NULL; 627a6cc0cfaSJohn Fastabend return 0; 628a6cc0cfaSJohn Fastabend } 629a6cc0cfaSJohn Fastabend 630df8ef8f3SJohn Fastabend dev_uc_unsync(lowerdev, dev); 631df8ef8f3SJohn Fastabend dev_mc_unsync(lowerdev, dev); 632df8ef8f3SJohn Fastabend 633eb06acdcSSridhar Samudrala if (vlan->port->passthru) { 634df8ef8f3SJohn Fastabend if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) 635eb06acdcSSridhar Samudrala dev_set_promiscuity(lowerdev, -1); 636eb06acdcSSridhar Samudrala goto hash_del; 637eb06acdcSSridhar Samudrala } 638eb06acdcSSridhar Samudrala 639b863ceb7SPatrick McHardy if (dev->flags & IFF_ALLMULTI) 640b863ceb7SPatrick McHardy dev_set_allmulti(lowerdev, -1); 641b863ceb7SPatrick McHardy 642a748ee24SJiri Pirko dev_uc_del(lowerdev, dev->dev_addr); 643b863ceb7SPatrick McHardy 644eb06acdcSSridhar Samudrala hash_del: 645449f4544SEric Dumazet macvlan_hash_del(vlan, !dev->dismantle); 646b863ceb7SPatrick McHardy return 0; 647b863ceb7SPatrick McHardy } 648b863ceb7SPatrick McHardy 649e289fd28Sdingtianhong static int macvlan_sync_address(struct net_device *dev, unsigned char *addr) 650ad5d20a6SPatrick McHardy { 651ad5d20a6SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 652ad5d20a6SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 653ad5d20a6SPatrick McHardy int err; 654ad5d20a6SPatrick McHardy 655f9ac30f0SEric Biederman if (!(dev->flags & IFF_UP)) { 656f9ac30f0SEric Biederman /* Just copy in the new address */ 657e289fd28Sdingtianhong ether_addr_copy(dev->dev_addr, addr); 658f9ac30f0SEric Biederman } else { 659f9ac30f0SEric Biederman /* Rehash and update the device filters */ 660e289fd28Sdingtianhong if (macvlan_addr_busy(vlan->port, addr)) 661f9ac30f0SEric Biederman return -EBUSY; 662ad5d20a6SPatrick McHardy 663e289fd28Sdingtianhong if (!vlan->port->passthru) { 664e289fd28Sdingtianhong err = dev_uc_add(lowerdev, addr); 665ccffad25SJiri Pirko if (err) 666ad5d20a6SPatrick McHardy return err; 667f9ac30f0SEric Biederman 668a748ee24SJiri Pirko dev_uc_del(lowerdev, dev->dev_addr); 669e289fd28Sdingtianhong } 670ad5d20a6SPatrick McHardy 671e289fd28Sdingtianhong macvlan_hash_change_addr(vlan, addr); 672f9ac30f0SEric Biederman } 673ad5d20a6SPatrick McHardy return 0; 674ad5d20a6SPatrick McHardy } 675ad5d20a6SPatrick McHardy 676e289fd28Sdingtianhong static int macvlan_set_mac_address(struct net_device *dev, void *p) 677e289fd28Sdingtianhong { 678e289fd28Sdingtianhong struct macvlan_dev *vlan = netdev_priv(dev); 679e289fd28Sdingtianhong struct sockaddr *addr = p; 680e289fd28Sdingtianhong 681e289fd28Sdingtianhong if (!is_valid_ether_addr(addr->sa_data)) 682e289fd28Sdingtianhong return -EADDRNOTAVAIL; 683e289fd28Sdingtianhong 684e289fd28Sdingtianhong if (vlan->mode == MACVLAN_MODE_PASSTHRU) { 685e289fd28Sdingtianhong dev_set_mac_address(vlan->lowerdev, addr); 686e289fd28Sdingtianhong return 0; 687e289fd28Sdingtianhong } 688e289fd28Sdingtianhong 689e289fd28Sdingtianhong return macvlan_sync_address(dev, addr->sa_data); 690e289fd28Sdingtianhong } 691e289fd28Sdingtianhong 692b863ceb7SPatrick McHardy static void macvlan_change_rx_flags(struct net_device *dev, int change) 693b863ceb7SPatrick McHardy { 694b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 695b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 696b863ceb7SPatrick McHardy 697bbeb0eadSPeter Christensen if (dev->flags & IFF_UP) { 698b863ceb7SPatrick McHardy if (change & IFF_ALLMULTI) 699b863ceb7SPatrick McHardy dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1); 700b863ceb7SPatrick McHardy } 701bbeb0eadSPeter Christensen } 702b863ceb7SPatrick McHardy 703df8ef8f3SJohn Fastabend static void macvlan_set_mac_lists(struct net_device *dev) 704b863ceb7SPatrick McHardy { 705b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 706b863ceb7SPatrick McHardy 707cd431e73SEric Dumazet if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) { 708cd431e73SEric Dumazet bitmap_fill(vlan->mc_filter, MACVLAN_MC_FILTER_SZ); 709cd431e73SEric Dumazet } else { 710cd431e73SEric Dumazet struct netdev_hw_addr *ha; 711cd431e73SEric Dumazet DECLARE_BITMAP(filter, MACVLAN_MC_FILTER_SZ); 712cd431e73SEric Dumazet 713cd431e73SEric Dumazet bitmap_zero(filter, MACVLAN_MC_FILTER_SZ); 714cd431e73SEric Dumazet netdev_for_each_mc_addr(ha, dev) { 7153807ff58SEric Dumazet __set_bit(mc_hash(vlan, ha->addr), filter); 716cd431e73SEric Dumazet } 717d5270430SEric Dumazet 7183807ff58SEric Dumazet __set_bit(mc_hash(vlan, dev->broadcast), filter); 719d5270430SEric Dumazet 720cd431e73SEric Dumazet bitmap_copy(vlan->mc_filter, filter, MACVLAN_MC_FILTER_SZ); 721cd431e73SEric Dumazet } 722df8ef8f3SJohn Fastabend dev_uc_sync(vlan->lowerdev, dev); 723b863ceb7SPatrick McHardy dev_mc_sync(vlan->lowerdev, dev); 724b863ceb7SPatrick McHardy } 725b863ceb7SPatrick McHardy 726b863ceb7SPatrick McHardy static int macvlan_change_mtu(struct net_device *dev, int new_mtu) 727b863ceb7SPatrick McHardy { 728b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 729b863ceb7SPatrick McHardy 730b863ceb7SPatrick McHardy if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu) 731b863ceb7SPatrick McHardy return -EINVAL; 732b863ceb7SPatrick McHardy dev->mtu = new_mtu; 733b863ceb7SPatrick McHardy return 0; 734b863ceb7SPatrick McHardy } 735b863ceb7SPatrick McHardy 736b863ceb7SPatrick McHardy /* 737b863ceb7SPatrick McHardy * macvlan network devices have devices nesting below it and are a special 738b863ceb7SPatrick McHardy * "super class" of normal network devices; split their locks off into a 739b863ceb7SPatrick McHardy * separate class since they always nest. 740b863ceb7SPatrick McHardy */ 741b863ceb7SPatrick McHardy static struct lock_class_key macvlan_netdev_xmit_lock_key; 742cf508b12SDavid S. Miller static struct lock_class_key macvlan_netdev_addr_lock_key; 743b863ceb7SPatrick McHardy 7448b4703e9SVlad Yasevich #define ALWAYS_ON_FEATURES \ 7458b4703e9SVlad Yasevich (NETIF_F_SG | NETIF_F_GEN_CSUM | NETIF_F_GSO_SOFTWARE | NETIF_F_LLTX) 7468b4703e9SVlad Yasevich 747b863ceb7SPatrick McHardy #define MACVLAN_FEATURES \ 748b863ceb7SPatrick McHardy (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \ 749b863ceb7SPatrick McHardy NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \ 7508d13e670SJohn Fastabend NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \ 75128d2b136SPatrick McHardy NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER) 752b863ceb7SPatrick McHardy 753b863ceb7SPatrick McHardy #define MACVLAN_STATE_MASK \ 754b863ceb7SPatrick McHardy ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT)) 755b863ceb7SPatrick McHardy 756c674ac30SVlad Yasevich static int macvlan_get_nest_level(struct net_device *dev) 757c674ac30SVlad Yasevich { 758c674ac30SVlad Yasevich return ((struct macvlan_dev *)netdev_priv(dev))->nest_level; 759c674ac30SVlad Yasevich } 760c674ac30SVlad Yasevich 761e8a0464cSDavid S. Miller static void macvlan_set_lockdep_class_one(struct net_device *dev, 762e8a0464cSDavid S. Miller struct netdev_queue *txq, 763e8a0464cSDavid S. Miller void *_unused) 764c773e847SDavid S. Miller { 765c773e847SDavid S. Miller lockdep_set_class(&txq->_xmit_lock, 766c773e847SDavid S. Miller &macvlan_netdev_xmit_lock_key); 767c773e847SDavid S. Miller } 768c773e847SDavid S. Miller 769c773e847SDavid S. Miller static void macvlan_set_lockdep_class(struct net_device *dev) 770c773e847SDavid S. Miller { 771c674ac30SVlad Yasevich lockdep_set_class_and_subclass(&dev->addr_list_lock, 772c674ac30SVlad Yasevich &macvlan_netdev_addr_lock_key, 773c674ac30SVlad Yasevich macvlan_get_nest_level(dev)); 774e8a0464cSDavid S. Miller netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL); 775c773e847SDavid S. Miller } 776c773e847SDavid S. Miller 777b863ceb7SPatrick McHardy static int macvlan_init(struct net_device *dev) 778b863ceb7SPatrick McHardy { 779b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 780b863ceb7SPatrick McHardy const struct net_device *lowerdev = vlan->lowerdev; 781b863ceb7SPatrick McHardy 782b863ceb7SPatrick McHardy dev->state = (dev->state & ~MACVLAN_STATE_MASK) | 783b863ceb7SPatrick McHardy (lowerdev->state & MACVLAN_STATE_MASK); 784b863ceb7SPatrick McHardy dev->features = lowerdev->features & MACVLAN_FEATURES; 7858b4703e9SVlad Yasevich dev->features |= ALWAYS_ON_FEATURES; 786081e83a7SVlad Yasevich dev->vlan_features = lowerdev->vlan_features & MACVLAN_FEATURES; 7878c2acc53SPatrick McHardy dev->gso_max_size = lowerdev->gso_max_size; 788b863ceb7SPatrick McHardy dev->iflink = lowerdev->ifindex; 789ef5c8996Ssg.tweak@gmail.com dev->hard_header_len = lowerdev->hard_header_len; 790b863ceb7SPatrick McHardy 791c773e847SDavid S. Miller macvlan_set_lockdep_class(dev); 792c773e847SDavid S. Miller 7931c213bd2SWANG Cong vlan->pcpu_stats = netdev_alloc_pcpu_stats(struct vlan_pcpu_stats); 7948ffab51bSEric Dumazet if (!vlan->pcpu_stats) 795fccaf710SEric Dumazet return -ENOMEM; 796fccaf710SEric Dumazet 797b863ceb7SPatrick McHardy return 0; 798b863ceb7SPatrick McHardy } 799b863ceb7SPatrick McHardy 800fccaf710SEric Dumazet static void macvlan_uninit(struct net_device *dev) 801fccaf710SEric Dumazet { 802fccaf710SEric Dumazet struct macvlan_dev *vlan = netdev_priv(dev); 803d5cd9244SEric W. Biederman struct macvlan_port *port = vlan->port; 804fccaf710SEric Dumazet 8058ffab51bSEric Dumazet free_percpu(vlan->pcpu_stats); 806d5cd9244SEric W. Biederman 80779cf79abSMichael Braun macvlan_flush_sources(port, vlan); 8085e3c516bSDavid S. Miller port->count -= 1; 8095e3c516bSDavid S. Miller if (!port->count) 810d5cd9244SEric W. Biederman macvlan_port_destroy(port->dev); 811fccaf710SEric Dumazet } 812fccaf710SEric Dumazet 81328172739SEric Dumazet static struct rtnl_link_stats64 *macvlan_dev_get_stats64(struct net_device *dev, 81428172739SEric Dumazet struct rtnl_link_stats64 *stats) 815fccaf710SEric Dumazet { 816fccaf710SEric Dumazet struct macvlan_dev *vlan = netdev_priv(dev); 817fccaf710SEric Dumazet 8188ffab51bSEric Dumazet if (vlan->pcpu_stats) { 819cdf3e274SLi RongQing struct vlan_pcpu_stats *p; 8208ffab51bSEric Dumazet u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes; 8218ffab51bSEric Dumazet u32 rx_errors = 0, tx_dropped = 0; 822bc66154eSEric Dumazet unsigned int start; 823fccaf710SEric Dumazet int i; 824fccaf710SEric Dumazet 825fccaf710SEric Dumazet for_each_possible_cpu(i) { 8268ffab51bSEric Dumazet p = per_cpu_ptr(vlan->pcpu_stats, i); 827bc66154eSEric Dumazet do { 82857a7744eSEric W. Biederman start = u64_stats_fetch_begin_irq(&p->syncp); 829bc66154eSEric Dumazet rx_packets = p->rx_packets; 830bc66154eSEric Dumazet rx_bytes = p->rx_bytes; 831bc66154eSEric Dumazet rx_multicast = p->rx_multicast; 8328ffab51bSEric Dumazet tx_packets = p->tx_packets; 8338ffab51bSEric Dumazet tx_bytes = p->tx_bytes; 83457a7744eSEric W. Biederman } while (u64_stats_fetch_retry_irq(&p->syncp, start)); 8358ffab51bSEric Dumazet 8368ffab51bSEric Dumazet stats->rx_packets += rx_packets; 8378ffab51bSEric Dumazet stats->rx_bytes += rx_bytes; 8388ffab51bSEric Dumazet stats->multicast += rx_multicast; 8398ffab51bSEric Dumazet stats->tx_packets += tx_packets; 8408ffab51bSEric Dumazet stats->tx_bytes += tx_bytes; 8418ffab51bSEric Dumazet /* rx_errors & tx_dropped are u32, updated 8428ffab51bSEric Dumazet * without syncp protection. 8438ffab51bSEric Dumazet */ 8448ffab51bSEric Dumazet rx_errors += p->rx_errors; 8458ffab51bSEric Dumazet tx_dropped += p->tx_dropped; 846fccaf710SEric Dumazet } 8478ffab51bSEric Dumazet stats->rx_errors = rx_errors; 8488ffab51bSEric Dumazet stats->rx_dropped = rx_errors; 8498ffab51bSEric Dumazet stats->tx_dropped = tx_dropped; 850fccaf710SEric Dumazet } 851fccaf710SEric Dumazet return stats; 852fccaf710SEric Dumazet } 853fccaf710SEric Dumazet 8548e586137SJiri Pirko static int macvlan_vlan_rx_add_vid(struct net_device *dev, 85580d5c368SPatrick McHardy __be16 proto, u16 vid) 8568d13e670SJohn Fastabend { 8578d13e670SJohn Fastabend struct macvlan_dev *vlan = netdev_priv(dev); 8588d13e670SJohn Fastabend struct net_device *lowerdev = vlan->lowerdev; 8598d13e670SJohn Fastabend 86080d5c368SPatrick McHardy return vlan_vid_add(lowerdev, proto, vid); 8618d13e670SJohn Fastabend } 8628d13e670SJohn Fastabend 8638e586137SJiri Pirko static int macvlan_vlan_rx_kill_vid(struct net_device *dev, 86480d5c368SPatrick McHardy __be16 proto, u16 vid) 8658d13e670SJohn Fastabend { 8668d13e670SJohn Fastabend struct macvlan_dev *vlan = netdev_priv(dev); 8678d13e670SJohn Fastabend struct net_device *lowerdev = vlan->lowerdev; 8688d13e670SJohn Fastabend 86980d5c368SPatrick McHardy vlan_vid_del(lowerdev, proto, vid); 8708e586137SJiri Pirko return 0; 8718d13e670SJohn Fastabend } 8728d13e670SJohn Fastabend 873edc7d573Sstephen hemminger static int macvlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], 874df8ef8f3SJohn Fastabend struct net_device *dev, 8756b6e2725Sstephen hemminger const unsigned char *addr, 876df8ef8f3SJohn Fastabend u16 flags) 877df8ef8f3SJohn Fastabend { 878df8ef8f3SJohn Fastabend struct macvlan_dev *vlan = netdev_priv(dev); 879df8ef8f3SJohn Fastabend int err = -EINVAL; 880df8ef8f3SJohn Fastabend 8818a50f11cSVlad Yasevich /* Support unicast filter only on passthru devices. 8828a50f11cSVlad Yasevich * Multicast filter should be allowed on all devices. 8838a50f11cSVlad Yasevich */ 8848a50f11cSVlad Yasevich if (!vlan->port->passthru && is_unicast_ether_addr(addr)) 885df8ef8f3SJohn Fastabend return -EOPNOTSUPP; 886df8ef8f3SJohn Fastabend 887ab2cfbb2SThomas Richter if (flags & NLM_F_REPLACE) 888ab2cfbb2SThomas Richter return -EOPNOTSUPP; 889ab2cfbb2SThomas Richter 890df8ef8f3SJohn Fastabend if (is_unicast_ether_addr(addr)) 891df8ef8f3SJohn Fastabend err = dev_uc_add_excl(dev, addr); 892df8ef8f3SJohn Fastabend else if (is_multicast_ether_addr(addr)) 893df8ef8f3SJohn Fastabend err = dev_mc_add_excl(dev, addr); 894df8ef8f3SJohn Fastabend 895df8ef8f3SJohn Fastabend return err; 896df8ef8f3SJohn Fastabend } 897df8ef8f3SJohn Fastabend 8981690be63SVlad Yasevich static int macvlan_fdb_del(struct ndmsg *ndm, struct nlattr *tb[], 899df8ef8f3SJohn Fastabend struct net_device *dev, 9006b6e2725Sstephen hemminger const unsigned char *addr) 901df8ef8f3SJohn Fastabend { 902df8ef8f3SJohn Fastabend struct macvlan_dev *vlan = netdev_priv(dev); 903df8ef8f3SJohn Fastabend int err = -EINVAL; 904df8ef8f3SJohn Fastabend 9058a50f11cSVlad Yasevich /* Support unicast filter only on passthru devices. 9068a50f11cSVlad Yasevich * Multicast filter should be allowed on all devices. 9078a50f11cSVlad Yasevich */ 9088a50f11cSVlad Yasevich if (!vlan->port->passthru && is_unicast_ether_addr(addr)) 909df8ef8f3SJohn Fastabend return -EOPNOTSUPP; 910df8ef8f3SJohn Fastabend 911df8ef8f3SJohn Fastabend if (is_unicast_ether_addr(addr)) 912df8ef8f3SJohn Fastabend err = dev_uc_del(dev, addr); 913df8ef8f3SJohn Fastabend else if (is_multicast_ether_addr(addr)) 914df8ef8f3SJohn Fastabend err = dev_mc_del(dev, addr); 915df8ef8f3SJohn Fastabend 916df8ef8f3SJohn Fastabend return err; 917df8ef8f3SJohn Fastabend } 918df8ef8f3SJohn Fastabend 919b863ceb7SPatrick McHardy static void macvlan_ethtool_get_drvinfo(struct net_device *dev, 920b863ceb7SPatrick McHardy struct ethtool_drvinfo *drvinfo) 921b863ceb7SPatrick McHardy { 9227826d43fSJiri Pirko strlcpy(drvinfo->driver, "macvlan", sizeof(drvinfo->driver)); 9237826d43fSJiri Pirko strlcpy(drvinfo->version, "0.1", sizeof(drvinfo->version)); 924b863ceb7SPatrick McHardy } 925b863ceb7SPatrick McHardy 9269edb8bb6SStephen Hemminger static int macvlan_ethtool_get_settings(struct net_device *dev, 9279edb8bb6SStephen Hemminger struct ethtool_cmd *cmd) 9289edb8bb6SStephen Hemminger { 9299edb8bb6SStephen Hemminger const struct macvlan_dev *vlan = netdev_priv(dev); 9304bc71cb9SJiri Pirko 9314bc71cb9SJiri Pirko return __ethtool_get_settings(vlan->lowerdev, cmd); 9329edb8bb6SStephen Hemminger } 9339edb8bb6SStephen Hemminger 9342be5c767SVlad Yasevich static netdev_features_t macvlan_fix_features(struct net_device *dev, 9352be5c767SVlad Yasevich netdev_features_t features) 9362be5c767SVlad Yasevich { 9372be5c767SVlad Yasevich struct macvlan_dev *vlan = netdev_priv(dev); 938797f87f8SFlorian Westphal netdev_features_t mask; 9392be5c767SVlad Yasevich 940797f87f8SFlorian Westphal features |= NETIF_F_ALL_FOR_ALL; 941797f87f8SFlorian Westphal features &= (vlan->set_features | ~MACVLAN_FEATURES); 942797f87f8SFlorian Westphal mask = features; 943797f87f8SFlorian Westphal 944797f87f8SFlorian Westphal features = netdev_increment_features(vlan->lowerdev->features, 945797f87f8SFlorian Westphal features, 946797f87f8SFlorian Westphal mask); 9478b4703e9SVlad Yasevich features |= ALWAYS_ON_FEATURES; 9480d0162e7SFrancesco Ruggeri features &= ~NETIF_F_NETNS_LOCAL; 949797f87f8SFlorian Westphal 950797f87f8SFlorian Westphal return features; 9512be5c767SVlad Yasevich } 9522be5c767SVlad Yasevich 953688cea83Sdingtianhong #ifdef CONFIG_NET_POLL_CONTROLLER 954688cea83Sdingtianhong static void macvlan_dev_poll_controller(struct net_device *dev) 955688cea83Sdingtianhong { 956688cea83Sdingtianhong return; 957688cea83Sdingtianhong } 958688cea83Sdingtianhong 959688cea83Sdingtianhong static int macvlan_dev_netpoll_setup(struct net_device *dev, struct netpoll_info *npinfo) 960688cea83Sdingtianhong { 961688cea83Sdingtianhong struct macvlan_dev *vlan = netdev_priv(dev); 962688cea83Sdingtianhong struct net_device *real_dev = vlan->lowerdev; 963688cea83Sdingtianhong struct netpoll *netpoll; 964688cea83Sdingtianhong int err = 0; 965688cea83Sdingtianhong 966688cea83Sdingtianhong netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL); 967688cea83Sdingtianhong err = -ENOMEM; 968688cea83Sdingtianhong if (!netpoll) 969688cea83Sdingtianhong goto out; 970688cea83Sdingtianhong 971688cea83Sdingtianhong err = __netpoll_setup(netpoll, real_dev); 972688cea83Sdingtianhong if (err) { 973688cea83Sdingtianhong kfree(netpoll); 974688cea83Sdingtianhong goto out; 975688cea83Sdingtianhong } 976688cea83Sdingtianhong 977688cea83Sdingtianhong vlan->netpoll = netpoll; 978688cea83Sdingtianhong 979688cea83Sdingtianhong out: 980688cea83Sdingtianhong return err; 981688cea83Sdingtianhong } 982688cea83Sdingtianhong 983688cea83Sdingtianhong static void macvlan_dev_netpoll_cleanup(struct net_device *dev) 984688cea83Sdingtianhong { 985688cea83Sdingtianhong struct macvlan_dev *vlan = netdev_priv(dev); 986688cea83Sdingtianhong struct netpoll *netpoll = vlan->netpoll; 987688cea83Sdingtianhong 988688cea83Sdingtianhong if (!netpoll) 989688cea83Sdingtianhong return; 990688cea83Sdingtianhong 991688cea83Sdingtianhong vlan->netpoll = NULL; 992688cea83Sdingtianhong 993688cea83Sdingtianhong __netpoll_free_async(netpoll); 994688cea83Sdingtianhong } 995688cea83Sdingtianhong #endif /* CONFIG_NET_POLL_CONTROLLER */ 996688cea83Sdingtianhong 997b863ceb7SPatrick McHardy static const struct ethtool_ops macvlan_ethtool_ops = { 998b863ceb7SPatrick McHardy .get_link = ethtool_op_get_link, 9999edb8bb6SStephen Hemminger .get_settings = macvlan_ethtool_get_settings, 1000b863ceb7SPatrick McHardy .get_drvinfo = macvlan_ethtool_get_drvinfo, 1001b863ceb7SPatrick McHardy }; 1002b863ceb7SPatrick McHardy 100354a30c97SStephen Hemminger static const struct net_device_ops macvlan_netdev_ops = { 100454a30c97SStephen Hemminger .ndo_init = macvlan_init, 1005fccaf710SEric Dumazet .ndo_uninit = macvlan_uninit, 100654a30c97SStephen Hemminger .ndo_open = macvlan_open, 100754a30c97SStephen Hemminger .ndo_stop = macvlan_stop, 100800829823SStephen Hemminger .ndo_start_xmit = macvlan_start_xmit, 100954a30c97SStephen Hemminger .ndo_change_mtu = macvlan_change_mtu, 10102be5c767SVlad Yasevich .ndo_fix_features = macvlan_fix_features, 101154a30c97SStephen Hemminger .ndo_change_rx_flags = macvlan_change_rx_flags, 101254a30c97SStephen Hemminger .ndo_set_mac_address = macvlan_set_mac_address, 1013df8ef8f3SJohn Fastabend .ndo_set_rx_mode = macvlan_set_mac_lists, 1014bc66154eSEric Dumazet .ndo_get_stats64 = macvlan_dev_get_stats64, 101554a30c97SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 10168d13e670SJohn Fastabend .ndo_vlan_rx_add_vid = macvlan_vlan_rx_add_vid, 10178d13e670SJohn Fastabend .ndo_vlan_rx_kill_vid = macvlan_vlan_rx_kill_vid, 1018df8ef8f3SJohn Fastabend .ndo_fdb_add = macvlan_fdb_add, 1019df8ef8f3SJohn Fastabend .ndo_fdb_del = macvlan_fdb_del, 1020df8ef8f3SJohn Fastabend .ndo_fdb_dump = ndo_dflt_fdb_dump, 1021c674ac30SVlad Yasevich .ndo_get_lock_subclass = macvlan_get_nest_level, 1022688cea83Sdingtianhong #ifdef CONFIG_NET_POLL_CONTROLLER 1023688cea83Sdingtianhong .ndo_poll_controller = macvlan_dev_poll_controller, 1024688cea83Sdingtianhong .ndo_netpoll_setup = macvlan_dev_netpoll_setup, 1025688cea83Sdingtianhong .ndo_netpoll_cleanup = macvlan_dev_netpoll_cleanup, 1026688cea83Sdingtianhong #endif 102754a30c97SStephen Hemminger }; 102854a30c97SStephen Hemminger 10298a35747aSHerbert Xu void macvlan_common_setup(struct net_device *dev) 1030b863ceb7SPatrick McHardy { 1031b863ceb7SPatrick McHardy ether_setup(dev); 1032b863ceb7SPatrick McHardy 103302875878SEric Dumazet dev->priv_flags &= ~IFF_TX_SKB_SHARING; 103402875878SEric Dumazet netif_keep_dst(dev); 103587ab7f6fSVlad Yasevich dev->priv_flags |= IFF_UNICAST_FLT; 103654a30c97SStephen Hemminger dev->netdev_ops = &macvlan_netdev_ops; 1037b863ceb7SPatrick McHardy dev->destructor = free_netdev; 103871740129SLutz Jaenicke dev->header_ops = &macvlan_hard_header_ops; 1039b863ceb7SPatrick McHardy dev->ethtool_ops = &macvlan_ethtool_ops; 10408a35747aSHerbert Xu } 10418a35747aSHerbert Xu EXPORT_SYMBOL_GPL(macvlan_common_setup); 10428a35747aSHerbert Xu 10438a35747aSHerbert Xu static void macvlan_setup(struct net_device *dev) 10448a35747aSHerbert Xu { 10458a35747aSHerbert Xu macvlan_common_setup(dev); 1046b863ceb7SPatrick McHardy dev->tx_queue_len = 0; 1047b863ceb7SPatrick McHardy } 1048b863ceb7SPatrick McHardy 1049b863ceb7SPatrick McHardy static int macvlan_port_create(struct net_device *dev) 1050b863ceb7SPatrick McHardy { 1051b863ceb7SPatrick McHardy struct macvlan_port *port; 1052b863ceb7SPatrick McHardy unsigned int i; 1053ab95bfe0SJiri Pirko int err; 1054b863ceb7SPatrick McHardy 1055b863ceb7SPatrick McHardy if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK) 1056b863ceb7SPatrick McHardy return -EINVAL; 1057b863ceb7SPatrick McHardy 1058b863ceb7SPatrick McHardy port = kzalloc(sizeof(*port), GFP_KERNEL); 1059b863ceb7SPatrick McHardy if (port == NULL) 1060b863ceb7SPatrick McHardy return -ENOMEM; 1061b863ceb7SPatrick McHardy 1062eb06acdcSSridhar Samudrala port->passthru = false; 1063b863ceb7SPatrick McHardy port->dev = dev; 1064b863ceb7SPatrick McHardy INIT_LIST_HEAD(&port->vlans); 1065b863ceb7SPatrick McHardy for (i = 0; i < MACVLAN_HASH_SIZE; i++) 1066b863ceb7SPatrick McHardy INIT_HLIST_HEAD(&port->vlan_hash[i]); 106779cf79abSMichael Braun for (i = 0; i < MACVLAN_HASH_SIZE; i++) 106879cf79abSMichael Braun INIT_HLIST_HEAD(&port->vlan_source_hash[i]); 1069ab95bfe0SJiri Pirko 1070412ca155SHerbert Xu skb_queue_head_init(&port->bc_queue); 1071412ca155SHerbert Xu INIT_WORK(&port->bc_work, macvlan_process_broadcast); 1072412ca155SHerbert Xu 1073a35e2c1bSJiri Pirko err = netdev_rx_handler_register(dev, macvlan_handle_frame, port); 1074a35e2c1bSJiri Pirko if (err) 1075ab95bfe0SJiri Pirko kfree(port); 1076d9351561SEric Dumazet else 1077a35e2c1bSJiri Pirko dev->priv_flags |= IFF_MACVLAN_PORT; 1078ab95bfe0SJiri Pirko return err; 1079b863ceb7SPatrick McHardy } 1080b863ceb7SPatrick McHardy 1081b863ceb7SPatrick McHardy static void macvlan_port_destroy(struct net_device *dev) 1082b863ceb7SPatrick McHardy { 1083e052f7e6SEric Dumazet struct macvlan_port *port = macvlan_port_get_rtnl(dev); 1084b863ceb7SPatrick McHardy 1085a35e2c1bSJiri Pirko dev->priv_flags &= ~IFF_MACVLAN_PORT; 1086ab95bfe0SJiri Pirko netdev_rx_handler_unregister(dev); 1087*fe0ca732SEric Dumazet 1088*fe0ca732SEric Dumazet /* After this point, no packet can schedule bc_work anymore, 1089*fe0ca732SEric Dumazet * but we need to cancel it and purge left skbs if any. 1090*fe0ca732SEric Dumazet */ 1091*fe0ca732SEric Dumazet cancel_work_sync(&port->bc_work); 1092*fe0ca732SEric Dumazet __skb_queue_purge(&port->bc_queue); 1093*fe0ca732SEric Dumazet 10946e070aecSLai Jiangshan kfree_rcu(port, rcu); 1095b863ceb7SPatrick McHardy } 1096b863ceb7SPatrick McHardy 1097b863ceb7SPatrick McHardy static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[]) 1098b863ceb7SPatrick McHardy { 1099b863ceb7SPatrick McHardy if (tb[IFLA_ADDRESS]) { 1100b863ceb7SPatrick McHardy if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) 1101b863ceb7SPatrick McHardy return -EINVAL; 1102b863ceb7SPatrick McHardy if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) 1103b863ceb7SPatrick McHardy return -EADDRNOTAVAIL; 1104b863ceb7SPatrick McHardy } 110527c0b1a8SArnd Bergmann 110615127478SMichael S. Tsirkin if (data && data[IFLA_MACVLAN_FLAGS] && 110715127478SMichael S. Tsirkin nla_get_u16(data[IFLA_MACVLAN_FLAGS]) & ~MACVLAN_FLAG_NOPROMISC) 110815127478SMichael S. Tsirkin return -EINVAL; 110915127478SMichael S. Tsirkin 111027c0b1a8SArnd Bergmann if (data && data[IFLA_MACVLAN_MODE]) { 111127c0b1a8SArnd Bergmann switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) { 111227c0b1a8SArnd Bergmann case MACVLAN_MODE_PRIVATE: 111327c0b1a8SArnd Bergmann case MACVLAN_MODE_VEPA: 111427c0b1a8SArnd Bergmann case MACVLAN_MODE_BRIDGE: 1115eb06acdcSSridhar Samudrala case MACVLAN_MODE_PASSTHRU: 111679cf79abSMichael Braun case MACVLAN_MODE_SOURCE: 111727c0b1a8SArnd Bergmann break; 111827c0b1a8SArnd Bergmann default: 111927c0b1a8SArnd Bergmann return -EINVAL; 112027c0b1a8SArnd Bergmann } 112127c0b1a8SArnd Bergmann } 112279cf79abSMichael Braun 112379cf79abSMichael Braun if (data && data[IFLA_MACVLAN_MACADDR_MODE]) { 112479cf79abSMichael Braun switch (nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE])) { 112579cf79abSMichael Braun case MACVLAN_MACADDR_ADD: 112679cf79abSMichael Braun case MACVLAN_MACADDR_DEL: 112779cf79abSMichael Braun case MACVLAN_MACADDR_FLUSH: 112879cf79abSMichael Braun case MACVLAN_MACADDR_SET: 112979cf79abSMichael Braun break; 113079cf79abSMichael Braun default: 113179cf79abSMichael Braun return -EINVAL; 113279cf79abSMichael Braun } 113379cf79abSMichael Braun } 113479cf79abSMichael Braun 113579cf79abSMichael Braun if (data && data[IFLA_MACVLAN_MACADDR]) { 113679cf79abSMichael Braun if (nla_len(data[IFLA_MACVLAN_MACADDR]) != ETH_ALEN) 113779cf79abSMichael Braun return -EINVAL; 113879cf79abSMichael Braun 113979cf79abSMichael Braun if (!is_valid_ether_addr(nla_data(data[IFLA_MACVLAN_MACADDR]))) 114079cf79abSMichael Braun return -EADDRNOTAVAIL; 114179cf79abSMichael Braun } 114279cf79abSMichael Braun 114379cf79abSMichael Braun if (data && data[IFLA_MACVLAN_MACADDR_COUNT]) 114479cf79abSMichael Braun return -EINVAL; 114579cf79abSMichael Braun 114679cf79abSMichael Braun return 0; 114779cf79abSMichael Braun } 114879cf79abSMichael Braun 114979cf79abSMichael Braun /** 115079cf79abSMichael Braun * reconfigure list of remote source mac address 115179cf79abSMichael Braun * (only for macvlan devices in source mode) 115279cf79abSMichael Braun * Note regarding alignment: all netlink data is aligned to 4 Byte, which 115379cf79abSMichael Braun * suffices for both ether_addr_copy and ether_addr_equal_64bits usage. 115479cf79abSMichael Braun */ 115579cf79abSMichael Braun static int macvlan_changelink_sources(struct macvlan_dev *vlan, u32 mode, 115679cf79abSMichael Braun struct nlattr *data[]) 115779cf79abSMichael Braun { 115879cf79abSMichael Braun char *addr = NULL; 115979cf79abSMichael Braun int ret, rem, len; 116079cf79abSMichael Braun struct nlattr *nla, *head; 116179cf79abSMichael Braun struct macvlan_source_entry *entry; 116279cf79abSMichael Braun 116379cf79abSMichael Braun if (data[IFLA_MACVLAN_MACADDR]) 116479cf79abSMichael Braun addr = nla_data(data[IFLA_MACVLAN_MACADDR]); 116579cf79abSMichael Braun 116679cf79abSMichael Braun if (mode == MACVLAN_MACADDR_ADD) { 116779cf79abSMichael Braun if (!addr) 116879cf79abSMichael Braun return -EINVAL; 116979cf79abSMichael Braun 117079cf79abSMichael Braun return macvlan_hash_add_source(vlan, addr); 117179cf79abSMichael Braun 117279cf79abSMichael Braun } else if (mode == MACVLAN_MACADDR_DEL) { 117379cf79abSMichael Braun if (!addr) 117479cf79abSMichael Braun return -EINVAL; 117579cf79abSMichael Braun 117679cf79abSMichael Braun entry = macvlan_hash_lookup_source(vlan, addr); 117779cf79abSMichael Braun if (entry) { 117879cf79abSMichael Braun macvlan_hash_del_source(entry); 117979cf79abSMichael Braun vlan->macaddr_count--; 118079cf79abSMichael Braun } 118179cf79abSMichael Braun } else if (mode == MACVLAN_MACADDR_FLUSH) { 118279cf79abSMichael Braun macvlan_flush_sources(vlan->port, vlan); 118379cf79abSMichael Braun } else if (mode == MACVLAN_MACADDR_SET) { 118479cf79abSMichael Braun macvlan_flush_sources(vlan->port, vlan); 118579cf79abSMichael Braun 118679cf79abSMichael Braun if (addr) { 118779cf79abSMichael Braun ret = macvlan_hash_add_source(vlan, addr); 118879cf79abSMichael Braun if (ret) 118979cf79abSMichael Braun return ret; 119079cf79abSMichael Braun } 119179cf79abSMichael Braun 119279cf79abSMichael Braun if (!data || !data[IFLA_MACVLAN_MACADDR_DATA]) 119379cf79abSMichael Braun return 0; 119479cf79abSMichael Braun 119579cf79abSMichael Braun head = nla_data(data[IFLA_MACVLAN_MACADDR_DATA]); 119679cf79abSMichael Braun len = nla_len(data[IFLA_MACVLAN_MACADDR_DATA]); 119779cf79abSMichael Braun 119879cf79abSMichael Braun nla_for_each_attr(nla, head, len, rem) { 119979cf79abSMichael Braun if (nla_type(nla) != IFLA_MACVLAN_MACADDR || 120079cf79abSMichael Braun nla_len(nla) != ETH_ALEN) 120179cf79abSMichael Braun continue; 120279cf79abSMichael Braun 120379cf79abSMichael Braun addr = nla_data(nla); 120479cf79abSMichael Braun ret = macvlan_hash_add_source(vlan, addr); 120579cf79abSMichael Braun if (ret) 120679cf79abSMichael Braun return ret; 120779cf79abSMichael Braun } 120879cf79abSMichael Braun } else { 120979cf79abSMichael Braun return -EINVAL; 121079cf79abSMichael Braun } 121179cf79abSMichael Braun 1212b863ceb7SPatrick McHardy return 0; 1213b863ceb7SPatrick McHardy } 1214b863ceb7SPatrick McHardy 1215fc0663d6SArnd Bergmann int macvlan_common_newlink(struct net *src_net, struct net_device *dev, 12162f6a1b66SVlad Yasevich struct nlattr *tb[], struct nlattr *data[]) 1217b863ceb7SPatrick McHardy { 1218b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 1219b863ceb7SPatrick McHardy struct macvlan_port *port; 1220b863ceb7SPatrick McHardy struct net_device *lowerdev; 1221b863ceb7SPatrick McHardy int err; 122279cf79abSMichael Braun int macmode; 1223b863ceb7SPatrick McHardy 1224b863ceb7SPatrick McHardy if (!tb[IFLA_LINK]) 1225b863ceb7SPatrick McHardy return -EINVAL; 1226b863ceb7SPatrick McHardy 122781adee47SEric W. Biederman lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK])); 1228b863ceb7SPatrick McHardy if (lowerdev == NULL) 1229b863ceb7SPatrick McHardy return -ENODEV; 1230b863ceb7SPatrick McHardy 1231d70f2cf5SKevin Wallace /* When creating macvlans or macvtaps on top of other macvlans - use 1232b0832a29SEric Biederman * the real device as the lowerdev. 1233a6ca5f1dSPatrick McHardy */ 1234d70f2cf5SKevin Wallace if (netif_is_macvlan(lowerdev)) 1235d70f2cf5SKevin Wallace lowerdev = macvlan_dev_real_dev(lowerdev); 1236a6ca5f1dSPatrick McHardy 1237b863ceb7SPatrick McHardy if (!tb[IFLA_MTU]) 1238b863ceb7SPatrick McHardy dev->mtu = lowerdev->mtu; 1239b863ceb7SPatrick McHardy else if (dev->mtu > lowerdev->mtu) 1240b863ceb7SPatrick McHardy return -EINVAL; 1241b863ceb7SPatrick McHardy 1242b863ceb7SPatrick McHardy if (!tb[IFLA_ADDRESS]) 12437ce5d222SDanny Kukawka eth_hw_addr_random(dev); 1244b863ceb7SPatrick McHardy 1245a35e2c1bSJiri Pirko if (!macvlan_port_exists(lowerdev)) { 1246b863ceb7SPatrick McHardy err = macvlan_port_create(lowerdev); 1247b863ceb7SPatrick McHardy if (err < 0) 1248b863ceb7SPatrick McHardy return err; 1249b863ceb7SPatrick McHardy } 1250e052f7e6SEric Dumazet port = macvlan_port_get_rtnl(lowerdev); 1251b863ceb7SPatrick McHardy 1252eb06acdcSSridhar Samudrala /* Only 1 macvlan device can be created in passthru mode */ 1253eb06acdcSSridhar Samudrala if (port->passthru) 1254eb06acdcSSridhar Samudrala return -EINVAL; 1255eb06acdcSSridhar Samudrala 1256b863ceb7SPatrick McHardy vlan->lowerdev = lowerdev; 1257b863ceb7SPatrick McHardy vlan->dev = dev; 1258b863ceb7SPatrick McHardy vlan->port = port; 12592be5c767SVlad Yasevich vlan->set_features = MACVLAN_FEATURES; 1260c674ac30SVlad Yasevich vlan->nest_level = dev_get_nest_level(lowerdev, netif_is_macvlan) + 1; 1261b863ceb7SPatrick McHardy 126227c0b1a8SArnd Bergmann vlan->mode = MACVLAN_MODE_VEPA; 126327c0b1a8SArnd Bergmann if (data && data[IFLA_MACVLAN_MODE]) 126427c0b1a8SArnd Bergmann vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]); 126527c0b1a8SArnd Bergmann 1266df8ef8f3SJohn Fastabend if (data && data[IFLA_MACVLAN_FLAGS]) 1267df8ef8f3SJohn Fastabend vlan->flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]); 1268df8ef8f3SJohn Fastabend 1269eb06acdcSSridhar Samudrala if (vlan->mode == MACVLAN_MODE_PASSTHRU) { 12705e3c516bSDavid S. Miller if (port->count) 1271eb06acdcSSridhar Samudrala return -EINVAL; 1272eb06acdcSSridhar Samudrala port->passthru = true; 12738b98604eSBjørn Mork eth_hw_addr_inherit(dev, lowerdev); 1274eb06acdcSSridhar Samudrala } 1275eb06acdcSSridhar Samudrala 127679cf79abSMichael Braun if (data && data[IFLA_MACVLAN_MACADDR_MODE]) { 127779cf79abSMichael Braun if (vlan->mode != MACVLAN_MODE_SOURCE) 127879cf79abSMichael Braun return -EINVAL; 127979cf79abSMichael Braun macmode = nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE]); 128079cf79abSMichael Braun err = macvlan_changelink_sources(vlan, macmode, data); 128179cf79abSMichael Braun if (err) 128279cf79abSMichael Braun return err; 128379cf79abSMichael Braun } 128479cf79abSMichael Braun 12855e3c516bSDavid S. Miller port->count += 1; 128647d4ab91SJohn Fastabend err = register_netdevice(dev); 128747d4ab91SJohn Fastabend if (err < 0) 128847d4ab91SJohn Fastabend goto destroy_port; 128947d4ab91SJohn Fastabend 1290a6cc0cfaSJohn Fastabend dev->priv_flags |= IFF_MACVLAN; 12917cd43db7SJiri Pirko err = netdev_upper_dev_link(lowerdev, dev); 12927cd43db7SJiri Pirko if (err) 1293da37705cSCong Wang goto unregister_netdev; 1294b863ceb7SPatrick McHardy 1295233c7df0SJiri Pirko list_add_tail_rcu(&vlan->list, &port->vlans); 1296fc4a7489SPatrick Mullaney netif_stacked_transfer_operstate(lowerdev, dev); 1297f16d3d57SJiri Pirko 1298b863ceb7SPatrick McHardy return 0; 1299f16d3d57SJiri Pirko 1300da37705cSCong Wang unregister_netdev: 1301da37705cSCong Wang unregister_netdevice(dev); 1302f16d3d57SJiri Pirko destroy_port: 13035e3c516bSDavid S. Miller port->count -= 1; 13045e3c516bSDavid S. Miller if (!port->count) 1305f16d3d57SJiri Pirko macvlan_port_destroy(lowerdev); 1306f16d3d57SJiri Pirko 1307f16d3d57SJiri Pirko return err; 1308b863ceb7SPatrick McHardy } 1309fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_common_newlink); 1310b863ceb7SPatrick McHardy 1311fc0663d6SArnd Bergmann static int macvlan_newlink(struct net *src_net, struct net_device *dev, 1312fc0663d6SArnd Bergmann struct nlattr *tb[], struct nlattr *data[]) 1313fc0663d6SArnd Bergmann { 13142f6a1b66SVlad Yasevich return macvlan_common_newlink(src_net, dev, tb, data); 1315fc0663d6SArnd Bergmann } 1316fc0663d6SArnd Bergmann 1317fc0663d6SArnd Bergmann void macvlan_dellink(struct net_device *dev, struct list_head *head) 1318b863ceb7SPatrick McHardy { 1319b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 1320b863ceb7SPatrick McHardy 132179cf79abSMichael Braun if (vlan->mode == MACVLAN_MODE_SOURCE) 132279cf79abSMichael Braun macvlan_flush_sources(vlan->port, vlan); 1323233c7df0SJiri Pirko list_del_rcu(&vlan->list); 132423289a37SEric Dumazet unregister_netdevice_queue(dev, head); 13257cd43db7SJiri Pirko netdev_upper_dev_unlink(vlan->lowerdev, dev); 1326b863ceb7SPatrick McHardy } 1327fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_dellink); 1328b863ceb7SPatrick McHardy 132927c0b1a8SArnd Bergmann static int macvlan_changelink(struct net_device *dev, 133027c0b1a8SArnd Bergmann struct nlattr *tb[], struct nlattr *data[]) 133127c0b1a8SArnd Bergmann { 133227c0b1a8SArnd Bergmann struct macvlan_dev *vlan = netdev_priv(dev); 1333266e8347SMichael S. Tsirkin enum macvlan_mode mode; 1334266e8347SMichael S. Tsirkin bool set_mode = false; 133579cf79abSMichael Braun enum macvlan_macaddr_mode macmode; 133679cf79abSMichael Braun int ret; 1337266e8347SMichael S. Tsirkin 1338266e8347SMichael S. Tsirkin /* Validate mode, but don't set yet: setting flags may fail. */ 1339266e8347SMichael S. Tsirkin if (data && data[IFLA_MACVLAN_MODE]) { 1340266e8347SMichael S. Tsirkin set_mode = true; 1341266e8347SMichael S. Tsirkin mode = nla_get_u32(data[IFLA_MACVLAN_MODE]); 1342266e8347SMichael S. Tsirkin /* Passthrough mode can't be set or cleared dynamically */ 1343266e8347SMichael S. Tsirkin if ((mode == MACVLAN_MODE_PASSTHRU) != 1344266e8347SMichael S. Tsirkin (vlan->mode == MACVLAN_MODE_PASSTHRU)) 1345266e8347SMichael S. Tsirkin return -EINVAL; 134679cf79abSMichael Braun if (vlan->mode == MACVLAN_MODE_SOURCE && 134779cf79abSMichael Braun vlan->mode != mode) 134879cf79abSMichael Braun macvlan_flush_sources(vlan->port, vlan); 1349266e8347SMichael S. Tsirkin } 135099ffc3e7SMichael S. Tsirkin 1351df8ef8f3SJohn Fastabend if (data && data[IFLA_MACVLAN_FLAGS]) { 1352df8ef8f3SJohn Fastabend __u16 flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]); 1353df8ef8f3SJohn Fastabend bool promisc = (flags ^ vlan->flags) & MACVLAN_FLAG_NOPROMISC; 135499ffc3e7SMichael S. Tsirkin if (vlan->port->passthru && promisc) { 135599ffc3e7SMichael S. Tsirkin int err; 1356df8ef8f3SJohn Fastabend 135799ffc3e7SMichael S. Tsirkin if (flags & MACVLAN_FLAG_NOPROMISC) 135899ffc3e7SMichael S. Tsirkin err = dev_set_promiscuity(vlan->lowerdev, -1); 135999ffc3e7SMichael S. Tsirkin else 136099ffc3e7SMichael S. Tsirkin err = dev_set_promiscuity(vlan->lowerdev, 1); 136199ffc3e7SMichael S. Tsirkin if (err < 0) 136299ffc3e7SMichael S. Tsirkin return err; 136399ffc3e7SMichael S. Tsirkin } 1364df8ef8f3SJohn Fastabend vlan->flags = flags; 1365df8ef8f3SJohn Fastabend } 1366266e8347SMichael S. Tsirkin if (set_mode) 1367266e8347SMichael S. Tsirkin vlan->mode = mode; 136879cf79abSMichael Braun if (data && data[IFLA_MACVLAN_MACADDR_MODE]) { 136979cf79abSMichael Braun if (vlan->mode != MACVLAN_MODE_SOURCE) 137079cf79abSMichael Braun return -EINVAL; 137179cf79abSMichael Braun macmode = nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE]); 137279cf79abSMichael Braun ret = macvlan_changelink_sources(vlan, macmode, data); 137379cf79abSMichael Braun if (ret) 137479cf79abSMichael Braun return ret; 137579cf79abSMichael Braun } 137627c0b1a8SArnd Bergmann return 0; 137727c0b1a8SArnd Bergmann } 137827c0b1a8SArnd Bergmann 137979cf79abSMichael Braun static size_t macvlan_get_size_mac(const struct macvlan_dev *vlan) 138079cf79abSMichael Braun { 138179cf79abSMichael Braun if (vlan->macaddr_count == 0) 138279cf79abSMichael Braun return 0; 138379cf79abSMichael Braun return nla_total_size(0) /* IFLA_MACVLAN_MACADDR_DATA */ 138479cf79abSMichael Braun + vlan->macaddr_count * nla_total_size(sizeof(u8) * ETH_ALEN); 138579cf79abSMichael Braun } 138679cf79abSMichael Braun 138727c0b1a8SArnd Bergmann static size_t macvlan_get_size(const struct net_device *dev) 138827c0b1a8SArnd Bergmann { 138979cf79abSMichael Braun struct macvlan_dev *vlan = netdev_priv(dev); 139079cf79abSMichael Braun 139101fe944fSEric Dumazet return (0 139201fe944fSEric Dumazet + nla_total_size(4) /* IFLA_MACVLAN_MODE */ 139301fe944fSEric Dumazet + nla_total_size(2) /* IFLA_MACVLAN_FLAGS */ 139479cf79abSMichael Braun + nla_total_size(4) /* IFLA_MACVLAN_MACADDR_COUNT */ 139579cf79abSMichael Braun + macvlan_get_size_mac(vlan) /* IFLA_MACVLAN_MACADDR */ 139601fe944fSEric Dumazet ); 139727c0b1a8SArnd Bergmann } 139827c0b1a8SArnd Bergmann 139979cf79abSMichael Braun static int macvlan_fill_info_macaddr(struct sk_buff *skb, 140079cf79abSMichael Braun const struct macvlan_dev *vlan, 140179cf79abSMichael Braun const int i) 140279cf79abSMichael Braun { 140379cf79abSMichael Braun struct hlist_head *h = &vlan->port->vlan_source_hash[i]; 140479cf79abSMichael Braun struct macvlan_source_entry *entry; 140579cf79abSMichael Braun 140679cf79abSMichael Braun hlist_for_each_entry_rcu(entry, h, hlist) { 140779cf79abSMichael Braun if (entry->vlan != vlan) 140879cf79abSMichael Braun continue; 140979cf79abSMichael Braun if (nla_put(skb, IFLA_MACVLAN_MACADDR, ETH_ALEN, entry->addr)) 141079cf79abSMichael Braun return 1; 141179cf79abSMichael Braun } 141279cf79abSMichael Braun return 0; 141379cf79abSMichael Braun } 141479cf79abSMichael Braun 141527c0b1a8SArnd Bergmann static int macvlan_fill_info(struct sk_buff *skb, 141627c0b1a8SArnd Bergmann const struct net_device *dev) 141727c0b1a8SArnd Bergmann { 141827c0b1a8SArnd Bergmann struct macvlan_dev *vlan = netdev_priv(dev); 141979cf79abSMichael Braun int i; 142079cf79abSMichael Braun struct nlattr *nest; 142127c0b1a8SArnd Bergmann 1422ead9a76cSDavid S. Miller if (nla_put_u32(skb, IFLA_MACVLAN_MODE, vlan->mode)) 1423ead9a76cSDavid S. Miller goto nla_put_failure; 1424df8ef8f3SJohn Fastabend if (nla_put_u16(skb, IFLA_MACVLAN_FLAGS, vlan->flags)) 1425df8ef8f3SJohn Fastabend goto nla_put_failure; 142679cf79abSMichael Braun if (nla_put_u32(skb, IFLA_MACVLAN_MACADDR_COUNT, vlan->macaddr_count)) 142779cf79abSMichael Braun goto nla_put_failure; 142879cf79abSMichael Braun if (vlan->macaddr_count > 0) { 142979cf79abSMichael Braun nest = nla_nest_start(skb, IFLA_MACVLAN_MACADDR_DATA); 143079cf79abSMichael Braun if (nest == NULL) 143179cf79abSMichael Braun goto nla_put_failure; 143279cf79abSMichael Braun 143379cf79abSMichael Braun for (i = 0; i < MACVLAN_HASH_SIZE; i++) { 143479cf79abSMichael Braun if (macvlan_fill_info_macaddr(skb, vlan, i)) 143579cf79abSMichael Braun goto nla_put_failure; 143679cf79abSMichael Braun } 143779cf79abSMichael Braun nla_nest_end(skb, nest); 143879cf79abSMichael Braun } 143927c0b1a8SArnd Bergmann return 0; 144027c0b1a8SArnd Bergmann 144127c0b1a8SArnd Bergmann nla_put_failure: 144227c0b1a8SArnd Bergmann return -EMSGSIZE; 144327c0b1a8SArnd Bergmann } 144427c0b1a8SArnd Bergmann 144527c0b1a8SArnd Bergmann static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = { 144627c0b1a8SArnd Bergmann [IFLA_MACVLAN_MODE] = { .type = NLA_U32 }, 1447df8ef8f3SJohn Fastabend [IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 }, 144879cf79abSMichael Braun [IFLA_MACVLAN_MACADDR_MODE] = { .type = NLA_U32 }, 144979cf79abSMichael Braun [IFLA_MACVLAN_MACADDR] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 145079cf79abSMichael Braun [IFLA_MACVLAN_MACADDR_DATA] = { .type = NLA_NESTED }, 145179cf79abSMichael Braun [IFLA_MACVLAN_MACADDR_COUNT] = { .type = NLA_U32 }, 145227c0b1a8SArnd Bergmann }; 145327c0b1a8SArnd Bergmann 1454fc0663d6SArnd Bergmann int macvlan_link_register(struct rtnl_link_ops *ops) 1455fc0663d6SArnd Bergmann { 1456fc0663d6SArnd Bergmann /* common fields */ 1457fc0663d6SArnd Bergmann ops->priv_size = sizeof(struct macvlan_dev); 1458fc0663d6SArnd Bergmann ops->validate = macvlan_validate; 1459fc0663d6SArnd Bergmann ops->maxtype = IFLA_MACVLAN_MAX; 1460fc0663d6SArnd Bergmann ops->policy = macvlan_policy; 1461fc0663d6SArnd Bergmann ops->changelink = macvlan_changelink; 1462fc0663d6SArnd Bergmann ops->get_size = macvlan_get_size; 1463fc0663d6SArnd Bergmann ops->fill_info = macvlan_fill_info; 1464fc0663d6SArnd Bergmann 1465fc0663d6SArnd Bergmann return rtnl_link_register(ops); 1466fc0663d6SArnd Bergmann }; 1467fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_link_register); 1468fc0663d6SArnd Bergmann 1469fc0663d6SArnd Bergmann static struct rtnl_link_ops macvlan_link_ops = { 1470b863ceb7SPatrick McHardy .kind = "macvlan", 14718a35747aSHerbert Xu .setup = macvlan_setup, 1472b863ceb7SPatrick McHardy .newlink = macvlan_newlink, 1473b863ceb7SPatrick McHardy .dellink = macvlan_dellink, 1474b863ceb7SPatrick McHardy }; 1475b863ceb7SPatrick McHardy 1476b863ceb7SPatrick McHardy static int macvlan_device_event(struct notifier_block *unused, 1477b863ceb7SPatrick McHardy unsigned long event, void *ptr) 1478b863ceb7SPatrick McHardy { 1479351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 1480b863ceb7SPatrick McHardy struct macvlan_dev *vlan, *next; 1481b863ceb7SPatrick McHardy struct macvlan_port *port; 1482226bd341SEric Dumazet LIST_HEAD(list_kill); 1483b863ceb7SPatrick McHardy 1484a35e2c1bSJiri Pirko if (!macvlan_port_exists(dev)) 1485b863ceb7SPatrick McHardy return NOTIFY_DONE; 1486b863ceb7SPatrick McHardy 1487e052f7e6SEric Dumazet port = macvlan_port_get_rtnl(dev); 1488a35e2c1bSJiri Pirko 1489b863ceb7SPatrick McHardy switch (event) { 1490b863ceb7SPatrick McHardy case NETDEV_CHANGE: 1491b863ceb7SPatrick McHardy list_for_each_entry(vlan, &port->vlans, list) 1492fc4a7489SPatrick Mullaney netif_stacked_transfer_operstate(vlan->lowerdev, 1493fc4a7489SPatrick Mullaney vlan->dev); 1494b863ceb7SPatrick McHardy break; 1495b863ceb7SPatrick McHardy case NETDEV_FEAT_CHANGE: 1496b863ceb7SPatrick McHardy list_for_each_entry(vlan, &port->vlans, list) { 14978c2acc53SPatrick McHardy vlan->dev->gso_max_size = dev->gso_max_size; 1498797f87f8SFlorian Westphal netdev_update_features(vlan->dev); 1499b863ceb7SPatrick McHardy } 1500b863ceb7SPatrick McHardy break; 15013763e7efSdingtianhong case NETDEV_CHANGEMTU: 15023763e7efSdingtianhong list_for_each_entry(vlan, &port->vlans, list) { 15033763e7efSdingtianhong if (vlan->dev->mtu <= dev->mtu) 15043763e7efSdingtianhong continue; 15053763e7efSdingtianhong dev_set_mtu(vlan->dev, dev->mtu); 15063763e7efSdingtianhong } 15073763e7efSdingtianhong break; 1508e289fd28Sdingtianhong case NETDEV_CHANGEADDR: 1509e289fd28Sdingtianhong if (!port->passthru) 1510e289fd28Sdingtianhong return NOTIFY_DONE; 1511e289fd28Sdingtianhong 1512e289fd28Sdingtianhong vlan = list_first_entry_or_null(&port->vlans, 1513e289fd28Sdingtianhong struct macvlan_dev, 1514e289fd28Sdingtianhong list); 1515e289fd28Sdingtianhong 1516e289fd28Sdingtianhong if (macvlan_sync_address(vlan->dev, dev->dev_addr)) 1517e289fd28Sdingtianhong return NOTIFY_BAD; 1518e289fd28Sdingtianhong 1519e289fd28Sdingtianhong break; 1520b863ceb7SPatrick McHardy case NETDEV_UNREGISTER: 15213b27e105SDavid Lamparter /* twiddle thumbs on netns device moves */ 15223b27e105SDavid Lamparter if (dev->reg_state != NETREG_UNREGISTERING) 15233b27e105SDavid Lamparter break; 15243b27e105SDavid Lamparter 1525b863ceb7SPatrick McHardy list_for_each_entry_safe(vlan, next, &port->vlans, list) 1526226bd341SEric Dumazet vlan->dev->rtnl_link_ops->dellink(vlan->dev, &list_kill); 1527226bd341SEric Dumazet unregister_netdevice_many(&list_kill); 1528b863ceb7SPatrick McHardy break; 15291c01fe14SJiri Pirko case NETDEV_PRE_TYPE_CHANGE: 15301c01fe14SJiri Pirko /* Forbid underlaying device to change its type. */ 15311c01fe14SJiri Pirko return NOTIFY_BAD; 15324c991255SVlad Yasevich 15334c991255SVlad Yasevich case NETDEV_NOTIFY_PEERS: 15344c991255SVlad Yasevich case NETDEV_BONDING_FAILOVER: 15354c991255SVlad Yasevich case NETDEV_RESEND_IGMP: 15364c991255SVlad Yasevich /* Propagate to all vlans */ 15374c991255SVlad Yasevich list_for_each_entry(vlan, &port->vlans, list) 15384c991255SVlad Yasevich call_netdevice_notifiers(event, vlan->dev); 1539b863ceb7SPatrick McHardy } 1540b863ceb7SPatrick McHardy return NOTIFY_DONE; 1541b863ceb7SPatrick McHardy } 1542b863ceb7SPatrick McHardy 1543b863ceb7SPatrick McHardy static struct notifier_block macvlan_notifier_block __read_mostly = { 1544b863ceb7SPatrick McHardy .notifier_call = macvlan_device_event, 1545b863ceb7SPatrick McHardy }; 1546b863ceb7SPatrick McHardy 1547b863ceb7SPatrick McHardy static int __init macvlan_init_module(void) 1548b863ceb7SPatrick McHardy { 1549b863ceb7SPatrick McHardy int err; 1550b863ceb7SPatrick McHardy 1551b863ceb7SPatrick McHardy register_netdevice_notifier(&macvlan_notifier_block); 1552b863ceb7SPatrick McHardy 1553fc0663d6SArnd Bergmann err = macvlan_link_register(&macvlan_link_ops); 1554b863ceb7SPatrick McHardy if (err < 0) 1555b863ceb7SPatrick McHardy goto err1; 1556b863ceb7SPatrick McHardy return 0; 1557b863ceb7SPatrick McHardy err1: 1558b863ceb7SPatrick McHardy unregister_netdevice_notifier(&macvlan_notifier_block); 1559b863ceb7SPatrick McHardy return err; 1560b863ceb7SPatrick McHardy } 1561b863ceb7SPatrick McHardy 1562b863ceb7SPatrick McHardy static void __exit macvlan_cleanup_module(void) 1563b863ceb7SPatrick McHardy { 1564b863ceb7SPatrick McHardy rtnl_link_unregister(&macvlan_link_ops); 1565b863ceb7SPatrick McHardy unregister_netdevice_notifier(&macvlan_notifier_block); 1566b863ceb7SPatrick McHardy } 1567b863ceb7SPatrick McHardy 1568b863ceb7SPatrick McHardy module_init(macvlan_init_module); 1569b863ceb7SPatrick McHardy module_exit(macvlan_cleanup_module); 1570b863ceb7SPatrick McHardy 1571b863ceb7SPatrick McHardy MODULE_LICENSE("GPL"); 1572b863ceb7SPatrick McHardy MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); 1573b863ceb7SPatrick McHardy MODULE_DESCRIPTION("Driver for MAC address based VLANs"); 1574b863ceb7SPatrick McHardy MODULE_ALIAS_RTNL_LINK("macvlan"); 1575