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 275fe0ca732SEric 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)) { 41519bcf9f2SEric W. Biederman skb = ip_check_defrag(dev_net(skb->dev), skb, IP_DEFRAG_MACVLAN); 416bc416d97SEric Dumazet if (!skb) 417bc416d97SEric Dumazet return RX_HANDLER_CONSUMED; 418e639b8d8SSabrina Dubroca *pskb = skb; 4194ec7ac12SEric Dumazet eth = eth_hdr(skb); 42079cf79abSMichael Braun macvlan_forward_source(skb, port, eth->h_source); 421618e1b74SArnd Bergmann src = macvlan_hash_lookup(port, eth->h_source); 422412ca155SHerbert Xu if (src && src->mode != MACVLAN_MODE_VEPA && 423412ca155SHerbert Xu src->mode != MACVLAN_MODE_BRIDGE) { 424729e72a1Sstephen hemminger /* forward to original port. */ 425729e72a1Sstephen hemminger vlan = src; 426412ca155SHerbert Xu ret = macvlan_broadcast_one(skb, vlan, eth, 0) ?: 427412ca155SHerbert Xu netif_rx(skb); 428d1dd9119Sjbaron@akamai.com handle_res = RX_HANDLER_CONSUMED; 429729e72a1Sstephen hemminger goto out; 430729e72a1Sstephen hemminger } 431729e72a1Sstephen hemminger 432412ca155SHerbert Xu MACVLAN_SKB_CB(skb)->src = src; 433412ca155SHerbert Xu macvlan_broadcast_enqueue(port, skb); 434412ca155SHerbert Xu 4358a4eb573SJiri Pirko return RX_HANDLER_PASS; 436b863ceb7SPatrick McHardy } 437b863ceb7SPatrick McHardy 43879cf79abSMichael Braun macvlan_forward_source(skb, port, eth->h_source); 439eb06acdcSSridhar Samudrala if (port->passthru) 440233c7df0SJiri Pirko vlan = list_first_or_null_rcu(&port->vlans, 441233c7df0SJiri Pirko struct macvlan_dev, list); 442eb06acdcSSridhar Samudrala else 443b863ceb7SPatrick McHardy vlan = macvlan_hash_lookup(port, eth->h_dest); 444b863ceb7SPatrick McHardy if (vlan == NULL) 4458a4eb573SJiri Pirko return RX_HANDLER_PASS; 446b863ceb7SPatrick McHardy 447b863ceb7SPatrick McHardy dev = vlan->dev; 448b863ceb7SPatrick McHardy if (unlikely(!(dev->flags & IFF_UP))) { 449b863ceb7SPatrick McHardy kfree_skb(skb); 4508a4eb573SJiri Pirko return RX_HANDLER_CONSUMED; 451b863ceb7SPatrick McHardy } 452a1e514c5SArnd Bergmann len = skb->len + ETH_HLEN; 453b863ceb7SPatrick McHardy skb = skb_share_check(skb, GFP_ATOMIC); 454d1dd9119Sjbaron@akamai.com if (!skb) { 455d1dd9119Sjbaron@akamai.com ret = NET_RX_DROP; 456d1dd9119Sjbaron@akamai.com handle_res = RX_HANDLER_CONSUMED; 457ba01877fSSridhar Samudrala goto out; 458d1dd9119Sjbaron@akamai.com } 459b863ceb7SPatrick McHardy 460e639b8d8SSabrina Dubroca *pskb = skb; 461b863ceb7SPatrick McHardy skb->dev = dev; 462b863ceb7SPatrick McHardy skb->pkt_type = PACKET_HOST; 463b863ceb7SPatrick McHardy 464d1dd9119Sjbaron@akamai.com ret = NET_RX_SUCCESS; 465d1dd9119Sjbaron@akamai.com handle_res = RX_HANDLER_ANOTHER; 466ba01877fSSridhar Samudrala out: 4674c979935Sjbaron@akamai.com macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, false); 468d1dd9119Sjbaron@akamai.com return handle_res; 469b863ceb7SPatrick McHardy } 470b863ceb7SPatrick McHardy 471618e1b74SArnd Bergmann static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev) 472618e1b74SArnd Bergmann { 473618e1b74SArnd Bergmann const struct macvlan_dev *vlan = netdev_priv(dev); 474618e1b74SArnd Bergmann const struct macvlan_port *port = vlan->port; 475618e1b74SArnd Bergmann const struct macvlan_dev *dest; 476618e1b74SArnd Bergmann 477618e1b74SArnd Bergmann if (vlan->mode == MACVLAN_MODE_BRIDGE) { 478618e1b74SArnd Bergmann const struct ethhdr *eth = (void *)skb->data; 479618e1b74SArnd Bergmann 480618e1b74SArnd Bergmann /* send to other bridge ports directly */ 481618e1b74SArnd Bergmann if (is_multicast_ether_addr(eth->h_dest)) { 482618e1b74SArnd Bergmann macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE); 483618e1b74SArnd Bergmann goto xmit_world; 484618e1b74SArnd Bergmann } 485618e1b74SArnd Bergmann 486618e1b74SArnd Bergmann dest = macvlan_hash_lookup(port, eth->h_dest); 487618e1b74SArnd Bergmann if (dest && dest->mode == MACVLAN_MODE_BRIDGE) { 488a37dd333SDavid Ward /* send to lowerdev first for its network taps */ 489cb2d0f3eSDavid Ward dev_forward_skb(vlan->lowerdev, skb); 490618e1b74SArnd Bergmann 491618e1b74SArnd Bergmann return NET_XMIT_SUCCESS; 492618e1b74SArnd Bergmann } 493618e1b74SArnd Bergmann } 494618e1b74SArnd Bergmann 495618e1b74SArnd Bergmann xmit_world: 49659b9997bSDavid S. Miller skb->dev = vlan->lowerdev; 497618e1b74SArnd Bergmann return dev_queue_xmit(skb); 498618e1b74SArnd Bergmann } 499618e1b74SArnd Bergmann 500688cea83Sdingtianhong static inline netdev_tx_t macvlan_netpoll_send_skb(struct macvlan_dev *vlan, struct sk_buff *skb) 501688cea83Sdingtianhong { 502688cea83Sdingtianhong #ifdef CONFIG_NET_POLL_CONTROLLER 503688cea83Sdingtianhong if (vlan->netpoll) 504688cea83Sdingtianhong netpoll_send_skb(vlan->netpoll, skb); 505688cea83Sdingtianhong #else 506688cea83Sdingtianhong BUG(); 507688cea83Sdingtianhong #endif 508688cea83Sdingtianhong return NETDEV_TX_OK; 509688cea83Sdingtianhong } 510688cea83Sdingtianhong 5110db901bdSstephen hemminger static netdev_tx_t macvlan_start_xmit(struct sk_buff *skb, 512424efe9cSStephen Hemminger struct net_device *dev) 513b863ceb7SPatrick McHardy { 514b863ceb7SPatrick McHardy unsigned int len = skb->len; 515b863ceb7SPatrick McHardy int ret; 516688cea83Sdingtianhong struct macvlan_dev *vlan = netdev_priv(dev); 517688cea83Sdingtianhong 518688cea83Sdingtianhong if (unlikely(netpoll_tx_running(dev))) 519688cea83Sdingtianhong return macvlan_netpoll_send_skb(vlan, skb); 520b863ceb7SPatrick McHardy 521a6cc0cfaSJohn Fastabend if (vlan->fwd_priv) { 522a6cc0cfaSJohn Fastabend skb->dev = vlan->lowerdev; 523f663dd9aSJason Wang ret = dev_queue_xmit_accel(skb, vlan->fwd_priv); 524a6cc0cfaSJohn Fastabend } else { 525618e1b74SArnd Bergmann ret = macvlan_queue_xmit(skb, dev); 526a6cc0cfaSJohn Fastabend } 527a6cc0cfaSJohn Fastabend 5282d6c9ffcSEric Dumazet if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) { 529cdf3e274SLi RongQing struct vlan_pcpu_stats *pcpu_stats; 5302c114553SEric Dumazet 5318ffab51bSEric Dumazet pcpu_stats = this_cpu_ptr(vlan->pcpu_stats); 5328ffab51bSEric Dumazet u64_stats_update_begin(&pcpu_stats->syncp); 5338ffab51bSEric Dumazet pcpu_stats->tx_packets++; 5348ffab51bSEric Dumazet pcpu_stats->tx_bytes += len; 5358ffab51bSEric Dumazet u64_stats_update_end(&pcpu_stats->syncp); 5368ffab51bSEric Dumazet } else { 5378ffab51bSEric Dumazet this_cpu_inc(vlan->pcpu_stats->tx_dropped); 5388ffab51bSEric Dumazet } 539cbbef5e1SPatrick McHardy return ret; 540b863ceb7SPatrick McHardy } 541b863ceb7SPatrick McHardy 542b863ceb7SPatrick McHardy static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev, 5433b04dddeSStephen Hemminger unsigned short type, const void *daddr, 5443b04dddeSStephen Hemminger const void *saddr, unsigned len) 545b863ceb7SPatrick McHardy { 546b863ceb7SPatrick McHardy const struct macvlan_dev *vlan = netdev_priv(dev); 547b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 548b863ceb7SPatrick McHardy 5490c4e8581SStephen Hemminger return dev_hard_header(skb, lowerdev, type, daddr, 550b863ceb7SPatrick McHardy saddr ? : dev->dev_addr, len); 551b863ceb7SPatrick McHardy } 552b863ceb7SPatrick McHardy 5533b04dddeSStephen Hemminger static const struct header_ops macvlan_hard_header_ops = { 5543b04dddeSStephen Hemminger .create = macvlan_hard_header, 5553b04dddeSStephen Hemminger .parse = eth_header_parse, 5563b04dddeSStephen Hemminger .cache = eth_header_cache, 5573b04dddeSStephen Hemminger .cache_update = eth_header_cache_update, 5583b04dddeSStephen Hemminger }; 5593b04dddeSStephen Hemminger 560b13ba1b8SJason Wang static struct rtnl_link_ops macvlan_link_ops; 561b13ba1b8SJason Wang 562b863ceb7SPatrick McHardy static int macvlan_open(struct net_device *dev) 563b863ceb7SPatrick McHardy { 564b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 565b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 566b863ceb7SPatrick McHardy int err; 567b863ceb7SPatrick McHardy 568eb06acdcSSridhar Samudrala if (vlan->port->passthru) { 56978738141SMichael S. Tsirkin if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) { 57078738141SMichael S. Tsirkin err = dev_set_promiscuity(lowerdev, 1); 57178738141SMichael S. Tsirkin if (err < 0) 57278738141SMichael S. Tsirkin goto out; 57378738141SMichael S. Tsirkin } 574eb06acdcSSridhar Samudrala goto hash_add; 575eb06acdcSSridhar Samudrala } 576eb06acdcSSridhar Samudrala 577b13ba1b8SJason Wang if (lowerdev->features & NETIF_F_HW_L2FW_DOFFLOAD && 578b13ba1b8SJason Wang dev->rtnl_link_ops == &macvlan_link_ops) { 579a6cc0cfaSJohn Fastabend vlan->fwd_priv = 580a6cc0cfaSJohn Fastabend lowerdev->netdev_ops->ndo_dfwd_add_station(lowerdev, dev); 581a6cc0cfaSJohn Fastabend 582a6cc0cfaSJohn Fastabend /* If we get a NULL pointer back, or if we get an error 583a6cc0cfaSJohn Fastabend * then we should just fall through to the non accelerated path 584a6cc0cfaSJohn Fastabend */ 585a6cc0cfaSJohn Fastabend if (IS_ERR_OR_NULL(vlan->fwd_priv)) { 586a6cc0cfaSJohn Fastabend vlan->fwd_priv = NULL; 587f663dd9aSJason Wang } else 588a6cc0cfaSJohn Fastabend return 0; 589a6cc0cfaSJohn Fastabend } 590a6cc0cfaSJohn Fastabend 591f9ac30f0SEric Biederman err = -EBUSY; 592f9ac30f0SEric Biederman if (macvlan_addr_busy(vlan->port, dev->dev_addr)) 593f9ac30f0SEric Biederman goto out; 594f9ac30f0SEric Biederman 595a748ee24SJiri Pirko err = dev_uc_add(lowerdev, dev->dev_addr); 596b863ceb7SPatrick McHardy if (err < 0) 597b89fb7daSWang Chen goto out; 598b89fb7daSWang Chen if (dev->flags & IFF_ALLMULTI) { 599b89fb7daSWang Chen err = dev_set_allmulti(lowerdev, 1); 600b89fb7daSWang Chen if (err < 0) 601b89fb7daSWang Chen goto del_unicast; 602b89fb7daSWang Chen } 603eb06acdcSSridhar Samudrala 604efdbd2b3SVlad Yasevich if (dev->flags & IFF_PROMISC) { 605efdbd2b3SVlad Yasevich err = dev_set_promiscuity(lowerdev, 1); 606efdbd2b3SVlad Yasevich if (err < 0) 607efdbd2b3SVlad Yasevich goto clear_multi; 608efdbd2b3SVlad Yasevich } 609efdbd2b3SVlad Yasevich 610eb06acdcSSridhar Samudrala hash_add: 611f9ac30f0SEric Biederman macvlan_hash_add(vlan); 612b863ceb7SPatrick McHardy return 0; 613b89fb7daSWang Chen 614efdbd2b3SVlad Yasevich clear_multi: 615efdbd2b3SVlad Yasevich dev_set_allmulti(lowerdev, -1); 616b89fb7daSWang Chen del_unicast: 617a748ee24SJiri Pirko dev_uc_del(lowerdev, dev->dev_addr); 618b89fb7daSWang Chen out: 619a6cc0cfaSJohn Fastabend if (vlan->fwd_priv) { 620a6cc0cfaSJohn Fastabend lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev, 621a6cc0cfaSJohn Fastabend vlan->fwd_priv); 622a6cc0cfaSJohn Fastabend vlan->fwd_priv = NULL; 623a6cc0cfaSJohn Fastabend } 624b89fb7daSWang Chen return err; 625b863ceb7SPatrick McHardy } 626b863ceb7SPatrick McHardy 627b863ceb7SPatrick McHardy static int macvlan_stop(struct net_device *dev) 628b863ceb7SPatrick McHardy { 629b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 630b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 631b863ceb7SPatrick McHardy 632a6cc0cfaSJohn Fastabend if (vlan->fwd_priv) { 633a6cc0cfaSJohn Fastabend lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev, 634a6cc0cfaSJohn Fastabend vlan->fwd_priv); 635a6cc0cfaSJohn Fastabend vlan->fwd_priv = NULL; 636a6cc0cfaSJohn Fastabend return 0; 637a6cc0cfaSJohn Fastabend } 638a6cc0cfaSJohn Fastabend 639df8ef8f3SJohn Fastabend dev_uc_unsync(lowerdev, dev); 640df8ef8f3SJohn Fastabend dev_mc_unsync(lowerdev, dev); 641df8ef8f3SJohn Fastabend 642eb06acdcSSridhar Samudrala if (vlan->port->passthru) { 643df8ef8f3SJohn Fastabend if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) 644eb06acdcSSridhar Samudrala dev_set_promiscuity(lowerdev, -1); 645eb06acdcSSridhar Samudrala goto hash_del; 646eb06acdcSSridhar Samudrala } 647eb06acdcSSridhar Samudrala 648b863ceb7SPatrick McHardy if (dev->flags & IFF_ALLMULTI) 649b863ceb7SPatrick McHardy dev_set_allmulti(lowerdev, -1); 650b863ceb7SPatrick McHardy 651efdbd2b3SVlad Yasevich if (dev->flags & IFF_PROMISC) 652efdbd2b3SVlad Yasevich dev_set_promiscuity(lowerdev, -1); 653efdbd2b3SVlad Yasevich 654a748ee24SJiri Pirko dev_uc_del(lowerdev, dev->dev_addr); 655b863ceb7SPatrick McHardy 656eb06acdcSSridhar Samudrala hash_del: 657449f4544SEric Dumazet macvlan_hash_del(vlan, !dev->dismantle); 658b863ceb7SPatrick McHardy return 0; 659b863ceb7SPatrick McHardy } 660b863ceb7SPatrick McHardy 661e289fd28Sdingtianhong static int macvlan_sync_address(struct net_device *dev, unsigned char *addr) 662ad5d20a6SPatrick McHardy { 663ad5d20a6SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 664ad5d20a6SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 665ad5d20a6SPatrick McHardy int err; 666ad5d20a6SPatrick McHardy 667f9ac30f0SEric Biederman if (!(dev->flags & IFF_UP)) { 668f9ac30f0SEric Biederman /* Just copy in the new address */ 669e289fd28Sdingtianhong ether_addr_copy(dev->dev_addr, addr); 670f9ac30f0SEric Biederman } else { 671f9ac30f0SEric Biederman /* Rehash and update the device filters */ 672e289fd28Sdingtianhong if (macvlan_addr_busy(vlan->port, addr)) 673f9ac30f0SEric Biederman return -EBUSY; 674ad5d20a6SPatrick McHardy 675e289fd28Sdingtianhong if (!vlan->port->passthru) { 676e289fd28Sdingtianhong err = dev_uc_add(lowerdev, addr); 677ccffad25SJiri Pirko if (err) 678ad5d20a6SPatrick McHardy return err; 679f9ac30f0SEric Biederman 680a748ee24SJiri Pirko dev_uc_del(lowerdev, dev->dev_addr); 681e289fd28Sdingtianhong } 682ad5d20a6SPatrick McHardy 683e289fd28Sdingtianhong macvlan_hash_change_addr(vlan, addr); 684f9ac30f0SEric Biederman } 685ad5d20a6SPatrick McHardy return 0; 686ad5d20a6SPatrick McHardy } 687ad5d20a6SPatrick McHardy 688e289fd28Sdingtianhong static int macvlan_set_mac_address(struct net_device *dev, void *p) 689e289fd28Sdingtianhong { 690e289fd28Sdingtianhong struct macvlan_dev *vlan = netdev_priv(dev); 691e289fd28Sdingtianhong struct sockaddr *addr = p; 692e289fd28Sdingtianhong 693e289fd28Sdingtianhong if (!is_valid_ether_addr(addr->sa_data)) 694e289fd28Sdingtianhong return -EADDRNOTAVAIL; 695e289fd28Sdingtianhong 696e289fd28Sdingtianhong if (vlan->mode == MACVLAN_MODE_PASSTHRU) { 697e289fd28Sdingtianhong dev_set_mac_address(vlan->lowerdev, addr); 698e289fd28Sdingtianhong return 0; 699e289fd28Sdingtianhong } 700e289fd28Sdingtianhong 701e289fd28Sdingtianhong return macvlan_sync_address(dev, addr->sa_data); 702e289fd28Sdingtianhong } 703e289fd28Sdingtianhong 704b863ceb7SPatrick McHardy static void macvlan_change_rx_flags(struct net_device *dev, int change) 705b863ceb7SPatrick McHardy { 706b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 707b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 708b863ceb7SPatrick McHardy 709bbeb0eadSPeter Christensen if (dev->flags & IFF_UP) { 710b863ceb7SPatrick McHardy if (change & IFF_ALLMULTI) 711b863ceb7SPatrick McHardy dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1); 712efdbd2b3SVlad Yasevich if (change & IFF_PROMISC) 713efdbd2b3SVlad Yasevich dev_set_promiscuity(lowerdev, 714efdbd2b3SVlad Yasevich dev->flags & IFF_PROMISC ? 1 : -1); 715efdbd2b3SVlad Yasevich 716b863ceb7SPatrick McHardy } 717bbeb0eadSPeter Christensen } 718b863ceb7SPatrick McHardy 719df8ef8f3SJohn Fastabend static void macvlan_set_mac_lists(struct net_device *dev) 720b863ceb7SPatrick McHardy { 721b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 722b863ceb7SPatrick McHardy 723cd431e73SEric Dumazet if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) { 724cd431e73SEric Dumazet bitmap_fill(vlan->mc_filter, MACVLAN_MC_FILTER_SZ); 725cd431e73SEric Dumazet } else { 726cd431e73SEric Dumazet struct netdev_hw_addr *ha; 727cd431e73SEric Dumazet DECLARE_BITMAP(filter, MACVLAN_MC_FILTER_SZ); 728cd431e73SEric Dumazet 729cd431e73SEric Dumazet bitmap_zero(filter, MACVLAN_MC_FILTER_SZ); 730cd431e73SEric Dumazet netdev_for_each_mc_addr(ha, dev) { 7313807ff58SEric Dumazet __set_bit(mc_hash(vlan, ha->addr), filter); 732cd431e73SEric Dumazet } 733d5270430SEric Dumazet 7343807ff58SEric Dumazet __set_bit(mc_hash(vlan, dev->broadcast), filter); 735d5270430SEric Dumazet 736cd431e73SEric Dumazet bitmap_copy(vlan->mc_filter, filter, MACVLAN_MC_FILTER_SZ); 737cd431e73SEric Dumazet } 738df8ef8f3SJohn Fastabend dev_uc_sync(vlan->lowerdev, dev); 739b863ceb7SPatrick McHardy dev_mc_sync(vlan->lowerdev, dev); 740b863ceb7SPatrick McHardy } 741b863ceb7SPatrick McHardy 742b863ceb7SPatrick McHardy static int macvlan_change_mtu(struct net_device *dev, int new_mtu) 743b863ceb7SPatrick McHardy { 744b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 745b863ceb7SPatrick McHardy 746b863ceb7SPatrick McHardy if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu) 747b863ceb7SPatrick McHardy return -EINVAL; 748b863ceb7SPatrick McHardy dev->mtu = new_mtu; 749b863ceb7SPatrick McHardy return 0; 750b863ceb7SPatrick McHardy } 751b863ceb7SPatrick McHardy 752b863ceb7SPatrick McHardy /* 753b863ceb7SPatrick McHardy * macvlan network devices have devices nesting below it and are a special 754b863ceb7SPatrick McHardy * "super class" of normal network devices; split their locks off into a 755b863ceb7SPatrick McHardy * separate class since they always nest. 756b863ceb7SPatrick McHardy */ 757b863ceb7SPatrick McHardy static struct lock_class_key macvlan_netdev_xmit_lock_key; 758cf508b12SDavid S. Miller static struct lock_class_key macvlan_netdev_addr_lock_key; 759b863ceb7SPatrick McHardy 7608b4703e9SVlad Yasevich #define ALWAYS_ON_FEATURES \ 761a523a5ecSJason Wang (NETIF_F_SG | NETIF_F_GEN_CSUM | NETIF_F_GSO_SOFTWARE | NETIF_F_LLTX | \ 762a523a5ecSJason Wang NETIF_F_GSO_ROBUST) 7638b4703e9SVlad Yasevich 764b863ceb7SPatrick McHardy #define MACVLAN_FEATURES \ 765*a188222bSTom Herbert (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \ 76662dbe830SMichal Kubeček NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_LRO | \ 7678d13e670SJohn Fastabend NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \ 76828d2b136SPatrick McHardy NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER) 769b863ceb7SPatrick McHardy 770b863ceb7SPatrick McHardy #define MACVLAN_STATE_MASK \ 771b863ceb7SPatrick McHardy ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT)) 772b863ceb7SPatrick McHardy 773c674ac30SVlad Yasevich static int macvlan_get_nest_level(struct net_device *dev) 774c674ac30SVlad Yasevich { 775c674ac30SVlad Yasevich return ((struct macvlan_dev *)netdev_priv(dev))->nest_level; 776c674ac30SVlad Yasevich } 777c674ac30SVlad Yasevich 778e8a0464cSDavid S. Miller static void macvlan_set_lockdep_class_one(struct net_device *dev, 779e8a0464cSDavid S. Miller struct netdev_queue *txq, 780e8a0464cSDavid S. Miller void *_unused) 781c773e847SDavid S. Miller { 782c773e847SDavid S. Miller lockdep_set_class(&txq->_xmit_lock, 783c773e847SDavid S. Miller &macvlan_netdev_xmit_lock_key); 784c773e847SDavid S. Miller } 785c773e847SDavid S. Miller 786c773e847SDavid S. Miller static void macvlan_set_lockdep_class(struct net_device *dev) 787c773e847SDavid S. Miller { 788c674ac30SVlad Yasevich lockdep_set_class_and_subclass(&dev->addr_list_lock, 789c674ac30SVlad Yasevich &macvlan_netdev_addr_lock_key, 790c674ac30SVlad Yasevich macvlan_get_nest_level(dev)); 791e8a0464cSDavid S. Miller netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL); 792c773e847SDavid S. Miller } 793c773e847SDavid S. Miller 794b863ceb7SPatrick McHardy static int macvlan_init(struct net_device *dev) 795b863ceb7SPatrick McHardy { 796b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 797b863ceb7SPatrick McHardy const struct net_device *lowerdev = vlan->lowerdev; 798b863ceb7SPatrick McHardy 799b863ceb7SPatrick McHardy dev->state = (dev->state & ~MACVLAN_STATE_MASK) | 800b863ceb7SPatrick McHardy (lowerdev->state & MACVLAN_STATE_MASK); 801b863ceb7SPatrick McHardy dev->features = lowerdev->features & MACVLAN_FEATURES; 8028b4703e9SVlad Yasevich dev->features |= ALWAYS_ON_FEATURES; 80362dbe830SMichal Kubeček dev->hw_features |= NETIF_F_LRO; 804081e83a7SVlad Yasevich dev->vlan_features = lowerdev->vlan_features & MACVLAN_FEATURES; 8058c2acc53SPatrick McHardy dev->gso_max_size = lowerdev->gso_max_size; 806ef5c8996Ssg.tweak@gmail.com dev->hard_header_len = lowerdev->hard_header_len; 807b863ceb7SPatrick McHardy 808c773e847SDavid S. Miller macvlan_set_lockdep_class(dev); 809c773e847SDavid S. Miller 8101c213bd2SWANG Cong vlan->pcpu_stats = netdev_alloc_pcpu_stats(struct vlan_pcpu_stats); 8118ffab51bSEric Dumazet if (!vlan->pcpu_stats) 812fccaf710SEric Dumazet return -ENOMEM; 813fccaf710SEric Dumazet 814b863ceb7SPatrick McHardy return 0; 815b863ceb7SPatrick McHardy } 816b863ceb7SPatrick McHardy 817fccaf710SEric Dumazet static void macvlan_uninit(struct net_device *dev) 818fccaf710SEric Dumazet { 819fccaf710SEric Dumazet struct macvlan_dev *vlan = netdev_priv(dev); 820d5cd9244SEric W. Biederman struct macvlan_port *port = vlan->port; 821fccaf710SEric Dumazet 8228ffab51bSEric Dumazet free_percpu(vlan->pcpu_stats); 823d5cd9244SEric W. Biederman 82479cf79abSMichael Braun macvlan_flush_sources(port, vlan); 8255e3c516bSDavid S. Miller port->count -= 1; 8265e3c516bSDavid S. Miller if (!port->count) 827d5cd9244SEric W. Biederman macvlan_port_destroy(port->dev); 828fccaf710SEric Dumazet } 829fccaf710SEric Dumazet 83028172739SEric Dumazet static struct rtnl_link_stats64 *macvlan_dev_get_stats64(struct net_device *dev, 83128172739SEric Dumazet struct rtnl_link_stats64 *stats) 832fccaf710SEric Dumazet { 833fccaf710SEric Dumazet struct macvlan_dev *vlan = netdev_priv(dev); 834fccaf710SEric Dumazet 8358ffab51bSEric Dumazet if (vlan->pcpu_stats) { 836cdf3e274SLi RongQing struct vlan_pcpu_stats *p; 8378ffab51bSEric Dumazet u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes; 8388ffab51bSEric Dumazet u32 rx_errors = 0, tx_dropped = 0; 839bc66154eSEric Dumazet unsigned int start; 840fccaf710SEric Dumazet int i; 841fccaf710SEric Dumazet 842fccaf710SEric Dumazet for_each_possible_cpu(i) { 8438ffab51bSEric Dumazet p = per_cpu_ptr(vlan->pcpu_stats, i); 844bc66154eSEric Dumazet do { 84557a7744eSEric W. Biederman start = u64_stats_fetch_begin_irq(&p->syncp); 846bc66154eSEric Dumazet rx_packets = p->rx_packets; 847bc66154eSEric Dumazet rx_bytes = p->rx_bytes; 848bc66154eSEric Dumazet rx_multicast = p->rx_multicast; 8498ffab51bSEric Dumazet tx_packets = p->tx_packets; 8508ffab51bSEric Dumazet tx_bytes = p->tx_bytes; 85157a7744eSEric W. Biederman } while (u64_stats_fetch_retry_irq(&p->syncp, start)); 8528ffab51bSEric Dumazet 8538ffab51bSEric Dumazet stats->rx_packets += rx_packets; 8548ffab51bSEric Dumazet stats->rx_bytes += rx_bytes; 8558ffab51bSEric Dumazet stats->multicast += rx_multicast; 8568ffab51bSEric Dumazet stats->tx_packets += tx_packets; 8578ffab51bSEric Dumazet stats->tx_bytes += tx_bytes; 8588ffab51bSEric Dumazet /* rx_errors & tx_dropped are u32, updated 8598ffab51bSEric Dumazet * without syncp protection. 8608ffab51bSEric Dumazet */ 8618ffab51bSEric Dumazet rx_errors += p->rx_errors; 8628ffab51bSEric Dumazet tx_dropped += p->tx_dropped; 863fccaf710SEric Dumazet } 8648ffab51bSEric Dumazet stats->rx_errors = rx_errors; 8658ffab51bSEric Dumazet stats->rx_dropped = rx_errors; 8668ffab51bSEric Dumazet stats->tx_dropped = tx_dropped; 867fccaf710SEric Dumazet } 868fccaf710SEric Dumazet return stats; 869fccaf710SEric Dumazet } 870fccaf710SEric Dumazet 8718e586137SJiri Pirko static int macvlan_vlan_rx_add_vid(struct net_device *dev, 87280d5c368SPatrick McHardy __be16 proto, u16 vid) 8738d13e670SJohn Fastabend { 8748d13e670SJohn Fastabend struct macvlan_dev *vlan = netdev_priv(dev); 8758d13e670SJohn Fastabend struct net_device *lowerdev = vlan->lowerdev; 8768d13e670SJohn Fastabend 87780d5c368SPatrick McHardy return vlan_vid_add(lowerdev, proto, vid); 8788d13e670SJohn Fastabend } 8798d13e670SJohn Fastabend 8808e586137SJiri Pirko static int macvlan_vlan_rx_kill_vid(struct net_device *dev, 88180d5c368SPatrick McHardy __be16 proto, u16 vid) 8828d13e670SJohn Fastabend { 8838d13e670SJohn Fastabend struct macvlan_dev *vlan = netdev_priv(dev); 8848d13e670SJohn Fastabend struct net_device *lowerdev = vlan->lowerdev; 8858d13e670SJohn Fastabend 88680d5c368SPatrick McHardy vlan_vid_del(lowerdev, proto, vid); 8878e586137SJiri Pirko return 0; 8888d13e670SJohn Fastabend } 8898d13e670SJohn Fastabend 890edc7d573Sstephen hemminger static int macvlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], 891df8ef8f3SJohn Fastabend struct net_device *dev, 892f6f6424bSJiri Pirko const unsigned char *addr, u16 vid, 893df8ef8f3SJohn Fastabend u16 flags) 894df8ef8f3SJohn Fastabend { 895df8ef8f3SJohn Fastabend struct macvlan_dev *vlan = netdev_priv(dev); 896df8ef8f3SJohn Fastabend int err = -EINVAL; 897df8ef8f3SJohn Fastabend 8988a50f11cSVlad Yasevich /* Support unicast filter only on passthru devices. 8998a50f11cSVlad Yasevich * Multicast filter should be allowed on all devices. 9008a50f11cSVlad Yasevich */ 9018a50f11cSVlad Yasevich if (!vlan->port->passthru && is_unicast_ether_addr(addr)) 902df8ef8f3SJohn Fastabend return -EOPNOTSUPP; 903df8ef8f3SJohn Fastabend 904ab2cfbb2SThomas Richter if (flags & NLM_F_REPLACE) 905ab2cfbb2SThomas Richter return -EOPNOTSUPP; 906ab2cfbb2SThomas Richter 907df8ef8f3SJohn Fastabend if (is_unicast_ether_addr(addr)) 908df8ef8f3SJohn Fastabend err = dev_uc_add_excl(dev, addr); 909df8ef8f3SJohn Fastabend else if (is_multicast_ether_addr(addr)) 910df8ef8f3SJohn Fastabend err = dev_mc_add_excl(dev, addr); 911df8ef8f3SJohn Fastabend 912df8ef8f3SJohn Fastabend return err; 913df8ef8f3SJohn Fastabend } 914df8ef8f3SJohn Fastabend 9151690be63SVlad Yasevich static int macvlan_fdb_del(struct ndmsg *ndm, struct nlattr *tb[], 916df8ef8f3SJohn Fastabend struct net_device *dev, 917f6f6424bSJiri Pirko const unsigned char *addr, u16 vid) 918df8ef8f3SJohn Fastabend { 919df8ef8f3SJohn Fastabend struct macvlan_dev *vlan = netdev_priv(dev); 920df8ef8f3SJohn Fastabend int err = -EINVAL; 921df8ef8f3SJohn Fastabend 9228a50f11cSVlad Yasevich /* Support unicast filter only on passthru devices. 9238a50f11cSVlad Yasevich * Multicast filter should be allowed on all devices. 9248a50f11cSVlad Yasevich */ 9258a50f11cSVlad Yasevich if (!vlan->port->passthru && is_unicast_ether_addr(addr)) 926df8ef8f3SJohn Fastabend return -EOPNOTSUPP; 927df8ef8f3SJohn Fastabend 928df8ef8f3SJohn Fastabend if (is_unicast_ether_addr(addr)) 929df8ef8f3SJohn Fastabend err = dev_uc_del(dev, addr); 930df8ef8f3SJohn Fastabend else if (is_multicast_ether_addr(addr)) 931df8ef8f3SJohn Fastabend err = dev_mc_del(dev, addr); 932df8ef8f3SJohn Fastabend 933df8ef8f3SJohn Fastabend return err; 934df8ef8f3SJohn Fastabend } 935df8ef8f3SJohn Fastabend 936b863ceb7SPatrick McHardy static void macvlan_ethtool_get_drvinfo(struct net_device *dev, 937b863ceb7SPatrick McHardy struct ethtool_drvinfo *drvinfo) 938b863ceb7SPatrick McHardy { 9397826d43fSJiri Pirko strlcpy(drvinfo->driver, "macvlan", sizeof(drvinfo->driver)); 9407826d43fSJiri Pirko strlcpy(drvinfo->version, "0.1", sizeof(drvinfo->version)); 941b863ceb7SPatrick McHardy } 942b863ceb7SPatrick McHardy 9439edb8bb6SStephen Hemminger static int macvlan_ethtool_get_settings(struct net_device *dev, 9449edb8bb6SStephen Hemminger struct ethtool_cmd *cmd) 9459edb8bb6SStephen Hemminger { 9469edb8bb6SStephen Hemminger const struct macvlan_dev *vlan = netdev_priv(dev); 9474bc71cb9SJiri Pirko 9484bc71cb9SJiri Pirko return __ethtool_get_settings(vlan->lowerdev, cmd); 9499edb8bb6SStephen Hemminger } 9509edb8bb6SStephen Hemminger 9512be5c767SVlad Yasevich static netdev_features_t macvlan_fix_features(struct net_device *dev, 9522be5c767SVlad Yasevich netdev_features_t features) 9532be5c767SVlad Yasevich { 9542be5c767SVlad Yasevich struct macvlan_dev *vlan = netdev_priv(dev); 95562dbe830SMichal Kubeček netdev_features_t lowerdev_features = vlan->lowerdev->features; 956797f87f8SFlorian Westphal netdev_features_t mask; 9572be5c767SVlad Yasevich 958797f87f8SFlorian Westphal features |= NETIF_F_ALL_FOR_ALL; 959797f87f8SFlorian Westphal features &= (vlan->set_features | ~MACVLAN_FEATURES); 960797f87f8SFlorian Westphal mask = features; 961797f87f8SFlorian Westphal 96262dbe830SMichal Kubeček lowerdev_features &= (features | ~NETIF_F_LRO); 96362dbe830SMichal Kubeček features = netdev_increment_features(lowerdev_features, features, mask); 9648b4703e9SVlad Yasevich features |= ALWAYS_ON_FEATURES; 9650d0162e7SFrancesco Ruggeri features &= ~NETIF_F_NETNS_LOCAL; 966797f87f8SFlorian Westphal 967797f87f8SFlorian Westphal return features; 9682be5c767SVlad Yasevich } 9692be5c767SVlad Yasevich 970688cea83Sdingtianhong #ifdef CONFIG_NET_POLL_CONTROLLER 971688cea83Sdingtianhong static void macvlan_dev_poll_controller(struct net_device *dev) 972688cea83Sdingtianhong { 973688cea83Sdingtianhong return; 974688cea83Sdingtianhong } 975688cea83Sdingtianhong 976688cea83Sdingtianhong static int macvlan_dev_netpoll_setup(struct net_device *dev, struct netpoll_info *npinfo) 977688cea83Sdingtianhong { 978688cea83Sdingtianhong struct macvlan_dev *vlan = netdev_priv(dev); 979688cea83Sdingtianhong struct net_device *real_dev = vlan->lowerdev; 980688cea83Sdingtianhong struct netpoll *netpoll; 981688cea83Sdingtianhong int err = 0; 982688cea83Sdingtianhong 983688cea83Sdingtianhong netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL); 984688cea83Sdingtianhong err = -ENOMEM; 985688cea83Sdingtianhong if (!netpoll) 986688cea83Sdingtianhong goto out; 987688cea83Sdingtianhong 988688cea83Sdingtianhong err = __netpoll_setup(netpoll, real_dev); 989688cea83Sdingtianhong if (err) { 990688cea83Sdingtianhong kfree(netpoll); 991688cea83Sdingtianhong goto out; 992688cea83Sdingtianhong } 993688cea83Sdingtianhong 994688cea83Sdingtianhong vlan->netpoll = netpoll; 995688cea83Sdingtianhong 996688cea83Sdingtianhong out: 997688cea83Sdingtianhong return err; 998688cea83Sdingtianhong } 999688cea83Sdingtianhong 1000688cea83Sdingtianhong static void macvlan_dev_netpoll_cleanup(struct net_device *dev) 1001688cea83Sdingtianhong { 1002688cea83Sdingtianhong struct macvlan_dev *vlan = netdev_priv(dev); 1003688cea83Sdingtianhong struct netpoll *netpoll = vlan->netpoll; 1004688cea83Sdingtianhong 1005688cea83Sdingtianhong if (!netpoll) 1006688cea83Sdingtianhong return; 1007688cea83Sdingtianhong 1008688cea83Sdingtianhong vlan->netpoll = NULL; 1009688cea83Sdingtianhong 1010688cea83Sdingtianhong __netpoll_free_async(netpoll); 1011688cea83Sdingtianhong } 1012688cea83Sdingtianhong #endif /* CONFIG_NET_POLL_CONTROLLER */ 1013688cea83Sdingtianhong 1014ef5fa6bcSNicolas Dichtel static int macvlan_dev_get_iflink(const struct net_device *dev) 1015ef5fa6bcSNicolas Dichtel { 1016ef5fa6bcSNicolas Dichtel struct macvlan_dev *vlan = netdev_priv(dev); 1017ef5fa6bcSNicolas Dichtel 1018ef5fa6bcSNicolas Dichtel return vlan->lowerdev->ifindex; 1019ef5fa6bcSNicolas Dichtel } 1020ef5fa6bcSNicolas Dichtel 1021b863ceb7SPatrick McHardy static const struct ethtool_ops macvlan_ethtool_ops = { 1022b863ceb7SPatrick McHardy .get_link = ethtool_op_get_link, 10239edb8bb6SStephen Hemminger .get_settings = macvlan_ethtool_get_settings, 1024b863ceb7SPatrick McHardy .get_drvinfo = macvlan_ethtool_get_drvinfo, 1025b863ceb7SPatrick McHardy }; 1026b863ceb7SPatrick McHardy 102754a30c97SStephen Hemminger static const struct net_device_ops macvlan_netdev_ops = { 102854a30c97SStephen Hemminger .ndo_init = macvlan_init, 1029fccaf710SEric Dumazet .ndo_uninit = macvlan_uninit, 103054a30c97SStephen Hemminger .ndo_open = macvlan_open, 103154a30c97SStephen Hemminger .ndo_stop = macvlan_stop, 103200829823SStephen Hemminger .ndo_start_xmit = macvlan_start_xmit, 103354a30c97SStephen Hemminger .ndo_change_mtu = macvlan_change_mtu, 10342be5c767SVlad Yasevich .ndo_fix_features = macvlan_fix_features, 103554a30c97SStephen Hemminger .ndo_change_rx_flags = macvlan_change_rx_flags, 103654a30c97SStephen Hemminger .ndo_set_mac_address = macvlan_set_mac_address, 1037df8ef8f3SJohn Fastabend .ndo_set_rx_mode = macvlan_set_mac_lists, 1038bc66154eSEric Dumazet .ndo_get_stats64 = macvlan_dev_get_stats64, 103954a30c97SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 10408d13e670SJohn Fastabend .ndo_vlan_rx_add_vid = macvlan_vlan_rx_add_vid, 10418d13e670SJohn Fastabend .ndo_vlan_rx_kill_vid = macvlan_vlan_rx_kill_vid, 1042df8ef8f3SJohn Fastabend .ndo_fdb_add = macvlan_fdb_add, 1043df8ef8f3SJohn Fastabend .ndo_fdb_del = macvlan_fdb_del, 1044df8ef8f3SJohn Fastabend .ndo_fdb_dump = ndo_dflt_fdb_dump, 1045c674ac30SVlad Yasevich .ndo_get_lock_subclass = macvlan_get_nest_level, 1046688cea83Sdingtianhong #ifdef CONFIG_NET_POLL_CONTROLLER 1047688cea83Sdingtianhong .ndo_poll_controller = macvlan_dev_poll_controller, 1048688cea83Sdingtianhong .ndo_netpoll_setup = macvlan_dev_netpoll_setup, 1049688cea83Sdingtianhong .ndo_netpoll_cleanup = macvlan_dev_netpoll_cleanup, 1050688cea83Sdingtianhong #endif 1051ef5fa6bcSNicolas Dichtel .ndo_get_iflink = macvlan_dev_get_iflink, 1052f56e67b5SToshiaki Makita .ndo_features_check = passthru_features_check, 105354a30c97SStephen Hemminger }; 105454a30c97SStephen Hemminger 10558a35747aSHerbert Xu void macvlan_common_setup(struct net_device *dev) 1056b863ceb7SPatrick McHardy { 1057b863ceb7SPatrick McHardy ether_setup(dev); 1058b863ceb7SPatrick McHardy 105902875878SEric Dumazet dev->priv_flags &= ~IFF_TX_SKB_SHARING; 106002875878SEric Dumazet netif_keep_dst(dev); 106187ab7f6fSVlad Yasevich dev->priv_flags |= IFF_UNICAST_FLT; 106254a30c97SStephen Hemminger dev->netdev_ops = &macvlan_netdev_ops; 1063b863ceb7SPatrick McHardy dev->destructor = free_netdev; 106471740129SLutz Jaenicke dev->header_ops = &macvlan_hard_header_ops; 1065b863ceb7SPatrick McHardy dev->ethtool_ops = &macvlan_ethtool_ops; 10668a35747aSHerbert Xu } 10678a35747aSHerbert Xu EXPORT_SYMBOL_GPL(macvlan_common_setup); 10688a35747aSHerbert Xu 10698a35747aSHerbert Xu static void macvlan_setup(struct net_device *dev) 10708a35747aSHerbert Xu { 10718a35747aSHerbert Xu macvlan_common_setup(dev); 1072b863ceb7SPatrick McHardy dev->tx_queue_len = 0; 1073b863ceb7SPatrick McHardy } 1074b863ceb7SPatrick McHardy 1075b863ceb7SPatrick McHardy static int macvlan_port_create(struct net_device *dev) 1076b863ceb7SPatrick McHardy { 1077b863ceb7SPatrick McHardy struct macvlan_port *port; 1078b863ceb7SPatrick McHardy unsigned int i; 1079ab95bfe0SJiri Pirko int err; 1080b863ceb7SPatrick McHardy 1081b863ceb7SPatrick McHardy if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK) 1082b863ceb7SPatrick McHardy return -EINVAL; 1083b863ceb7SPatrick McHardy 1084d6b00fecSMahesh Bandewar if (netif_is_ipvlan_port(dev)) 1085d6b00fecSMahesh Bandewar return -EBUSY; 1086d6b00fecSMahesh Bandewar 1087b863ceb7SPatrick McHardy port = kzalloc(sizeof(*port), GFP_KERNEL); 1088b863ceb7SPatrick McHardy if (port == NULL) 1089b863ceb7SPatrick McHardy return -ENOMEM; 1090b863ceb7SPatrick McHardy 1091eb06acdcSSridhar Samudrala port->passthru = false; 1092b863ceb7SPatrick McHardy port->dev = dev; 1093b863ceb7SPatrick McHardy INIT_LIST_HEAD(&port->vlans); 1094b863ceb7SPatrick McHardy for (i = 0; i < MACVLAN_HASH_SIZE; i++) 1095b863ceb7SPatrick McHardy INIT_HLIST_HEAD(&port->vlan_hash[i]); 109679cf79abSMichael Braun for (i = 0; i < MACVLAN_HASH_SIZE; i++) 109779cf79abSMichael Braun INIT_HLIST_HEAD(&port->vlan_source_hash[i]); 1098ab95bfe0SJiri Pirko 1099412ca155SHerbert Xu skb_queue_head_init(&port->bc_queue); 1100412ca155SHerbert Xu INIT_WORK(&port->bc_work, macvlan_process_broadcast); 1101412ca155SHerbert Xu 1102a35e2c1bSJiri Pirko err = netdev_rx_handler_register(dev, macvlan_handle_frame, port); 1103a35e2c1bSJiri Pirko if (err) 1104ab95bfe0SJiri Pirko kfree(port); 1105d9351561SEric Dumazet else 1106a35e2c1bSJiri Pirko dev->priv_flags |= IFF_MACVLAN_PORT; 1107ab95bfe0SJiri Pirko return err; 1108b863ceb7SPatrick McHardy } 1109b863ceb7SPatrick McHardy 1110b863ceb7SPatrick McHardy static void macvlan_port_destroy(struct net_device *dev) 1111b863ceb7SPatrick McHardy { 1112e052f7e6SEric Dumazet struct macvlan_port *port = macvlan_port_get_rtnl(dev); 1113b863ceb7SPatrick McHardy 1114a35e2c1bSJiri Pirko dev->priv_flags &= ~IFF_MACVLAN_PORT; 1115ab95bfe0SJiri Pirko netdev_rx_handler_unregister(dev); 1116fe0ca732SEric Dumazet 1117fe0ca732SEric Dumazet /* After this point, no packet can schedule bc_work anymore, 1118fe0ca732SEric Dumazet * but we need to cancel it and purge left skbs if any. 1119fe0ca732SEric Dumazet */ 1120fe0ca732SEric Dumazet cancel_work_sync(&port->bc_work); 1121fe0ca732SEric Dumazet __skb_queue_purge(&port->bc_queue); 1122fe0ca732SEric Dumazet 11236e070aecSLai Jiangshan kfree_rcu(port, rcu); 1124b863ceb7SPatrick McHardy } 1125b863ceb7SPatrick McHardy 1126b863ceb7SPatrick McHardy static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[]) 1127b863ceb7SPatrick McHardy { 1128b863ceb7SPatrick McHardy if (tb[IFLA_ADDRESS]) { 1129b863ceb7SPatrick McHardy if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) 1130b863ceb7SPatrick McHardy return -EINVAL; 1131b863ceb7SPatrick McHardy if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) 1132b863ceb7SPatrick McHardy return -EADDRNOTAVAIL; 1133b863ceb7SPatrick McHardy } 113427c0b1a8SArnd Bergmann 113515127478SMichael S. Tsirkin if (data && data[IFLA_MACVLAN_FLAGS] && 113615127478SMichael S. Tsirkin nla_get_u16(data[IFLA_MACVLAN_FLAGS]) & ~MACVLAN_FLAG_NOPROMISC) 113715127478SMichael S. Tsirkin return -EINVAL; 113815127478SMichael S. Tsirkin 113927c0b1a8SArnd Bergmann if (data && data[IFLA_MACVLAN_MODE]) { 114027c0b1a8SArnd Bergmann switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) { 114127c0b1a8SArnd Bergmann case MACVLAN_MODE_PRIVATE: 114227c0b1a8SArnd Bergmann case MACVLAN_MODE_VEPA: 114327c0b1a8SArnd Bergmann case MACVLAN_MODE_BRIDGE: 1144eb06acdcSSridhar Samudrala case MACVLAN_MODE_PASSTHRU: 114579cf79abSMichael Braun case MACVLAN_MODE_SOURCE: 114627c0b1a8SArnd Bergmann break; 114727c0b1a8SArnd Bergmann default: 114827c0b1a8SArnd Bergmann return -EINVAL; 114927c0b1a8SArnd Bergmann } 115027c0b1a8SArnd Bergmann } 115179cf79abSMichael Braun 115279cf79abSMichael Braun if (data && data[IFLA_MACVLAN_MACADDR_MODE]) { 115379cf79abSMichael Braun switch (nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE])) { 115479cf79abSMichael Braun case MACVLAN_MACADDR_ADD: 115579cf79abSMichael Braun case MACVLAN_MACADDR_DEL: 115679cf79abSMichael Braun case MACVLAN_MACADDR_FLUSH: 115779cf79abSMichael Braun case MACVLAN_MACADDR_SET: 115879cf79abSMichael Braun break; 115979cf79abSMichael Braun default: 116079cf79abSMichael Braun return -EINVAL; 116179cf79abSMichael Braun } 116279cf79abSMichael Braun } 116379cf79abSMichael Braun 116479cf79abSMichael Braun if (data && data[IFLA_MACVLAN_MACADDR]) { 116579cf79abSMichael Braun if (nla_len(data[IFLA_MACVLAN_MACADDR]) != ETH_ALEN) 116679cf79abSMichael Braun return -EINVAL; 116779cf79abSMichael Braun 116879cf79abSMichael Braun if (!is_valid_ether_addr(nla_data(data[IFLA_MACVLAN_MACADDR]))) 116979cf79abSMichael Braun return -EADDRNOTAVAIL; 117079cf79abSMichael Braun } 117179cf79abSMichael Braun 117279cf79abSMichael Braun if (data && data[IFLA_MACVLAN_MACADDR_COUNT]) 117379cf79abSMichael Braun return -EINVAL; 117479cf79abSMichael Braun 117579cf79abSMichael Braun return 0; 117679cf79abSMichael Braun } 117779cf79abSMichael Braun 117879cf79abSMichael Braun /** 117979cf79abSMichael Braun * reconfigure list of remote source mac address 118079cf79abSMichael Braun * (only for macvlan devices in source mode) 118179cf79abSMichael Braun * Note regarding alignment: all netlink data is aligned to 4 Byte, which 118279cf79abSMichael Braun * suffices for both ether_addr_copy and ether_addr_equal_64bits usage. 118379cf79abSMichael Braun */ 118479cf79abSMichael Braun static int macvlan_changelink_sources(struct macvlan_dev *vlan, u32 mode, 118579cf79abSMichael Braun struct nlattr *data[]) 118679cf79abSMichael Braun { 118779cf79abSMichael Braun char *addr = NULL; 118879cf79abSMichael Braun int ret, rem, len; 118979cf79abSMichael Braun struct nlattr *nla, *head; 119079cf79abSMichael Braun struct macvlan_source_entry *entry; 119179cf79abSMichael Braun 119279cf79abSMichael Braun if (data[IFLA_MACVLAN_MACADDR]) 119379cf79abSMichael Braun addr = nla_data(data[IFLA_MACVLAN_MACADDR]); 119479cf79abSMichael Braun 119579cf79abSMichael Braun if (mode == MACVLAN_MACADDR_ADD) { 119679cf79abSMichael Braun if (!addr) 119779cf79abSMichael Braun return -EINVAL; 119879cf79abSMichael Braun 119979cf79abSMichael Braun return macvlan_hash_add_source(vlan, addr); 120079cf79abSMichael Braun 120179cf79abSMichael Braun } else if (mode == MACVLAN_MACADDR_DEL) { 120279cf79abSMichael Braun if (!addr) 120379cf79abSMichael Braun return -EINVAL; 120479cf79abSMichael Braun 120579cf79abSMichael Braun entry = macvlan_hash_lookup_source(vlan, addr); 120679cf79abSMichael Braun if (entry) { 120779cf79abSMichael Braun macvlan_hash_del_source(entry); 120879cf79abSMichael Braun vlan->macaddr_count--; 120979cf79abSMichael Braun } 121079cf79abSMichael Braun } else if (mode == MACVLAN_MACADDR_FLUSH) { 121179cf79abSMichael Braun macvlan_flush_sources(vlan->port, vlan); 121279cf79abSMichael Braun } else if (mode == MACVLAN_MACADDR_SET) { 121379cf79abSMichael Braun macvlan_flush_sources(vlan->port, vlan); 121479cf79abSMichael Braun 121579cf79abSMichael Braun if (addr) { 121679cf79abSMichael Braun ret = macvlan_hash_add_source(vlan, addr); 121779cf79abSMichael Braun if (ret) 121879cf79abSMichael Braun return ret; 121979cf79abSMichael Braun } 122079cf79abSMichael Braun 122179cf79abSMichael Braun if (!data || !data[IFLA_MACVLAN_MACADDR_DATA]) 122279cf79abSMichael Braun return 0; 122379cf79abSMichael Braun 122479cf79abSMichael Braun head = nla_data(data[IFLA_MACVLAN_MACADDR_DATA]); 122579cf79abSMichael Braun len = nla_len(data[IFLA_MACVLAN_MACADDR_DATA]); 122679cf79abSMichael Braun 122779cf79abSMichael Braun nla_for_each_attr(nla, head, len, rem) { 122879cf79abSMichael Braun if (nla_type(nla) != IFLA_MACVLAN_MACADDR || 122979cf79abSMichael Braun nla_len(nla) != ETH_ALEN) 123079cf79abSMichael Braun continue; 123179cf79abSMichael Braun 123279cf79abSMichael Braun addr = nla_data(nla); 123379cf79abSMichael Braun ret = macvlan_hash_add_source(vlan, addr); 123479cf79abSMichael Braun if (ret) 123579cf79abSMichael Braun return ret; 123679cf79abSMichael Braun } 123779cf79abSMichael Braun } else { 123879cf79abSMichael Braun return -EINVAL; 123979cf79abSMichael Braun } 124079cf79abSMichael Braun 1241b863ceb7SPatrick McHardy return 0; 1242b863ceb7SPatrick McHardy } 1243b863ceb7SPatrick McHardy 1244fc0663d6SArnd Bergmann int macvlan_common_newlink(struct net *src_net, struct net_device *dev, 12452f6a1b66SVlad Yasevich struct nlattr *tb[], struct nlattr *data[]) 1246b863ceb7SPatrick McHardy { 1247b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 1248b863ceb7SPatrick McHardy struct macvlan_port *port; 1249b863ceb7SPatrick McHardy struct net_device *lowerdev; 1250b863ceb7SPatrick McHardy int err; 125179cf79abSMichael Braun int macmode; 1252b863ceb7SPatrick McHardy 1253b863ceb7SPatrick McHardy if (!tb[IFLA_LINK]) 1254b863ceb7SPatrick McHardy return -EINVAL; 1255b863ceb7SPatrick McHardy 125681adee47SEric W. Biederman lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK])); 1257b863ceb7SPatrick McHardy if (lowerdev == NULL) 1258b863ceb7SPatrick McHardy return -ENODEV; 1259b863ceb7SPatrick McHardy 1260d70f2cf5SKevin Wallace /* When creating macvlans or macvtaps on top of other macvlans - use 1261b0832a29SEric Biederman * the real device as the lowerdev. 1262a6ca5f1dSPatrick McHardy */ 1263d70f2cf5SKevin Wallace if (netif_is_macvlan(lowerdev)) 1264d70f2cf5SKevin Wallace lowerdev = macvlan_dev_real_dev(lowerdev); 1265a6ca5f1dSPatrick McHardy 1266b863ceb7SPatrick McHardy if (!tb[IFLA_MTU]) 1267b863ceb7SPatrick McHardy dev->mtu = lowerdev->mtu; 1268b863ceb7SPatrick McHardy else if (dev->mtu > lowerdev->mtu) 1269b863ceb7SPatrick McHardy return -EINVAL; 1270b863ceb7SPatrick McHardy 1271b863ceb7SPatrick McHardy if (!tb[IFLA_ADDRESS]) 12727ce5d222SDanny Kukawka eth_hw_addr_random(dev); 1273b863ceb7SPatrick McHardy 1274a35e2c1bSJiri Pirko if (!macvlan_port_exists(lowerdev)) { 1275b863ceb7SPatrick McHardy err = macvlan_port_create(lowerdev); 1276b863ceb7SPatrick McHardy if (err < 0) 1277b863ceb7SPatrick McHardy return err; 1278b863ceb7SPatrick McHardy } 1279e052f7e6SEric Dumazet port = macvlan_port_get_rtnl(lowerdev); 1280b863ceb7SPatrick McHardy 1281eb06acdcSSridhar Samudrala /* Only 1 macvlan device can be created in passthru mode */ 1282eb06acdcSSridhar Samudrala if (port->passthru) 1283eb06acdcSSridhar Samudrala return -EINVAL; 1284eb06acdcSSridhar Samudrala 1285b863ceb7SPatrick McHardy vlan->lowerdev = lowerdev; 1286b863ceb7SPatrick McHardy vlan->dev = dev; 1287b863ceb7SPatrick McHardy vlan->port = port; 12882be5c767SVlad Yasevich vlan->set_features = MACVLAN_FEATURES; 1289c674ac30SVlad Yasevich vlan->nest_level = dev_get_nest_level(lowerdev, netif_is_macvlan) + 1; 1290b863ceb7SPatrick McHardy 129127c0b1a8SArnd Bergmann vlan->mode = MACVLAN_MODE_VEPA; 129227c0b1a8SArnd Bergmann if (data && data[IFLA_MACVLAN_MODE]) 129327c0b1a8SArnd Bergmann vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]); 129427c0b1a8SArnd Bergmann 1295df8ef8f3SJohn Fastabend if (data && data[IFLA_MACVLAN_FLAGS]) 1296df8ef8f3SJohn Fastabend vlan->flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]); 1297df8ef8f3SJohn Fastabend 1298eb06acdcSSridhar Samudrala if (vlan->mode == MACVLAN_MODE_PASSTHRU) { 12995e3c516bSDavid S. Miller if (port->count) 1300eb06acdcSSridhar Samudrala return -EINVAL; 1301eb06acdcSSridhar Samudrala port->passthru = true; 13028b98604eSBjørn Mork eth_hw_addr_inherit(dev, lowerdev); 1303eb06acdcSSridhar Samudrala } 1304eb06acdcSSridhar Samudrala 130579cf79abSMichael Braun if (data && data[IFLA_MACVLAN_MACADDR_MODE]) { 130679cf79abSMichael Braun if (vlan->mode != MACVLAN_MODE_SOURCE) 130779cf79abSMichael Braun return -EINVAL; 130879cf79abSMichael Braun macmode = nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE]); 130979cf79abSMichael Braun err = macvlan_changelink_sources(vlan, macmode, data); 131079cf79abSMichael Braun if (err) 131179cf79abSMichael Braun return err; 131279cf79abSMichael Braun } 131379cf79abSMichael Braun 13145e3c516bSDavid S. Miller port->count += 1; 131547d4ab91SJohn Fastabend err = register_netdevice(dev); 131647d4ab91SJohn Fastabend if (err < 0) 131747d4ab91SJohn Fastabend goto destroy_port; 131847d4ab91SJohn Fastabend 1319a6cc0cfaSJohn Fastabend dev->priv_flags |= IFF_MACVLAN; 13207cd43db7SJiri Pirko err = netdev_upper_dev_link(lowerdev, dev); 13217cd43db7SJiri Pirko if (err) 1322da37705cSCong Wang goto unregister_netdev; 1323b863ceb7SPatrick McHardy 1324233c7df0SJiri Pirko list_add_tail_rcu(&vlan->list, &port->vlans); 1325fc4a7489SPatrick Mullaney netif_stacked_transfer_operstate(lowerdev, dev); 1326f16d3d57SJiri Pirko 1327b863ceb7SPatrick McHardy return 0; 1328f16d3d57SJiri Pirko 1329da37705cSCong Wang unregister_netdev: 1330da37705cSCong Wang unregister_netdevice(dev); 1331f16d3d57SJiri Pirko destroy_port: 13325e3c516bSDavid S. Miller port->count -= 1; 13335e3c516bSDavid S. Miller if (!port->count) 1334f16d3d57SJiri Pirko macvlan_port_destroy(lowerdev); 1335f16d3d57SJiri Pirko 1336f16d3d57SJiri Pirko return err; 1337b863ceb7SPatrick McHardy } 1338fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_common_newlink); 1339b863ceb7SPatrick McHardy 1340fc0663d6SArnd Bergmann static int macvlan_newlink(struct net *src_net, struct net_device *dev, 1341fc0663d6SArnd Bergmann struct nlattr *tb[], struct nlattr *data[]) 1342fc0663d6SArnd Bergmann { 13432f6a1b66SVlad Yasevich return macvlan_common_newlink(src_net, dev, tb, data); 1344fc0663d6SArnd Bergmann } 1345fc0663d6SArnd Bergmann 1346fc0663d6SArnd Bergmann void macvlan_dellink(struct net_device *dev, struct list_head *head) 1347b863ceb7SPatrick McHardy { 1348b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 1349b863ceb7SPatrick McHardy 135079cf79abSMichael Braun if (vlan->mode == MACVLAN_MODE_SOURCE) 135179cf79abSMichael Braun macvlan_flush_sources(vlan->port, vlan); 1352233c7df0SJiri Pirko list_del_rcu(&vlan->list); 135323289a37SEric Dumazet unregister_netdevice_queue(dev, head); 13547cd43db7SJiri Pirko netdev_upper_dev_unlink(vlan->lowerdev, dev); 1355b863ceb7SPatrick McHardy } 1356fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_dellink); 1357b863ceb7SPatrick McHardy 135827c0b1a8SArnd Bergmann static int macvlan_changelink(struct net_device *dev, 135927c0b1a8SArnd Bergmann struct nlattr *tb[], struct nlattr *data[]) 136027c0b1a8SArnd Bergmann { 136127c0b1a8SArnd Bergmann struct macvlan_dev *vlan = netdev_priv(dev); 1362266e8347SMichael S. Tsirkin enum macvlan_mode mode; 1363266e8347SMichael S. Tsirkin bool set_mode = false; 136479cf79abSMichael Braun enum macvlan_macaddr_mode macmode; 136579cf79abSMichael Braun int ret; 1366266e8347SMichael S. Tsirkin 1367266e8347SMichael S. Tsirkin /* Validate mode, but don't set yet: setting flags may fail. */ 1368266e8347SMichael S. Tsirkin if (data && data[IFLA_MACVLAN_MODE]) { 1369266e8347SMichael S. Tsirkin set_mode = true; 1370266e8347SMichael S. Tsirkin mode = nla_get_u32(data[IFLA_MACVLAN_MODE]); 1371266e8347SMichael S. Tsirkin /* Passthrough mode can't be set or cleared dynamically */ 1372266e8347SMichael S. Tsirkin if ((mode == MACVLAN_MODE_PASSTHRU) != 1373266e8347SMichael S. Tsirkin (vlan->mode == MACVLAN_MODE_PASSTHRU)) 1374266e8347SMichael S. Tsirkin return -EINVAL; 137579cf79abSMichael Braun if (vlan->mode == MACVLAN_MODE_SOURCE && 137679cf79abSMichael Braun vlan->mode != mode) 137779cf79abSMichael Braun macvlan_flush_sources(vlan->port, vlan); 1378266e8347SMichael S. Tsirkin } 137999ffc3e7SMichael S. Tsirkin 1380df8ef8f3SJohn Fastabend if (data && data[IFLA_MACVLAN_FLAGS]) { 1381df8ef8f3SJohn Fastabend __u16 flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]); 1382df8ef8f3SJohn Fastabend bool promisc = (flags ^ vlan->flags) & MACVLAN_FLAG_NOPROMISC; 138399ffc3e7SMichael S. Tsirkin if (vlan->port->passthru && promisc) { 138499ffc3e7SMichael S. Tsirkin int err; 1385df8ef8f3SJohn Fastabend 138699ffc3e7SMichael S. Tsirkin if (flags & MACVLAN_FLAG_NOPROMISC) 138799ffc3e7SMichael S. Tsirkin err = dev_set_promiscuity(vlan->lowerdev, -1); 138899ffc3e7SMichael S. Tsirkin else 138999ffc3e7SMichael S. Tsirkin err = dev_set_promiscuity(vlan->lowerdev, 1); 139099ffc3e7SMichael S. Tsirkin if (err < 0) 139199ffc3e7SMichael S. Tsirkin return err; 139299ffc3e7SMichael S. Tsirkin } 1393df8ef8f3SJohn Fastabend vlan->flags = flags; 1394df8ef8f3SJohn Fastabend } 1395266e8347SMichael S. Tsirkin if (set_mode) 1396266e8347SMichael S. Tsirkin vlan->mode = mode; 139779cf79abSMichael Braun if (data && data[IFLA_MACVLAN_MACADDR_MODE]) { 139879cf79abSMichael Braun if (vlan->mode != MACVLAN_MODE_SOURCE) 139979cf79abSMichael Braun return -EINVAL; 140079cf79abSMichael Braun macmode = nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE]); 140179cf79abSMichael Braun ret = macvlan_changelink_sources(vlan, macmode, data); 140279cf79abSMichael Braun if (ret) 140379cf79abSMichael Braun return ret; 140479cf79abSMichael Braun } 140527c0b1a8SArnd Bergmann return 0; 140627c0b1a8SArnd Bergmann } 140727c0b1a8SArnd Bergmann 140879cf79abSMichael Braun static size_t macvlan_get_size_mac(const struct macvlan_dev *vlan) 140979cf79abSMichael Braun { 141079cf79abSMichael Braun if (vlan->macaddr_count == 0) 141179cf79abSMichael Braun return 0; 141279cf79abSMichael Braun return nla_total_size(0) /* IFLA_MACVLAN_MACADDR_DATA */ 141379cf79abSMichael Braun + vlan->macaddr_count * nla_total_size(sizeof(u8) * ETH_ALEN); 141479cf79abSMichael Braun } 141579cf79abSMichael Braun 141627c0b1a8SArnd Bergmann static size_t macvlan_get_size(const struct net_device *dev) 141727c0b1a8SArnd Bergmann { 141879cf79abSMichael Braun struct macvlan_dev *vlan = netdev_priv(dev); 141979cf79abSMichael Braun 142001fe944fSEric Dumazet return (0 142101fe944fSEric Dumazet + nla_total_size(4) /* IFLA_MACVLAN_MODE */ 142201fe944fSEric Dumazet + nla_total_size(2) /* IFLA_MACVLAN_FLAGS */ 142379cf79abSMichael Braun + nla_total_size(4) /* IFLA_MACVLAN_MACADDR_COUNT */ 142479cf79abSMichael Braun + macvlan_get_size_mac(vlan) /* IFLA_MACVLAN_MACADDR */ 142501fe944fSEric Dumazet ); 142627c0b1a8SArnd Bergmann } 142727c0b1a8SArnd Bergmann 142879cf79abSMichael Braun static int macvlan_fill_info_macaddr(struct sk_buff *skb, 142979cf79abSMichael Braun const struct macvlan_dev *vlan, 143079cf79abSMichael Braun const int i) 143179cf79abSMichael Braun { 143279cf79abSMichael Braun struct hlist_head *h = &vlan->port->vlan_source_hash[i]; 143379cf79abSMichael Braun struct macvlan_source_entry *entry; 143479cf79abSMichael Braun 143579cf79abSMichael Braun hlist_for_each_entry_rcu(entry, h, hlist) { 143679cf79abSMichael Braun if (entry->vlan != vlan) 143779cf79abSMichael Braun continue; 143879cf79abSMichael Braun if (nla_put(skb, IFLA_MACVLAN_MACADDR, ETH_ALEN, entry->addr)) 143979cf79abSMichael Braun return 1; 144079cf79abSMichael Braun } 144179cf79abSMichael Braun return 0; 144279cf79abSMichael Braun } 144379cf79abSMichael Braun 144427c0b1a8SArnd Bergmann static int macvlan_fill_info(struct sk_buff *skb, 144527c0b1a8SArnd Bergmann const struct net_device *dev) 144627c0b1a8SArnd Bergmann { 144727c0b1a8SArnd Bergmann struct macvlan_dev *vlan = netdev_priv(dev); 144879cf79abSMichael Braun int i; 144979cf79abSMichael Braun struct nlattr *nest; 145027c0b1a8SArnd Bergmann 1451ead9a76cSDavid S. Miller if (nla_put_u32(skb, IFLA_MACVLAN_MODE, vlan->mode)) 1452ead9a76cSDavid S. Miller goto nla_put_failure; 1453df8ef8f3SJohn Fastabend if (nla_put_u16(skb, IFLA_MACVLAN_FLAGS, vlan->flags)) 1454df8ef8f3SJohn Fastabend goto nla_put_failure; 145579cf79abSMichael Braun if (nla_put_u32(skb, IFLA_MACVLAN_MACADDR_COUNT, vlan->macaddr_count)) 145679cf79abSMichael Braun goto nla_put_failure; 145779cf79abSMichael Braun if (vlan->macaddr_count > 0) { 145879cf79abSMichael Braun nest = nla_nest_start(skb, IFLA_MACVLAN_MACADDR_DATA); 145979cf79abSMichael Braun if (nest == NULL) 146079cf79abSMichael Braun goto nla_put_failure; 146179cf79abSMichael Braun 146279cf79abSMichael Braun for (i = 0; i < MACVLAN_HASH_SIZE; i++) { 146379cf79abSMichael Braun if (macvlan_fill_info_macaddr(skb, vlan, i)) 146479cf79abSMichael Braun goto nla_put_failure; 146579cf79abSMichael Braun } 146679cf79abSMichael Braun nla_nest_end(skb, nest); 146779cf79abSMichael Braun } 146827c0b1a8SArnd Bergmann return 0; 146927c0b1a8SArnd Bergmann 147027c0b1a8SArnd Bergmann nla_put_failure: 147127c0b1a8SArnd Bergmann return -EMSGSIZE; 147227c0b1a8SArnd Bergmann } 147327c0b1a8SArnd Bergmann 147427c0b1a8SArnd Bergmann static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = { 147527c0b1a8SArnd Bergmann [IFLA_MACVLAN_MODE] = { .type = NLA_U32 }, 1476df8ef8f3SJohn Fastabend [IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 }, 147779cf79abSMichael Braun [IFLA_MACVLAN_MACADDR_MODE] = { .type = NLA_U32 }, 147879cf79abSMichael Braun [IFLA_MACVLAN_MACADDR] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 147979cf79abSMichael Braun [IFLA_MACVLAN_MACADDR_DATA] = { .type = NLA_NESTED }, 148079cf79abSMichael Braun [IFLA_MACVLAN_MACADDR_COUNT] = { .type = NLA_U32 }, 148127c0b1a8SArnd Bergmann }; 148227c0b1a8SArnd Bergmann 1483fc0663d6SArnd Bergmann int macvlan_link_register(struct rtnl_link_ops *ops) 1484fc0663d6SArnd Bergmann { 1485fc0663d6SArnd Bergmann /* common fields */ 1486fc0663d6SArnd Bergmann ops->priv_size = sizeof(struct macvlan_dev); 1487fc0663d6SArnd Bergmann ops->validate = macvlan_validate; 1488fc0663d6SArnd Bergmann ops->maxtype = IFLA_MACVLAN_MAX; 1489fc0663d6SArnd Bergmann ops->policy = macvlan_policy; 1490fc0663d6SArnd Bergmann ops->changelink = macvlan_changelink; 1491fc0663d6SArnd Bergmann ops->get_size = macvlan_get_size; 1492fc0663d6SArnd Bergmann ops->fill_info = macvlan_fill_info; 1493fc0663d6SArnd Bergmann 1494fc0663d6SArnd Bergmann return rtnl_link_register(ops); 1495fc0663d6SArnd Bergmann }; 1496fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_link_register); 1497fc0663d6SArnd Bergmann 1498eaca400fSNicolas Dichtel static struct net *macvlan_get_link_net(const struct net_device *dev) 1499eaca400fSNicolas Dichtel { 1500eaca400fSNicolas Dichtel return dev_net(macvlan_dev_real_dev(dev)); 1501eaca400fSNicolas Dichtel } 1502eaca400fSNicolas Dichtel 1503fc0663d6SArnd Bergmann static struct rtnl_link_ops macvlan_link_ops = { 1504b863ceb7SPatrick McHardy .kind = "macvlan", 15058a35747aSHerbert Xu .setup = macvlan_setup, 1506b863ceb7SPatrick McHardy .newlink = macvlan_newlink, 1507b863ceb7SPatrick McHardy .dellink = macvlan_dellink, 1508eaca400fSNicolas Dichtel .get_link_net = macvlan_get_link_net, 1509b863ceb7SPatrick McHardy }; 1510b863ceb7SPatrick McHardy 1511b863ceb7SPatrick McHardy static int macvlan_device_event(struct notifier_block *unused, 1512b863ceb7SPatrick McHardy unsigned long event, void *ptr) 1513b863ceb7SPatrick McHardy { 1514351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 1515b863ceb7SPatrick McHardy struct macvlan_dev *vlan, *next; 1516b863ceb7SPatrick McHardy struct macvlan_port *port; 1517226bd341SEric Dumazet LIST_HEAD(list_kill); 1518b863ceb7SPatrick McHardy 1519a35e2c1bSJiri Pirko if (!macvlan_port_exists(dev)) 1520b863ceb7SPatrick McHardy return NOTIFY_DONE; 1521b863ceb7SPatrick McHardy 1522e052f7e6SEric Dumazet port = macvlan_port_get_rtnl(dev); 1523a35e2c1bSJiri Pirko 1524b863ceb7SPatrick McHardy switch (event) { 1525b863ceb7SPatrick McHardy case NETDEV_CHANGE: 1526b863ceb7SPatrick McHardy list_for_each_entry(vlan, &port->vlans, list) 1527fc4a7489SPatrick Mullaney netif_stacked_transfer_operstate(vlan->lowerdev, 1528fc4a7489SPatrick Mullaney vlan->dev); 1529b863ceb7SPatrick McHardy break; 1530b863ceb7SPatrick McHardy case NETDEV_FEAT_CHANGE: 1531b863ceb7SPatrick McHardy list_for_each_entry(vlan, &port->vlans, list) { 15328c2acc53SPatrick McHardy vlan->dev->gso_max_size = dev->gso_max_size; 1533797f87f8SFlorian Westphal netdev_update_features(vlan->dev); 1534b863ceb7SPatrick McHardy } 1535b863ceb7SPatrick McHardy break; 15363763e7efSdingtianhong case NETDEV_CHANGEMTU: 15373763e7efSdingtianhong list_for_each_entry(vlan, &port->vlans, list) { 15383763e7efSdingtianhong if (vlan->dev->mtu <= dev->mtu) 15393763e7efSdingtianhong continue; 15403763e7efSdingtianhong dev_set_mtu(vlan->dev, dev->mtu); 15413763e7efSdingtianhong } 15423763e7efSdingtianhong break; 1543e289fd28Sdingtianhong case NETDEV_CHANGEADDR: 1544e289fd28Sdingtianhong if (!port->passthru) 1545e289fd28Sdingtianhong return NOTIFY_DONE; 1546e289fd28Sdingtianhong 1547e289fd28Sdingtianhong vlan = list_first_entry_or_null(&port->vlans, 1548e289fd28Sdingtianhong struct macvlan_dev, 1549e289fd28Sdingtianhong list); 1550e289fd28Sdingtianhong 1551e289fd28Sdingtianhong if (macvlan_sync_address(vlan->dev, dev->dev_addr)) 1552e289fd28Sdingtianhong return NOTIFY_BAD; 1553e289fd28Sdingtianhong 1554e289fd28Sdingtianhong break; 1555b863ceb7SPatrick McHardy case NETDEV_UNREGISTER: 15563b27e105SDavid Lamparter /* twiddle thumbs on netns device moves */ 15573b27e105SDavid Lamparter if (dev->reg_state != NETREG_UNREGISTERING) 15583b27e105SDavid Lamparter break; 15593b27e105SDavid Lamparter 1560b863ceb7SPatrick McHardy list_for_each_entry_safe(vlan, next, &port->vlans, list) 1561226bd341SEric Dumazet vlan->dev->rtnl_link_ops->dellink(vlan->dev, &list_kill); 1562226bd341SEric Dumazet unregister_netdevice_many(&list_kill); 1563b863ceb7SPatrick McHardy break; 15641c01fe14SJiri Pirko case NETDEV_PRE_TYPE_CHANGE: 15651c01fe14SJiri Pirko /* Forbid underlaying device to change its type. */ 15661c01fe14SJiri Pirko return NOTIFY_BAD; 15674c991255SVlad Yasevich 15684c991255SVlad Yasevich case NETDEV_NOTIFY_PEERS: 15694c991255SVlad Yasevich case NETDEV_BONDING_FAILOVER: 15704c991255SVlad Yasevich case NETDEV_RESEND_IGMP: 15714c991255SVlad Yasevich /* Propagate to all vlans */ 15724c991255SVlad Yasevich list_for_each_entry(vlan, &port->vlans, list) 15734c991255SVlad Yasevich call_netdevice_notifiers(event, vlan->dev); 1574b863ceb7SPatrick McHardy } 1575b863ceb7SPatrick McHardy return NOTIFY_DONE; 1576b863ceb7SPatrick McHardy } 1577b863ceb7SPatrick McHardy 1578b863ceb7SPatrick McHardy static struct notifier_block macvlan_notifier_block __read_mostly = { 1579b863ceb7SPatrick McHardy .notifier_call = macvlan_device_event, 1580b863ceb7SPatrick McHardy }; 1581b863ceb7SPatrick McHardy 1582b863ceb7SPatrick McHardy static int __init macvlan_init_module(void) 1583b863ceb7SPatrick McHardy { 1584b863ceb7SPatrick McHardy int err; 1585b863ceb7SPatrick McHardy 1586b863ceb7SPatrick McHardy register_netdevice_notifier(&macvlan_notifier_block); 1587b863ceb7SPatrick McHardy 1588fc0663d6SArnd Bergmann err = macvlan_link_register(&macvlan_link_ops); 1589b863ceb7SPatrick McHardy if (err < 0) 1590b863ceb7SPatrick McHardy goto err1; 1591b863ceb7SPatrick McHardy return 0; 1592b863ceb7SPatrick McHardy err1: 1593b863ceb7SPatrick McHardy unregister_netdevice_notifier(&macvlan_notifier_block); 1594b863ceb7SPatrick McHardy return err; 1595b863ceb7SPatrick McHardy } 1596b863ceb7SPatrick McHardy 1597b863ceb7SPatrick McHardy static void __exit macvlan_cleanup_module(void) 1598b863ceb7SPatrick McHardy { 1599b863ceb7SPatrick McHardy rtnl_link_unregister(&macvlan_link_ops); 1600b863ceb7SPatrick McHardy unregister_netdevice_notifier(&macvlan_notifier_block); 1601b863ceb7SPatrick McHardy } 1602b863ceb7SPatrick McHardy 1603b863ceb7SPatrick McHardy module_init(macvlan_init_module); 1604b863ceb7SPatrick McHardy module_exit(macvlan_cleanup_module); 1605b863ceb7SPatrick McHardy 1606b863ceb7SPatrick McHardy MODULE_LICENSE("GPL"); 1607b863ceb7SPatrick McHardy MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); 1608b863ceb7SPatrick McHardy MODULE_DESCRIPTION("Driver for MAC address based VLANs"); 1609b863ceb7SPatrick McHardy MODULE_ALIAS_RTNL_LINK("macvlan"); 1610