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; 46412ca155SHerbert Xu struct sk_buff_head bc_queue; 47412ca155SHerbert Xu struct work_struct bc_work; 48eb06acdcSSridhar Samudrala bool passthru; 495e3c516bSDavid S. Miller int count; 5079cf79abSMichael Braun struct hlist_head vlan_source_hash[MACVLAN_HASH_SIZE]; 519c127a01SHerbert Xu DECLARE_BITMAP(mc_filter, MACVLAN_MC_FILTER_SZ); 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 181d94d0254SGao Feng static bool 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)) 189d94d0254SGao Feng return true; 190f9ac30f0SEric Biederman 191f9ac30f0SEric Biederman if (macvlan_hash_lookup(port, addr)) 192d94d0254SGao Feng return true; 193f9ac30f0SEric Biederman 194d94d0254SGao Feng return false; 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 308260916dfSHerbert Xu if (src) 309260916dfSHerbert Xu dev_put(src->dev); 310412ca155SHerbert Xu kfree_skb(skb); 311412ca155SHerbert Xu } 312412ca155SHerbert Xu } 313412ca155SHerbert Xu 314412ca155SHerbert Xu static void macvlan_broadcast_enqueue(struct macvlan_port *port, 315260916dfSHerbert Xu const struct macvlan_dev *src, 316412ca155SHerbert Xu struct sk_buff *skb) 317412ca155SHerbert Xu { 318e676f197SHerbert Xu struct sk_buff *nskb; 319412ca155SHerbert Xu int err = -ENOMEM; 320412ca155SHerbert Xu 321e676f197SHerbert Xu nskb = skb_clone(skb, GFP_ATOMIC); 322e676f197SHerbert Xu if (!nskb) 323412ca155SHerbert Xu goto err; 324412ca155SHerbert Xu 325260916dfSHerbert Xu MACVLAN_SKB_CB(nskb)->src = src; 326260916dfSHerbert Xu 327412ca155SHerbert Xu spin_lock(&port->bc_queue.lock); 32807d92d5cSNicolas Dichtel if (skb_queue_len(&port->bc_queue) < MACVLAN_BC_QUEUE_LEN) { 329260916dfSHerbert Xu if (src) 330260916dfSHerbert Xu dev_hold(src->dev); 331e676f197SHerbert Xu __skb_queue_tail(&port->bc_queue, nskb); 332412ca155SHerbert Xu err = 0; 333412ca155SHerbert Xu } 334412ca155SHerbert Xu spin_unlock(&port->bc_queue.lock); 335412ca155SHerbert Xu 336412ca155SHerbert Xu if (err) 337e676f197SHerbert Xu goto free_nskb; 338412ca155SHerbert Xu 339412ca155SHerbert Xu schedule_work(&port->bc_work); 340412ca155SHerbert Xu return; 341412ca155SHerbert Xu 342e676f197SHerbert Xu free_nskb: 343e676f197SHerbert Xu kfree_skb(nskb); 344412ca155SHerbert Xu err: 345412ca155SHerbert Xu atomic_long_inc(&skb->dev->rx_dropped); 346412ca155SHerbert Xu } 347412ca155SHerbert Xu 34879cf79abSMichael Braun static void macvlan_flush_sources(struct macvlan_port *port, 34979cf79abSMichael Braun struct macvlan_dev *vlan) 35079cf79abSMichael Braun { 35179cf79abSMichael Braun int i; 35279cf79abSMichael Braun 35379cf79abSMichael Braun for (i = 0; i < MACVLAN_HASH_SIZE; i++) { 35479cf79abSMichael Braun struct hlist_node *h, *n; 35579cf79abSMichael Braun 35679cf79abSMichael Braun hlist_for_each_safe(h, n, &port->vlan_source_hash[i]) { 35779cf79abSMichael Braun struct macvlan_source_entry *entry; 35879cf79abSMichael Braun 35979cf79abSMichael Braun entry = hlist_entry(h, struct macvlan_source_entry, 36079cf79abSMichael Braun hlist); 36179cf79abSMichael Braun if (entry->vlan == vlan) 36279cf79abSMichael Braun macvlan_hash_del_source(entry); 36379cf79abSMichael Braun } 36479cf79abSMichael Braun } 36579cf79abSMichael Braun vlan->macaddr_count = 0; 36679cf79abSMichael Braun } 36779cf79abSMichael Braun 36879cf79abSMichael Braun static void macvlan_forward_source_one(struct sk_buff *skb, 36979cf79abSMichael Braun struct macvlan_dev *vlan) 37079cf79abSMichael Braun { 37179cf79abSMichael Braun struct sk_buff *nskb; 37279cf79abSMichael Braun struct net_device *dev; 37379cf79abSMichael Braun int len; 37479cf79abSMichael Braun int ret; 37579cf79abSMichael Braun 37679cf79abSMichael Braun dev = vlan->dev; 37779cf79abSMichael Braun if (unlikely(!(dev->flags & IFF_UP))) 37879cf79abSMichael Braun return; 37979cf79abSMichael Braun 38079cf79abSMichael Braun nskb = skb_clone(skb, GFP_ATOMIC); 38179cf79abSMichael Braun if (!nskb) 38279cf79abSMichael Braun return; 38379cf79abSMichael Braun 38479cf79abSMichael Braun len = nskb->len + ETH_HLEN; 38579cf79abSMichael Braun nskb->dev = dev; 38679cf79abSMichael Braun nskb->pkt_type = PACKET_HOST; 38779cf79abSMichael Braun 38879cf79abSMichael Braun ret = netif_rx(nskb); 3894c979935Sjbaron@akamai.com macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, false); 39079cf79abSMichael Braun } 39179cf79abSMichael Braun 39279cf79abSMichael Braun static void macvlan_forward_source(struct sk_buff *skb, 39379cf79abSMichael Braun struct macvlan_port *port, 39479cf79abSMichael Braun const unsigned char *addr) 39579cf79abSMichael Braun { 39679cf79abSMichael Braun struct macvlan_source_entry *entry; 39779cf79abSMichael Braun u32 idx = macvlan_eth_hash(addr); 39879cf79abSMichael Braun struct hlist_head *h = &port->vlan_source_hash[idx]; 39979cf79abSMichael Braun 40079cf79abSMichael Braun hlist_for_each_entry_rcu(entry, h, hlist) { 40179cf79abSMichael Braun if (ether_addr_equal_64bits(entry->addr, addr)) 40279cf79abSMichael Braun macvlan_forward_source_one(skb, entry->vlan); 40379cf79abSMichael Braun } 40479cf79abSMichael Braun } 40579cf79abSMichael Braun 406b863ceb7SPatrick McHardy /* called under rcu_read_lock() from netif_receive_skb */ 4078a4eb573SJiri Pirko static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb) 408b863ceb7SPatrick McHardy { 409ab95bfe0SJiri Pirko struct macvlan_port *port; 4108a4eb573SJiri Pirko struct sk_buff *skb = *pskb; 411b863ceb7SPatrick McHardy const struct ethhdr *eth = eth_hdr(skb); 412b863ceb7SPatrick McHardy const struct macvlan_dev *vlan; 413618e1b74SArnd Bergmann const struct macvlan_dev *src; 414b863ceb7SPatrick McHardy struct net_device *dev; 415ba01877fSSridhar Samudrala unsigned int len = 0; 416d1dd9119Sjbaron@akamai.com int ret; 417d1dd9119Sjbaron@akamai.com rx_handler_result_t handle_res; 418b863ceb7SPatrick McHardy 419a35e2c1bSJiri Pirko port = macvlan_port_get_rcu(skb->dev); 420b863ceb7SPatrick McHardy if (is_multicast_ether_addr(eth->h_dest)) { 4219c127a01SHerbert Xu unsigned int hash; 4229c127a01SHerbert Xu 42319bcf9f2SEric W. Biederman skb = ip_check_defrag(dev_net(skb->dev), skb, IP_DEFRAG_MACVLAN); 424bc416d97SEric Dumazet if (!skb) 425bc416d97SEric Dumazet return RX_HANDLER_CONSUMED; 426e639b8d8SSabrina Dubroca *pskb = skb; 4274ec7ac12SEric Dumazet eth = eth_hdr(skb); 42879cf79abSMichael Braun macvlan_forward_source(skb, port, eth->h_source); 429618e1b74SArnd Bergmann src = macvlan_hash_lookup(port, eth->h_source); 430412ca155SHerbert Xu if (src && src->mode != MACVLAN_MODE_VEPA && 431412ca155SHerbert Xu src->mode != MACVLAN_MODE_BRIDGE) { 432729e72a1Sstephen hemminger /* forward to original port. */ 433729e72a1Sstephen hemminger vlan = src; 434412ca155SHerbert Xu ret = macvlan_broadcast_one(skb, vlan, eth, 0) ?: 435412ca155SHerbert Xu netif_rx(skb); 436d1dd9119Sjbaron@akamai.com handle_res = RX_HANDLER_CONSUMED; 437729e72a1Sstephen hemminger goto out; 438729e72a1Sstephen hemminger } 439729e72a1Sstephen hemminger 4409c127a01SHerbert Xu hash = mc_hash(NULL, eth->h_dest); 4419c127a01SHerbert Xu if (test_bit(hash, port->mc_filter)) 442260916dfSHerbert Xu macvlan_broadcast_enqueue(port, src, skb); 443412ca155SHerbert Xu 4448a4eb573SJiri Pirko return RX_HANDLER_PASS; 445b863ceb7SPatrick McHardy } 446b863ceb7SPatrick McHardy 44779cf79abSMichael Braun macvlan_forward_source(skb, port, eth->h_source); 448eb06acdcSSridhar Samudrala if (port->passthru) 449233c7df0SJiri Pirko vlan = list_first_or_null_rcu(&port->vlans, 450233c7df0SJiri Pirko struct macvlan_dev, list); 451eb06acdcSSridhar Samudrala else 452b863ceb7SPatrick McHardy vlan = macvlan_hash_lookup(port, eth->h_dest); 453b863ceb7SPatrick McHardy if (vlan == NULL) 4548a4eb573SJiri Pirko return RX_HANDLER_PASS; 455b863ceb7SPatrick McHardy 456b863ceb7SPatrick McHardy dev = vlan->dev; 457b863ceb7SPatrick McHardy if (unlikely(!(dev->flags & IFF_UP))) { 458b863ceb7SPatrick McHardy kfree_skb(skb); 4598a4eb573SJiri Pirko return RX_HANDLER_CONSUMED; 460b863ceb7SPatrick McHardy } 461a1e514c5SArnd Bergmann len = skb->len + ETH_HLEN; 462b863ceb7SPatrick McHardy skb = skb_share_check(skb, GFP_ATOMIC); 463d1dd9119Sjbaron@akamai.com if (!skb) { 464d1dd9119Sjbaron@akamai.com ret = NET_RX_DROP; 465d1dd9119Sjbaron@akamai.com handle_res = RX_HANDLER_CONSUMED; 466ba01877fSSridhar Samudrala goto out; 467d1dd9119Sjbaron@akamai.com } 468b863ceb7SPatrick McHardy 469e639b8d8SSabrina Dubroca *pskb = skb; 470b863ceb7SPatrick McHardy skb->dev = dev; 471b863ceb7SPatrick McHardy skb->pkt_type = PACKET_HOST; 472b863ceb7SPatrick McHardy 473d1dd9119Sjbaron@akamai.com ret = NET_RX_SUCCESS; 474d1dd9119Sjbaron@akamai.com handle_res = RX_HANDLER_ANOTHER; 475ba01877fSSridhar Samudrala out: 4764c979935Sjbaron@akamai.com macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, false); 477d1dd9119Sjbaron@akamai.com return handle_res; 478b863ceb7SPatrick McHardy } 479b863ceb7SPatrick McHardy 480618e1b74SArnd Bergmann static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev) 481618e1b74SArnd Bergmann { 482618e1b74SArnd Bergmann const struct macvlan_dev *vlan = netdev_priv(dev); 483618e1b74SArnd Bergmann const struct macvlan_port *port = vlan->port; 484618e1b74SArnd Bergmann const struct macvlan_dev *dest; 485618e1b74SArnd Bergmann 486618e1b74SArnd Bergmann if (vlan->mode == MACVLAN_MODE_BRIDGE) { 487618e1b74SArnd Bergmann const struct ethhdr *eth = (void *)skb->data; 488618e1b74SArnd Bergmann 489618e1b74SArnd Bergmann /* send to other bridge ports directly */ 490618e1b74SArnd Bergmann if (is_multicast_ether_addr(eth->h_dest)) { 491618e1b74SArnd Bergmann macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE); 492618e1b74SArnd Bergmann goto xmit_world; 493618e1b74SArnd Bergmann } 494618e1b74SArnd Bergmann 495618e1b74SArnd Bergmann dest = macvlan_hash_lookup(port, eth->h_dest); 496618e1b74SArnd Bergmann if (dest && dest->mode == MACVLAN_MODE_BRIDGE) { 497a37dd333SDavid Ward /* send to lowerdev first for its network taps */ 498cb2d0f3eSDavid Ward dev_forward_skb(vlan->lowerdev, skb); 499618e1b74SArnd Bergmann 500618e1b74SArnd Bergmann return NET_XMIT_SUCCESS; 501618e1b74SArnd Bergmann } 502618e1b74SArnd Bergmann } 503618e1b74SArnd Bergmann 504618e1b74SArnd Bergmann xmit_world: 50559b9997bSDavid S. Miller skb->dev = vlan->lowerdev; 506618e1b74SArnd Bergmann return dev_queue_xmit(skb); 507618e1b74SArnd Bergmann } 508618e1b74SArnd Bergmann 509688cea83Sdingtianhong static inline netdev_tx_t macvlan_netpoll_send_skb(struct macvlan_dev *vlan, struct sk_buff *skb) 510688cea83Sdingtianhong { 511688cea83Sdingtianhong #ifdef CONFIG_NET_POLL_CONTROLLER 512688cea83Sdingtianhong if (vlan->netpoll) 513688cea83Sdingtianhong netpoll_send_skb(vlan->netpoll, skb); 514688cea83Sdingtianhong #else 515688cea83Sdingtianhong BUG(); 516688cea83Sdingtianhong #endif 517688cea83Sdingtianhong return NETDEV_TX_OK; 518688cea83Sdingtianhong } 519688cea83Sdingtianhong 5200db901bdSstephen hemminger static netdev_tx_t macvlan_start_xmit(struct sk_buff *skb, 521424efe9cSStephen Hemminger struct net_device *dev) 522b863ceb7SPatrick McHardy { 523b863ceb7SPatrick McHardy unsigned int len = skb->len; 524b863ceb7SPatrick McHardy int ret; 525688cea83Sdingtianhong struct macvlan_dev *vlan = netdev_priv(dev); 526688cea83Sdingtianhong 527688cea83Sdingtianhong if (unlikely(netpoll_tx_running(dev))) 528688cea83Sdingtianhong return macvlan_netpoll_send_skb(vlan, skb); 529b863ceb7SPatrick McHardy 530a6cc0cfaSJohn Fastabend if (vlan->fwd_priv) { 531a6cc0cfaSJohn Fastabend skb->dev = vlan->lowerdev; 532f663dd9aSJason Wang ret = dev_queue_xmit_accel(skb, vlan->fwd_priv); 533a6cc0cfaSJohn Fastabend } else { 534618e1b74SArnd Bergmann ret = macvlan_queue_xmit(skb, dev); 535a6cc0cfaSJohn Fastabend } 536a6cc0cfaSJohn Fastabend 5372d6c9ffcSEric Dumazet if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) { 538cdf3e274SLi RongQing struct vlan_pcpu_stats *pcpu_stats; 5392c114553SEric Dumazet 5408ffab51bSEric Dumazet pcpu_stats = this_cpu_ptr(vlan->pcpu_stats); 5418ffab51bSEric Dumazet u64_stats_update_begin(&pcpu_stats->syncp); 5428ffab51bSEric Dumazet pcpu_stats->tx_packets++; 5438ffab51bSEric Dumazet pcpu_stats->tx_bytes += len; 5448ffab51bSEric Dumazet u64_stats_update_end(&pcpu_stats->syncp); 5458ffab51bSEric Dumazet } else { 5468ffab51bSEric Dumazet this_cpu_inc(vlan->pcpu_stats->tx_dropped); 5478ffab51bSEric Dumazet } 548cbbef5e1SPatrick McHardy return ret; 549b863ceb7SPatrick McHardy } 550b863ceb7SPatrick McHardy 551b863ceb7SPatrick McHardy static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev, 5523b04dddeSStephen Hemminger unsigned short type, const void *daddr, 5533b04dddeSStephen Hemminger const void *saddr, unsigned len) 554b863ceb7SPatrick McHardy { 555b863ceb7SPatrick McHardy const struct macvlan_dev *vlan = netdev_priv(dev); 556b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 557b863ceb7SPatrick McHardy 5580c4e8581SStephen Hemminger return dev_hard_header(skb, lowerdev, type, daddr, 559b863ceb7SPatrick McHardy saddr ? : dev->dev_addr, len); 560b863ceb7SPatrick McHardy } 561b863ceb7SPatrick McHardy 5623b04dddeSStephen Hemminger static const struct header_ops macvlan_hard_header_ops = { 5633b04dddeSStephen Hemminger .create = macvlan_hard_header, 5643b04dddeSStephen Hemminger .parse = eth_header_parse, 5653b04dddeSStephen Hemminger .cache = eth_header_cache, 5663b04dddeSStephen Hemminger .cache_update = eth_header_cache_update, 5673b04dddeSStephen Hemminger }; 5683b04dddeSStephen Hemminger 569b13ba1b8SJason Wang static struct rtnl_link_ops macvlan_link_ops; 570b13ba1b8SJason Wang 571b863ceb7SPatrick McHardy static int macvlan_open(struct net_device *dev) 572b863ceb7SPatrick McHardy { 573b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 574b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 575b863ceb7SPatrick McHardy int err; 576b863ceb7SPatrick McHardy 577eb06acdcSSridhar Samudrala if (vlan->port->passthru) { 57878738141SMichael S. Tsirkin if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) { 57978738141SMichael S. Tsirkin err = dev_set_promiscuity(lowerdev, 1); 58078738141SMichael S. Tsirkin if (err < 0) 58178738141SMichael S. Tsirkin goto out; 58278738141SMichael S. Tsirkin } 583eb06acdcSSridhar Samudrala goto hash_add; 584eb06acdcSSridhar Samudrala } 585eb06acdcSSridhar Samudrala 586b13ba1b8SJason Wang if (lowerdev->features & NETIF_F_HW_L2FW_DOFFLOAD && 587b13ba1b8SJason Wang dev->rtnl_link_ops == &macvlan_link_ops) { 588a6cc0cfaSJohn Fastabend vlan->fwd_priv = 589a6cc0cfaSJohn Fastabend lowerdev->netdev_ops->ndo_dfwd_add_station(lowerdev, dev); 590a6cc0cfaSJohn Fastabend 591a6cc0cfaSJohn Fastabend /* If we get a NULL pointer back, or if we get an error 592a6cc0cfaSJohn Fastabend * then we should just fall through to the non accelerated path 593a6cc0cfaSJohn Fastabend */ 594a6cc0cfaSJohn Fastabend if (IS_ERR_OR_NULL(vlan->fwd_priv)) { 595a6cc0cfaSJohn Fastabend vlan->fwd_priv = NULL; 596f663dd9aSJason Wang } else 597a6cc0cfaSJohn Fastabend return 0; 598a6cc0cfaSJohn Fastabend } 599a6cc0cfaSJohn Fastabend 600f9ac30f0SEric Biederman err = -EBUSY; 601f9ac30f0SEric Biederman if (macvlan_addr_busy(vlan->port, dev->dev_addr)) 602f9ac30f0SEric Biederman goto out; 603f9ac30f0SEric Biederman 604a748ee24SJiri Pirko err = dev_uc_add(lowerdev, dev->dev_addr); 605b863ceb7SPatrick McHardy if (err < 0) 606b89fb7daSWang Chen goto out; 607b89fb7daSWang Chen if (dev->flags & IFF_ALLMULTI) { 608b89fb7daSWang Chen err = dev_set_allmulti(lowerdev, 1); 609b89fb7daSWang Chen if (err < 0) 610b89fb7daSWang Chen goto del_unicast; 611b89fb7daSWang Chen } 612eb06acdcSSridhar Samudrala 613efdbd2b3SVlad Yasevich if (dev->flags & IFF_PROMISC) { 614efdbd2b3SVlad Yasevich err = dev_set_promiscuity(lowerdev, 1); 615efdbd2b3SVlad Yasevich if (err < 0) 616efdbd2b3SVlad Yasevich goto clear_multi; 617efdbd2b3SVlad Yasevich } 618efdbd2b3SVlad Yasevich 619eb06acdcSSridhar Samudrala hash_add: 620f9ac30f0SEric Biederman macvlan_hash_add(vlan); 621b863ceb7SPatrick McHardy return 0; 622b89fb7daSWang Chen 623efdbd2b3SVlad Yasevich clear_multi: 624c3891fa2SGao Feng if (dev->flags & IFF_ALLMULTI) 625efdbd2b3SVlad Yasevich dev_set_allmulti(lowerdev, -1); 626b89fb7daSWang Chen del_unicast: 627a748ee24SJiri Pirko dev_uc_del(lowerdev, dev->dev_addr); 628b89fb7daSWang Chen out: 629a6cc0cfaSJohn Fastabend if (vlan->fwd_priv) { 630a6cc0cfaSJohn Fastabend lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev, 631a6cc0cfaSJohn Fastabend vlan->fwd_priv); 632a6cc0cfaSJohn Fastabend vlan->fwd_priv = NULL; 633a6cc0cfaSJohn Fastabend } 634b89fb7daSWang Chen return err; 635b863ceb7SPatrick McHardy } 636b863ceb7SPatrick McHardy 637b863ceb7SPatrick McHardy static int macvlan_stop(struct net_device *dev) 638b863ceb7SPatrick McHardy { 639b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 640b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 641b863ceb7SPatrick McHardy 642a6cc0cfaSJohn Fastabend if (vlan->fwd_priv) { 643a6cc0cfaSJohn Fastabend lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev, 644a6cc0cfaSJohn Fastabend vlan->fwd_priv); 645a6cc0cfaSJohn Fastabend vlan->fwd_priv = NULL; 646a6cc0cfaSJohn Fastabend return 0; 647a6cc0cfaSJohn Fastabend } 648a6cc0cfaSJohn Fastabend 649df8ef8f3SJohn Fastabend dev_uc_unsync(lowerdev, dev); 650df8ef8f3SJohn Fastabend dev_mc_unsync(lowerdev, dev); 651df8ef8f3SJohn Fastabend 652eb06acdcSSridhar Samudrala if (vlan->port->passthru) { 653df8ef8f3SJohn Fastabend if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) 654eb06acdcSSridhar Samudrala dev_set_promiscuity(lowerdev, -1); 655eb06acdcSSridhar Samudrala goto hash_del; 656eb06acdcSSridhar Samudrala } 657eb06acdcSSridhar Samudrala 658b863ceb7SPatrick McHardy if (dev->flags & IFF_ALLMULTI) 659b863ceb7SPatrick McHardy dev_set_allmulti(lowerdev, -1); 660b863ceb7SPatrick McHardy 661efdbd2b3SVlad Yasevich if (dev->flags & IFF_PROMISC) 662efdbd2b3SVlad Yasevich dev_set_promiscuity(lowerdev, -1); 663efdbd2b3SVlad Yasevich 664a748ee24SJiri Pirko dev_uc_del(lowerdev, dev->dev_addr); 665b863ceb7SPatrick McHardy 666eb06acdcSSridhar Samudrala hash_del: 667449f4544SEric Dumazet macvlan_hash_del(vlan, !dev->dismantle); 668b863ceb7SPatrick McHardy return 0; 669b863ceb7SPatrick McHardy } 670b863ceb7SPatrick McHardy 671e289fd28Sdingtianhong static int macvlan_sync_address(struct net_device *dev, unsigned char *addr) 672ad5d20a6SPatrick McHardy { 673ad5d20a6SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 674ad5d20a6SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 675ad5d20a6SPatrick McHardy int err; 676ad5d20a6SPatrick McHardy 677f9ac30f0SEric Biederman if (!(dev->flags & IFF_UP)) { 678f9ac30f0SEric Biederman /* Just copy in the new address */ 679e289fd28Sdingtianhong ether_addr_copy(dev->dev_addr, addr); 680f9ac30f0SEric Biederman } else { 681f9ac30f0SEric Biederman /* Rehash and update the device filters */ 682e289fd28Sdingtianhong if (macvlan_addr_busy(vlan->port, addr)) 683f9ac30f0SEric Biederman return -EBUSY; 684ad5d20a6SPatrick McHardy 685e289fd28Sdingtianhong if (!vlan->port->passthru) { 686e289fd28Sdingtianhong err = dev_uc_add(lowerdev, addr); 687ccffad25SJiri Pirko if (err) 688ad5d20a6SPatrick McHardy return err; 689f9ac30f0SEric Biederman 690a748ee24SJiri Pirko dev_uc_del(lowerdev, dev->dev_addr); 691e289fd28Sdingtianhong } 692ad5d20a6SPatrick McHardy 693e289fd28Sdingtianhong macvlan_hash_change_addr(vlan, addr); 694f9ac30f0SEric Biederman } 695ad5d20a6SPatrick McHardy return 0; 696ad5d20a6SPatrick McHardy } 697ad5d20a6SPatrick McHardy 698e289fd28Sdingtianhong static int macvlan_set_mac_address(struct net_device *dev, void *p) 699e289fd28Sdingtianhong { 700e289fd28Sdingtianhong struct macvlan_dev *vlan = netdev_priv(dev); 701e289fd28Sdingtianhong struct sockaddr *addr = p; 702e289fd28Sdingtianhong 703e289fd28Sdingtianhong if (!is_valid_ether_addr(addr->sa_data)) 704e289fd28Sdingtianhong return -EADDRNOTAVAIL; 705e289fd28Sdingtianhong 706e289fd28Sdingtianhong if (vlan->mode == MACVLAN_MODE_PASSTHRU) { 707e289fd28Sdingtianhong dev_set_mac_address(vlan->lowerdev, addr); 708e289fd28Sdingtianhong return 0; 709e289fd28Sdingtianhong } 710e289fd28Sdingtianhong 711e289fd28Sdingtianhong return macvlan_sync_address(dev, addr->sa_data); 712e289fd28Sdingtianhong } 713e289fd28Sdingtianhong 714b863ceb7SPatrick McHardy static void macvlan_change_rx_flags(struct net_device *dev, int change) 715b863ceb7SPatrick McHardy { 716b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 717b863ceb7SPatrick McHardy struct net_device *lowerdev = vlan->lowerdev; 718b863ceb7SPatrick McHardy 719bbeb0eadSPeter Christensen if (dev->flags & IFF_UP) { 720b863ceb7SPatrick McHardy if (change & IFF_ALLMULTI) 721b863ceb7SPatrick McHardy dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1); 722efdbd2b3SVlad Yasevich if (change & IFF_PROMISC) 723efdbd2b3SVlad Yasevich dev_set_promiscuity(lowerdev, 724efdbd2b3SVlad Yasevich dev->flags & IFF_PROMISC ? 1 : -1); 725efdbd2b3SVlad Yasevich 726b863ceb7SPatrick McHardy } 727bbeb0eadSPeter Christensen } 728b863ceb7SPatrick McHardy 7299c127a01SHerbert Xu static void macvlan_compute_filter(unsigned long *mc_filter, 7309c127a01SHerbert Xu struct net_device *dev, 7319c127a01SHerbert Xu struct macvlan_dev *vlan) 732b863ceb7SPatrick McHardy { 733cd431e73SEric Dumazet if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) { 7349c127a01SHerbert Xu bitmap_fill(mc_filter, MACVLAN_MC_FILTER_SZ); 735cd431e73SEric Dumazet } else { 736cd431e73SEric Dumazet struct netdev_hw_addr *ha; 737cd431e73SEric Dumazet DECLARE_BITMAP(filter, MACVLAN_MC_FILTER_SZ); 738cd431e73SEric Dumazet 739cd431e73SEric Dumazet bitmap_zero(filter, MACVLAN_MC_FILTER_SZ); 740cd431e73SEric Dumazet netdev_for_each_mc_addr(ha, dev) { 7413807ff58SEric Dumazet __set_bit(mc_hash(vlan, ha->addr), filter); 742cd431e73SEric Dumazet } 743d5270430SEric Dumazet 7443807ff58SEric Dumazet __set_bit(mc_hash(vlan, dev->broadcast), filter); 745d5270430SEric Dumazet 7469c127a01SHerbert Xu bitmap_copy(mc_filter, filter, MACVLAN_MC_FILTER_SZ); 747cd431e73SEric Dumazet } 7489c127a01SHerbert Xu } 7499c127a01SHerbert Xu 7509c127a01SHerbert Xu static void macvlan_set_mac_lists(struct net_device *dev) 7519c127a01SHerbert Xu { 7529c127a01SHerbert Xu struct macvlan_dev *vlan = netdev_priv(dev); 7539c127a01SHerbert Xu 7549c127a01SHerbert Xu macvlan_compute_filter(vlan->mc_filter, dev, vlan); 7559c127a01SHerbert Xu 756df8ef8f3SJohn Fastabend dev_uc_sync(vlan->lowerdev, dev); 757b863ceb7SPatrick McHardy dev_mc_sync(vlan->lowerdev, dev); 7589c127a01SHerbert Xu 7599c127a01SHerbert Xu /* This is slightly inaccurate as we're including the subscription 7609c127a01SHerbert Xu * list of vlan->lowerdev too. 7619c127a01SHerbert Xu * 7629c127a01SHerbert Xu * Bug alert: This only works if everyone has the same broadcast 7639c127a01SHerbert Xu * address as lowerdev. As soon as someone changes theirs this 7649c127a01SHerbert Xu * will break. 7659c127a01SHerbert Xu * 7669c127a01SHerbert Xu * However, this is already broken as when you change your broadcast 7679c127a01SHerbert Xu * address we don't get called. 7689c127a01SHerbert Xu * 7699c127a01SHerbert Xu * The solution is to maintain a list of broadcast addresses like 7709c127a01SHerbert Xu * we do for uc/mc, if you care. 7719c127a01SHerbert Xu */ 7729c127a01SHerbert Xu macvlan_compute_filter(vlan->port->mc_filter, vlan->lowerdev, NULL); 773b863ceb7SPatrick McHardy } 774b863ceb7SPatrick McHardy 775b863ceb7SPatrick McHardy static int macvlan_change_mtu(struct net_device *dev, int new_mtu) 776b863ceb7SPatrick McHardy { 777b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 778b863ceb7SPatrick McHardy 77991572088SJarod Wilson if (vlan->lowerdev->mtu < new_mtu) 780b863ceb7SPatrick McHardy return -EINVAL; 781b863ceb7SPatrick McHardy dev->mtu = new_mtu; 782b863ceb7SPatrick McHardy return 0; 783b863ceb7SPatrick McHardy } 784b863ceb7SPatrick McHardy 785b863ceb7SPatrick McHardy /* 786b863ceb7SPatrick McHardy * macvlan network devices have devices nesting below it and are a special 787b863ceb7SPatrick McHardy * "super class" of normal network devices; split their locks off into a 788b863ceb7SPatrick McHardy * separate class since they always nest. 789b863ceb7SPatrick McHardy */ 790cf508b12SDavid S. Miller static struct lock_class_key macvlan_netdev_addr_lock_key; 791b863ceb7SPatrick McHardy 7928b4703e9SVlad Yasevich #define ALWAYS_ON_FEATURES \ 793c8cd0989STom Herbert (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE | NETIF_F_LLTX | \ 794a523a5ecSJason Wang NETIF_F_GSO_ROBUST) 7958b4703e9SVlad Yasevich 796b863ceb7SPatrick McHardy #define MACVLAN_FEATURES \ 797a188222bSTom Herbert (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \ 79862dbe830SMichal Kubeček NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_LRO | \ 7998d13e670SJohn Fastabend NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \ 80028d2b136SPatrick McHardy NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER) 801b863ceb7SPatrick McHardy 802b863ceb7SPatrick McHardy #define MACVLAN_STATE_MASK \ 803b863ceb7SPatrick McHardy ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT)) 804b863ceb7SPatrick McHardy 805c674ac30SVlad Yasevich static int macvlan_get_nest_level(struct net_device *dev) 806c674ac30SVlad Yasevich { 807c674ac30SVlad Yasevich return ((struct macvlan_dev *)netdev_priv(dev))->nest_level; 808c674ac30SVlad Yasevich } 809c674ac30SVlad Yasevich 810c773e847SDavid S. Miller static void macvlan_set_lockdep_class(struct net_device *dev) 811c773e847SDavid S. Miller { 81224ffd752SEric Dumazet netdev_lockdep_set_classes(dev); 813c674ac30SVlad Yasevich lockdep_set_class_and_subclass(&dev->addr_list_lock, 814c674ac30SVlad Yasevich &macvlan_netdev_addr_lock_key, 815c674ac30SVlad Yasevich macvlan_get_nest_level(dev)); 816c773e847SDavid S. Miller } 817c773e847SDavid S. Miller 818b863ceb7SPatrick McHardy static int macvlan_init(struct net_device *dev) 819b863ceb7SPatrick McHardy { 820b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 821b863ceb7SPatrick McHardy const struct net_device *lowerdev = vlan->lowerdev; 82230837960SFrancesco Ruggeri struct macvlan_port *port = vlan->port; 823b863ceb7SPatrick McHardy 824b863ceb7SPatrick McHardy dev->state = (dev->state & ~MACVLAN_STATE_MASK) | 825b863ceb7SPatrick McHardy (lowerdev->state & MACVLAN_STATE_MASK); 826b863ceb7SPatrick McHardy dev->features = lowerdev->features & MACVLAN_FEATURES; 8278b4703e9SVlad Yasevich dev->features |= ALWAYS_ON_FEATURES; 82862dbe830SMichal Kubeček dev->hw_features |= NETIF_F_LRO; 829081e83a7SVlad Yasevich dev->vlan_features = lowerdev->vlan_features & MACVLAN_FEATURES; 8308c2acc53SPatrick McHardy dev->gso_max_size = lowerdev->gso_max_size; 831f6773c5eSEric Dumazet dev->gso_max_segs = lowerdev->gso_max_segs; 832ef5c8996Ssg.tweak@gmail.com dev->hard_header_len = lowerdev->hard_header_len; 833b863ceb7SPatrick McHardy 834c773e847SDavid S. Miller macvlan_set_lockdep_class(dev); 835c773e847SDavid S. Miller 8361c213bd2SWANG Cong vlan->pcpu_stats = netdev_alloc_pcpu_stats(struct vlan_pcpu_stats); 8378ffab51bSEric Dumazet if (!vlan->pcpu_stats) 838fccaf710SEric Dumazet return -ENOMEM; 839fccaf710SEric Dumazet 84030837960SFrancesco Ruggeri port->count += 1; 84130837960SFrancesco Ruggeri 842b863ceb7SPatrick McHardy return 0; 843b863ceb7SPatrick McHardy } 844b863ceb7SPatrick McHardy 845fccaf710SEric Dumazet static void macvlan_uninit(struct net_device *dev) 846fccaf710SEric Dumazet { 847fccaf710SEric Dumazet struct macvlan_dev *vlan = netdev_priv(dev); 848d5cd9244SEric W. Biederman struct macvlan_port *port = vlan->port; 849fccaf710SEric Dumazet 8508ffab51bSEric Dumazet free_percpu(vlan->pcpu_stats); 851d5cd9244SEric W. Biederman 85279cf79abSMichael Braun macvlan_flush_sources(port, vlan); 8535e3c516bSDavid S. Miller port->count -= 1; 8545e3c516bSDavid S. Miller if (!port->count) 855d5cd9244SEric W. Biederman macvlan_port_destroy(port->dev); 856fccaf710SEric Dumazet } 857fccaf710SEric Dumazet 858*bc1f4470Sstephen hemminger static void macvlan_dev_get_stats64(struct net_device *dev, 85928172739SEric Dumazet struct rtnl_link_stats64 *stats) 860fccaf710SEric Dumazet { 861fccaf710SEric Dumazet struct macvlan_dev *vlan = netdev_priv(dev); 862fccaf710SEric Dumazet 8638ffab51bSEric Dumazet if (vlan->pcpu_stats) { 864cdf3e274SLi RongQing struct vlan_pcpu_stats *p; 8658ffab51bSEric Dumazet u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes; 8668ffab51bSEric Dumazet u32 rx_errors = 0, tx_dropped = 0; 867bc66154eSEric Dumazet unsigned int start; 868fccaf710SEric Dumazet int i; 869fccaf710SEric Dumazet 870fccaf710SEric Dumazet for_each_possible_cpu(i) { 8718ffab51bSEric Dumazet p = per_cpu_ptr(vlan->pcpu_stats, i); 872bc66154eSEric Dumazet do { 87357a7744eSEric W. Biederman start = u64_stats_fetch_begin_irq(&p->syncp); 874bc66154eSEric Dumazet rx_packets = p->rx_packets; 875bc66154eSEric Dumazet rx_bytes = p->rx_bytes; 876bc66154eSEric Dumazet rx_multicast = p->rx_multicast; 8778ffab51bSEric Dumazet tx_packets = p->tx_packets; 8788ffab51bSEric Dumazet tx_bytes = p->tx_bytes; 87957a7744eSEric W. Biederman } while (u64_stats_fetch_retry_irq(&p->syncp, start)); 8808ffab51bSEric Dumazet 8818ffab51bSEric Dumazet stats->rx_packets += rx_packets; 8828ffab51bSEric Dumazet stats->rx_bytes += rx_bytes; 8838ffab51bSEric Dumazet stats->multicast += rx_multicast; 8848ffab51bSEric Dumazet stats->tx_packets += tx_packets; 8858ffab51bSEric Dumazet stats->tx_bytes += tx_bytes; 8868ffab51bSEric Dumazet /* rx_errors & tx_dropped are u32, updated 8878ffab51bSEric Dumazet * without syncp protection. 8888ffab51bSEric Dumazet */ 8898ffab51bSEric Dumazet rx_errors += p->rx_errors; 8908ffab51bSEric Dumazet tx_dropped += p->tx_dropped; 891fccaf710SEric Dumazet } 8928ffab51bSEric Dumazet stats->rx_errors = rx_errors; 8938ffab51bSEric Dumazet stats->rx_dropped = rx_errors; 8948ffab51bSEric Dumazet stats->tx_dropped = tx_dropped; 895fccaf710SEric Dumazet } 896fccaf710SEric Dumazet } 897fccaf710SEric Dumazet 8988e586137SJiri Pirko static int macvlan_vlan_rx_add_vid(struct net_device *dev, 89980d5c368SPatrick McHardy __be16 proto, u16 vid) 9008d13e670SJohn Fastabend { 9018d13e670SJohn Fastabend struct macvlan_dev *vlan = netdev_priv(dev); 9028d13e670SJohn Fastabend struct net_device *lowerdev = vlan->lowerdev; 9038d13e670SJohn Fastabend 90480d5c368SPatrick McHardy return vlan_vid_add(lowerdev, proto, vid); 9058d13e670SJohn Fastabend } 9068d13e670SJohn Fastabend 9078e586137SJiri Pirko static int macvlan_vlan_rx_kill_vid(struct net_device *dev, 90880d5c368SPatrick McHardy __be16 proto, u16 vid) 9098d13e670SJohn Fastabend { 9108d13e670SJohn Fastabend struct macvlan_dev *vlan = netdev_priv(dev); 9118d13e670SJohn Fastabend struct net_device *lowerdev = vlan->lowerdev; 9128d13e670SJohn Fastabend 91380d5c368SPatrick McHardy vlan_vid_del(lowerdev, proto, vid); 9148e586137SJiri Pirko return 0; 9158d13e670SJohn Fastabend } 9168d13e670SJohn Fastabend 917edc7d573Sstephen hemminger static int macvlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], 918df8ef8f3SJohn Fastabend struct net_device *dev, 919f6f6424bSJiri Pirko const unsigned char *addr, u16 vid, 920df8ef8f3SJohn Fastabend u16 flags) 921df8ef8f3SJohn Fastabend { 922df8ef8f3SJohn Fastabend struct macvlan_dev *vlan = netdev_priv(dev); 923df8ef8f3SJohn Fastabend int err = -EINVAL; 924df8ef8f3SJohn Fastabend 9258a50f11cSVlad Yasevich /* Support unicast filter only on passthru devices. 9268a50f11cSVlad Yasevich * Multicast filter should be allowed on all devices. 9278a50f11cSVlad Yasevich */ 9288a50f11cSVlad Yasevich if (!vlan->port->passthru && is_unicast_ether_addr(addr)) 929df8ef8f3SJohn Fastabend return -EOPNOTSUPP; 930df8ef8f3SJohn Fastabend 931ab2cfbb2SThomas Richter if (flags & NLM_F_REPLACE) 932ab2cfbb2SThomas Richter return -EOPNOTSUPP; 933ab2cfbb2SThomas Richter 934df8ef8f3SJohn Fastabend if (is_unicast_ether_addr(addr)) 935df8ef8f3SJohn Fastabend err = dev_uc_add_excl(dev, addr); 936df8ef8f3SJohn Fastabend else if (is_multicast_ether_addr(addr)) 937df8ef8f3SJohn Fastabend err = dev_mc_add_excl(dev, addr); 938df8ef8f3SJohn Fastabend 939df8ef8f3SJohn Fastabend return err; 940df8ef8f3SJohn Fastabend } 941df8ef8f3SJohn Fastabend 9421690be63SVlad Yasevich static int macvlan_fdb_del(struct ndmsg *ndm, struct nlattr *tb[], 943df8ef8f3SJohn Fastabend struct net_device *dev, 944f6f6424bSJiri Pirko const unsigned char *addr, u16 vid) 945df8ef8f3SJohn Fastabend { 946df8ef8f3SJohn Fastabend struct macvlan_dev *vlan = netdev_priv(dev); 947df8ef8f3SJohn Fastabend int err = -EINVAL; 948df8ef8f3SJohn Fastabend 9498a50f11cSVlad Yasevich /* Support unicast filter only on passthru devices. 9508a50f11cSVlad Yasevich * Multicast filter should be allowed on all devices. 9518a50f11cSVlad Yasevich */ 9528a50f11cSVlad Yasevich if (!vlan->port->passthru && is_unicast_ether_addr(addr)) 953df8ef8f3SJohn Fastabend return -EOPNOTSUPP; 954df8ef8f3SJohn Fastabend 955df8ef8f3SJohn Fastabend if (is_unicast_ether_addr(addr)) 956df8ef8f3SJohn Fastabend err = dev_uc_del(dev, addr); 957df8ef8f3SJohn Fastabend else if (is_multicast_ether_addr(addr)) 958df8ef8f3SJohn Fastabend err = dev_mc_del(dev, addr); 959df8ef8f3SJohn Fastabend 960df8ef8f3SJohn Fastabend return err; 961df8ef8f3SJohn Fastabend } 962df8ef8f3SJohn Fastabend 963b863ceb7SPatrick McHardy static void macvlan_ethtool_get_drvinfo(struct net_device *dev, 964b863ceb7SPatrick McHardy struct ethtool_drvinfo *drvinfo) 965b863ceb7SPatrick McHardy { 9667826d43fSJiri Pirko strlcpy(drvinfo->driver, "macvlan", sizeof(drvinfo->driver)); 9677826d43fSJiri Pirko strlcpy(drvinfo->version, "0.1", sizeof(drvinfo->version)); 968b863ceb7SPatrick McHardy } 969b863ceb7SPatrick McHardy 97085f95819SDavid Decotigny static int macvlan_ethtool_get_link_ksettings(struct net_device *dev, 97185f95819SDavid Decotigny struct ethtool_link_ksettings *cmd) 9729edb8bb6SStephen Hemminger { 9739edb8bb6SStephen Hemminger const struct macvlan_dev *vlan = netdev_priv(dev); 9744bc71cb9SJiri Pirko 97585f95819SDavid Decotigny return __ethtool_get_link_ksettings(vlan->lowerdev, cmd); 9769edb8bb6SStephen Hemminger } 9779edb8bb6SStephen Hemminger 9782be5c767SVlad Yasevich static netdev_features_t macvlan_fix_features(struct net_device *dev, 9792be5c767SVlad Yasevich netdev_features_t features) 9802be5c767SVlad Yasevich { 9812be5c767SVlad Yasevich struct macvlan_dev *vlan = netdev_priv(dev); 98262dbe830SMichal Kubeček netdev_features_t lowerdev_features = vlan->lowerdev->features; 983797f87f8SFlorian Westphal netdev_features_t mask; 9842be5c767SVlad Yasevich 985797f87f8SFlorian Westphal features |= NETIF_F_ALL_FOR_ALL; 986797f87f8SFlorian Westphal features &= (vlan->set_features | ~MACVLAN_FEATURES); 987797f87f8SFlorian Westphal mask = features; 988797f87f8SFlorian Westphal 98962dbe830SMichal Kubeček lowerdev_features &= (features | ~NETIF_F_LRO); 99062dbe830SMichal Kubeček features = netdev_increment_features(lowerdev_features, features, mask); 9918b4703e9SVlad Yasevich features |= ALWAYS_ON_FEATURES; 9920d0162e7SFrancesco Ruggeri features &= ~NETIF_F_NETNS_LOCAL; 993797f87f8SFlorian Westphal 994797f87f8SFlorian Westphal return features; 9952be5c767SVlad Yasevich } 9962be5c767SVlad Yasevich 997688cea83Sdingtianhong #ifdef CONFIG_NET_POLL_CONTROLLER 998688cea83Sdingtianhong static void macvlan_dev_poll_controller(struct net_device *dev) 999688cea83Sdingtianhong { 1000688cea83Sdingtianhong return; 1001688cea83Sdingtianhong } 1002688cea83Sdingtianhong 1003688cea83Sdingtianhong static int macvlan_dev_netpoll_setup(struct net_device *dev, struct netpoll_info *npinfo) 1004688cea83Sdingtianhong { 1005688cea83Sdingtianhong struct macvlan_dev *vlan = netdev_priv(dev); 1006688cea83Sdingtianhong struct net_device *real_dev = vlan->lowerdev; 1007688cea83Sdingtianhong struct netpoll *netpoll; 1008688cea83Sdingtianhong int err = 0; 1009688cea83Sdingtianhong 1010688cea83Sdingtianhong netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL); 1011688cea83Sdingtianhong err = -ENOMEM; 1012688cea83Sdingtianhong if (!netpoll) 1013688cea83Sdingtianhong goto out; 1014688cea83Sdingtianhong 1015688cea83Sdingtianhong err = __netpoll_setup(netpoll, real_dev); 1016688cea83Sdingtianhong if (err) { 1017688cea83Sdingtianhong kfree(netpoll); 1018688cea83Sdingtianhong goto out; 1019688cea83Sdingtianhong } 1020688cea83Sdingtianhong 1021688cea83Sdingtianhong vlan->netpoll = netpoll; 1022688cea83Sdingtianhong 1023688cea83Sdingtianhong out: 1024688cea83Sdingtianhong return err; 1025688cea83Sdingtianhong } 1026688cea83Sdingtianhong 1027688cea83Sdingtianhong static void macvlan_dev_netpoll_cleanup(struct net_device *dev) 1028688cea83Sdingtianhong { 1029688cea83Sdingtianhong struct macvlan_dev *vlan = netdev_priv(dev); 1030688cea83Sdingtianhong struct netpoll *netpoll = vlan->netpoll; 1031688cea83Sdingtianhong 1032688cea83Sdingtianhong if (!netpoll) 1033688cea83Sdingtianhong return; 1034688cea83Sdingtianhong 1035688cea83Sdingtianhong vlan->netpoll = NULL; 1036688cea83Sdingtianhong 1037688cea83Sdingtianhong __netpoll_free_async(netpoll); 1038688cea83Sdingtianhong } 1039688cea83Sdingtianhong #endif /* CONFIG_NET_POLL_CONTROLLER */ 1040688cea83Sdingtianhong 1041ef5fa6bcSNicolas Dichtel static int macvlan_dev_get_iflink(const struct net_device *dev) 1042ef5fa6bcSNicolas Dichtel { 1043ef5fa6bcSNicolas Dichtel struct macvlan_dev *vlan = netdev_priv(dev); 1044ef5fa6bcSNicolas Dichtel 1045ef5fa6bcSNicolas Dichtel return vlan->lowerdev->ifindex; 1046ef5fa6bcSNicolas Dichtel } 1047ef5fa6bcSNicolas Dichtel 1048b863ceb7SPatrick McHardy static const struct ethtool_ops macvlan_ethtool_ops = { 1049b863ceb7SPatrick McHardy .get_link = ethtool_op_get_link, 105085f95819SDavid Decotigny .get_link_ksettings = macvlan_ethtool_get_link_ksettings, 1051b863ceb7SPatrick McHardy .get_drvinfo = macvlan_ethtool_get_drvinfo, 1052b863ceb7SPatrick McHardy }; 1053b863ceb7SPatrick McHardy 105454a30c97SStephen Hemminger static const struct net_device_ops macvlan_netdev_ops = { 105554a30c97SStephen Hemminger .ndo_init = macvlan_init, 1056fccaf710SEric Dumazet .ndo_uninit = macvlan_uninit, 105754a30c97SStephen Hemminger .ndo_open = macvlan_open, 105854a30c97SStephen Hemminger .ndo_stop = macvlan_stop, 105900829823SStephen Hemminger .ndo_start_xmit = macvlan_start_xmit, 106054a30c97SStephen Hemminger .ndo_change_mtu = macvlan_change_mtu, 10612be5c767SVlad Yasevich .ndo_fix_features = macvlan_fix_features, 106254a30c97SStephen Hemminger .ndo_change_rx_flags = macvlan_change_rx_flags, 106354a30c97SStephen Hemminger .ndo_set_mac_address = macvlan_set_mac_address, 1064df8ef8f3SJohn Fastabend .ndo_set_rx_mode = macvlan_set_mac_lists, 1065bc66154eSEric Dumazet .ndo_get_stats64 = macvlan_dev_get_stats64, 106654a30c97SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 10678d13e670SJohn Fastabend .ndo_vlan_rx_add_vid = macvlan_vlan_rx_add_vid, 10688d13e670SJohn Fastabend .ndo_vlan_rx_kill_vid = macvlan_vlan_rx_kill_vid, 1069df8ef8f3SJohn Fastabend .ndo_fdb_add = macvlan_fdb_add, 1070df8ef8f3SJohn Fastabend .ndo_fdb_del = macvlan_fdb_del, 1071df8ef8f3SJohn Fastabend .ndo_fdb_dump = ndo_dflt_fdb_dump, 1072c674ac30SVlad Yasevich .ndo_get_lock_subclass = macvlan_get_nest_level, 1073688cea83Sdingtianhong #ifdef CONFIG_NET_POLL_CONTROLLER 1074688cea83Sdingtianhong .ndo_poll_controller = macvlan_dev_poll_controller, 1075688cea83Sdingtianhong .ndo_netpoll_setup = macvlan_dev_netpoll_setup, 1076688cea83Sdingtianhong .ndo_netpoll_cleanup = macvlan_dev_netpoll_cleanup, 1077688cea83Sdingtianhong #endif 1078ef5fa6bcSNicolas Dichtel .ndo_get_iflink = macvlan_dev_get_iflink, 1079f56e67b5SToshiaki Makita .ndo_features_check = passthru_features_check, 108054a30c97SStephen Hemminger }; 108154a30c97SStephen Hemminger 10828a35747aSHerbert Xu void macvlan_common_setup(struct net_device *dev) 1083b863ceb7SPatrick McHardy { 1084b863ceb7SPatrick McHardy ether_setup(dev); 1085b863ceb7SPatrick McHardy 108691572088SJarod Wilson dev->min_mtu = 0; 108791572088SJarod Wilson dev->max_mtu = ETH_MAX_MTU; 108802875878SEric Dumazet dev->priv_flags &= ~IFF_TX_SKB_SHARING; 108902875878SEric Dumazet netif_keep_dst(dev); 109087ab7f6fSVlad Yasevich dev->priv_flags |= IFF_UNICAST_FLT; 109154a30c97SStephen Hemminger dev->netdev_ops = &macvlan_netdev_ops; 1092b863ceb7SPatrick McHardy dev->destructor = free_netdev; 109371740129SLutz Jaenicke dev->header_ops = &macvlan_hard_header_ops; 1094b863ceb7SPatrick McHardy dev->ethtool_ops = &macvlan_ethtool_ops; 10958a35747aSHerbert Xu } 10968a35747aSHerbert Xu EXPORT_SYMBOL_GPL(macvlan_common_setup); 10978a35747aSHerbert Xu 10988a35747aSHerbert Xu static void macvlan_setup(struct net_device *dev) 10998a35747aSHerbert Xu { 11008a35747aSHerbert Xu macvlan_common_setup(dev); 11017009212bSZhang Shengju dev->priv_flags |= IFF_NO_QUEUE; 1102b863ceb7SPatrick McHardy } 1103b863ceb7SPatrick McHardy 1104b863ceb7SPatrick McHardy static int macvlan_port_create(struct net_device *dev) 1105b863ceb7SPatrick McHardy { 1106b863ceb7SPatrick McHardy struct macvlan_port *port; 1107b863ceb7SPatrick McHardy unsigned int i; 1108ab95bfe0SJiri Pirko int err; 1109b863ceb7SPatrick McHardy 1110b863ceb7SPatrick McHardy if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK) 1111b863ceb7SPatrick McHardy return -EINVAL; 1112b863ceb7SPatrick McHardy 1113d6b00fecSMahesh Bandewar if (netif_is_ipvlan_port(dev)) 1114d6b00fecSMahesh Bandewar return -EBUSY; 1115d6b00fecSMahesh Bandewar 1116b863ceb7SPatrick McHardy port = kzalloc(sizeof(*port), GFP_KERNEL); 1117b863ceb7SPatrick McHardy if (port == NULL) 1118b863ceb7SPatrick McHardy return -ENOMEM; 1119b863ceb7SPatrick McHardy 1120eb06acdcSSridhar Samudrala port->passthru = false; 1121b863ceb7SPatrick McHardy port->dev = dev; 1122b863ceb7SPatrick McHardy INIT_LIST_HEAD(&port->vlans); 1123b863ceb7SPatrick McHardy for (i = 0; i < MACVLAN_HASH_SIZE; i++) 1124b863ceb7SPatrick McHardy INIT_HLIST_HEAD(&port->vlan_hash[i]); 112579cf79abSMichael Braun for (i = 0; i < MACVLAN_HASH_SIZE; i++) 112679cf79abSMichael Braun INIT_HLIST_HEAD(&port->vlan_source_hash[i]); 1127ab95bfe0SJiri Pirko 1128412ca155SHerbert Xu skb_queue_head_init(&port->bc_queue); 1129412ca155SHerbert Xu INIT_WORK(&port->bc_work, macvlan_process_broadcast); 1130412ca155SHerbert Xu 1131a35e2c1bSJiri Pirko err = netdev_rx_handler_register(dev, macvlan_handle_frame, port); 1132a35e2c1bSJiri Pirko if (err) 1133ab95bfe0SJiri Pirko kfree(port); 1134d9351561SEric Dumazet else 1135a35e2c1bSJiri Pirko dev->priv_flags |= IFF_MACVLAN_PORT; 1136ab95bfe0SJiri Pirko return err; 1137b863ceb7SPatrick McHardy } 1138b863ceb7SPatrick McHardy 1139b863ceb7SPatrick McHardy static void macvlan_port_destroy(struct net_device *dev) 1140b863ceb7SPatrick McHardy { 1141e052f7e6SEric Dumazet struct macvlan_port *port = macvlan_port_get_rtnl(dev); 1142b863ceb7SPatrick McHardy 1143a35e2c1bSJiri Pirko dev->priv_flags &= ~IFF_MACVLAN_PORT; 1144ab95bfe0SJiri Pirko netdev_rx_handler_unregister(dev); 1145fe0ca732SEric Dumazet 1146fe0ca732SEric Dumazet /* After this point, no packet can schedule bc_work anymore, 1147fe0ca732SEric Dumazet * but we need to cancel it and purge left skbs if any. 1148fe0ca732SEric Dumazet */ 1149fe0ca732SEric Dumazet cancel_work_sync(&port->bc_work); 1150fe0ca732SEric Dumazet __skb_queue_purge(&port->bc_queue); 1151fe0ca732SEric Dumazet 1152a1f5315cSGao Feng kfree(port); 1153b863ceb7SPatrick McHardy } 1154b863ceb7SPatrick McHardy 1155b863ceb7SPatrick McHardy static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[]) 1156b863ceb7SPatrick McHardy { 1157b863ceb7SPatrick McHardy if (tb[IFLA_ADDRESS]) { 1158b863ceb7SPatrick McHardy if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) 1159b863ceb7SPatrick McHardy return -EINVAL; 1160b863ceb7SPatrick McHardy if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) 1161b863ceb7SPatrick McHardy return -EADDRNOTAVAIL; 1162b863ceb7SPatrick McHardy } 116327c0b1a8SArnd Bergmann 116415127478SMichael S. Tsirkin if (data && data[IFLA_MACVLAN_FLAGS] && 116515127478SMichael S. Tsirkin nla_get_u16(data[IFLA_MACVLAN_FLAGS]) & ~MACVLAN_FLAG_NOPROMISC) 116615127478SMichael S. Tsirkin return -EINVAL; 116715127478SMichael S. Tsirkin 116827c0b1a8SArnd Bergmann if (data && data[IFLA_MACVLAN_MODE]) { 116927c0b1a8SArnd Bergmann switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) { 117027c0b1a8SArnd Bergmann case MACVLAN_MODE_PRIVATE: 117127c0b1a8SArnd Bergmann case MACVLAN_MODE_VEPA: 117227c0b1a8SArnd Bergmann case MACVLAN_MODE_BRIDGE: 1173eb06acdcSSridhar Samudrala case MACVLAN_MODE_PASSTHRU: 117479cf79abSMichael Braun case MACVLAN_MODE_SOURCE: 117527c0b1a8SArnd Bergmann break; 117627c0b1a8SArnd Bergmann default: 117727c0b1a8SArnd Bergmann return -EINVAL; 117827c0b1a8SArnd Bergmann } 117927c0b1a8SArnd Bergmann } 118079cf79abSMichael Braun 118179cf79abSMichael Braun if (data && data[IFLA_MACVLAN_MACADDR_MODE]) { 118279cf79abSMichael Braun switch (nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE])) { 118379cf79abSMichael Braun case MACVLAN_MACADDR_ADD: 118479cf79abSMichael Braun case MACVLAN_MACADDR_DEL: 118579cf79abSMichael Braun case MACVLAN_MACADDR_FLUSH: 118679cf79abSMichael Braun case MACVLAN_MACADDR_SET: 118779cf79abSMichael Braun break; 118879cf79abSMichael Braun default: 118979cf79abSMichael Braun return -EINVAL; 119079cf79abSMichael Braun } 119179cf79abSMichael Braun } 119279cf79abSMichael Braun 119379cf79abSMichael Braun if (data && data[IFLA_MACVLAN_MACADDR]) { 119479cf79abSMichael Braun if (nla_len(data[IFLA_MACVLAN_MACADDR]) != ETH_ALEN) 119579cf79abSMichael Braun return -EINVAL; 119679cf79abSMichael Braun 119779cf79abSMichael Braun if (!is_valid_ether_addr(nla_data(data[IFLA_MACVLAN_MACADDR]))) 119879cf79abSMichael Braun return -EADDRNOTAVAIL; 119979cf79abSMichael Braun } 120079cf79abSMichael Braun 120179cf79abSMichael Braun if (data && data[IFLA_MACVLAN_MACADDR_COUNT]) 120279cf79abSMichael Braun return -EINVAL; 120379cf79abSMichael Braun 120479cf79abSMichael Braun return 0; 120579cf79abSMichael Braun } 120679cf79abSMichael Braun 120779cf79abSMichael Braun /** 120879cf79abSMichael Braun * reconfigure list of remote source mac address 120979cf79abSMichael Braun * (only for macvlan devices in source mode) 121079cf79abSMichael Braun * Note regarding alignment: all netlink data is aligned to 4 Byte, which 121179cf79abSMichael Braun * suffices for both ether_addr_copy and ether_addr_equal_64bits usage. 121279cf79abSMichael Braun */ 121379cf79abSMichael Braun static int macvlan_changelink_sources(struct macvlan_dev *vlan, u32 mode, 121479cf79abSMichael Braun struct nlattr *data[]) 121579cf79abSMichael Braun { 121679cf79abSMichael Braun char *addr = NULL; 121779cf79abSMichael Braun int ret, rem, len; 121879cf79abSMichael Braun struct nlattr *nla, *head; 121979cf79abSMichael Braun struct macvlan_source_entry *entry; 122079cf79abSMichael Braun 122179cf79abSMichael Braun if (data[IFLA_MACVLAN_MACADDR]) 122279cf79abSMichael Braun addr = nla_data(data[IFLA_MACVLAN_MACADDR]); 122379cf79abSMichael Braun 122479cf79abSMichael Braun if (mode == MACVLAN_MACADDR_ADD) { 122579cf79abSMichael Braun if (!addr) 122679cf79abSMichael Braun return -EINVAL; 122779cf79abSMichael Braun 122879cf79abSMichael Braun return macvlan_hash_add_source(vlan, addr); 122979cf79abSMichael Braun 123079cf79abSMichael Braun } else if (mode == MACVLAN_MACADDR_DEL) { 123179cf79abSMichael Braun if (!addr) 123279cf79abSMichael Braun return -EINVAL; 123379cf79abSMichael Braun 123479cf79abSMichael Braun entry = macvlan_hash_lookup_source(vlan, addr); 123579cf79abSMichael Braun if (entry) { 123679cf79abSMichael Braun macvlan_hash_del_source(entry); 123779cf79abSMichael Braun vlan->macaddr_count--; 123879cf79abSMichael Braun } 123979cf79abSMichael Braun } else if (mode == MACVLAN_MACADDR_FLUSH) { 124079cf79abSMichael Braun macvlan_flush_sources(vlan->port, vlan); 124179cf79abSMichael Braun } else if (mode == MACVLAN_MACADDR_SET) { 124279cf79abSMichael Braun macvlan_flush_sources(vlan->port, vlan); 124379cf79abSMichael Braun 124479cf79abSMichael Braun if (addr) { 124579cf79abSMichael Braun ret = macvlan_hash_add_source(vlan, addr); 124679cf79abSMichael Braun if (ret) 124779cf79abSMichael Braun return ret; 124879cf79abSMichael Braun } 124979cf79abSMichael Braun 125079cf79abSMichael Braun if (!data || !data[IFLA_MACVLAN_MACADDR_DATA]) 125179cf79abSMichael Braun return 0; 125279cf79abSMichael Braun 125379cf79abSMichael Braun head = nla_data(data[IFLA_MACVLAN_MACADDR_DATA]); 125479cf79abSMichael Braun len = nla_len(data[IFLA_MACVLAN_MACADDR_DATA]); 125579cf79abSMichael Braun 125679cf79abSMichael Braun nla_for_each_attr(nla, head, len, rem) { 125779cf79abSMichael Braun if (nla_type(nla) != IFLA_MACVLAN_MACADDR || 125879cf79abSMichael Braun nla_len(nla) != ETH_ALEN) 125979cf79abSMichael Braun continue; 126079cf79abSMichael Braun 126179cf79abSMichael Braun addr = nla_data(nla); 126279cf79abSMichael Braun ret = macvlan_hash_add_source(vlan, addr); 126379cf79abSMichael Braun if (ret) 126479cf79abSMichael Braun return ret; 126579cf79abSMichael Braun } 126679cf79abSMichael Braun } else { 126779cf79abSMichael Braun return -EINVAL; 126879cf79abSMichael Braun } 126979cf79abSMichael Braun 1270b863ceb7SPatrick McHardy return 0; 1271b863ceb7SPatrick McHardy } 1272b863ceb7SPatrick McHardy 1273fc0663d6SArnd Bergmann int macvlan_common_newlink(struct net *src_net, struct net_device *dev, 12742f6a1b66SVlad Yasevich struct nlattr *tb[], struct nlattr *data[]) 1275b863ceb7SPatrick McHardy { 1276b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 1277b863ceb7SPatrick McHardy struct macvlan_port *port; 1278b863ceb7SPatrick McHardy struct net_device *lowerdev; 1279b863ceb7SPatrick McHardy int err; 128079cf79abSMichael Braun int macmode; 1281aa5fd0fbSGao Feng bool create = false; 1282b863ceb7SPatrick McHardy 1283b863ceb7SPatrick McHardy if (!tb[IFLA_LINK]) 1284b863ceb7SPatrick McHardy return -EINVAL; 1285b863ceb7SPatrick McHardy 128681adee47SEric W. Biederman lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK])); 1287b863ceb7SPatrick McHardy if (lowerdev == NULL) 1288b863ceb7SPatrick McHardy return -ENODEV; 1289b863ceb7SPatrick McHardy 1290d70f2cf5SKevin Wallace /* When creating macvlans or macvtaps on top of other macvlans - use 1291b0832a29SEric Biederman * the real device as the lowerdev. 1292a6ca5f1dSPatrick McHardy */ 1293d70f2cf5SKevin Wallace if (netif_is_macvlan(lowerdev)) 1294d70f2cf5SKevin Wallace lowerdev = macvlan_dev_real_dev(lowerdev); 1295a6ca5f1dSPatrick McHardy 1296b863ceb7SPatrick McHardy if (!tb[IFLA_MTU]) 1297b863ceb7SPatrick McHardy dev->mtu = lowerdev->mtu; 1298b863ceb7SPatrick McHardy else if (dev->mtu > lowerdev->mtu) 1299b863ceb7SPatrick McHardy return -EINVAL; 1300b863ceb7SPatrick McHardy 130191572088SJarod Wilson /* MTU range: 68 - lowerdev->max_mtu */ 130291572088SJarod Wilson dev->min_mtu = ETH_MIN_MTU; 130391572088SJarod Wilson dev->max_mtu = lowerdev->max_mtu; 130491572088SJarod Wilson 1305b863ceb7SPatrick McHardy if (!tb[IFLA_ADDRESS]) 13067ce5d222SDanny Kukawka eth_hw_addr_random(dev); 1307b863ceb7SPatrick McHardy 1308a35e2c1bSJiri Pirko if (!macvlan_port_exists(lowerdev)) { 1309b863ceb7SPatrick McHardy err = macvlan_port_create(lowerdev); 1310b863ceb7SPatrick McHardy if (err < 0) 1311b863ceb7SPatrick McHardy return err; 1312aa5fd0fbSGao Feng create = true; 1313b863ceb7SPatrick McHardy } 1314e052f7e6SEric Dumazet port = macvlan_port_get_rtnl(lowerdev); 1315b863ceb7SPatrick McHardy 1316eb06acdcSSridhar Samudrala /* Only 1 macvlan device can be created in passthru mode */ 1317aa5fd0fbSGao Feng if (port->passthru) { 1318aa5fd0fbSGao Feng /* The macvlan port must be not created this time, 1319aa5fd0fbSGao Feng * still goto destroy_macvlan_port for readability. 1320aa5fd0fbSGao Feng */ 1321aa5fd0fbSGao Feng err = -EINVAL; 1322aa5fd0fbSGao Feng goto destroy_macvlan_port; 1323aa5fd0fbSGao Feng } 1324eb06acdcSSridhar Samudrala 1325b863ceb7SPatrick McHardy vlan->lowerdev = lowerdev; 1326b863ceb7SPatrick McHardy vlan->dev = dev; 1327b863ceb7SPatrick McHardy vlan->port = port; 13282be5c767SVlad Yasevich vlan->set_features = MACVLAN_FEATURES; 1329952fcfd0SSabrina Dubroca vlan->nest_level = dev_get_nest_level(lowerdev) + 1; 1330b863ceb7SPatrick McHardy 133127c0b1a8SArnd Bergmann vlan->mode = MACVLAN_MODE_VEPA; 133227c0b1a8SArnd Bergmann if (data && data[IFLA_MACVLAN_MODE]) 133327c0b1a8SArnd Bergmann vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]); 133427c0b1a8SArnd Bergmann 1335df8ef8f3SJohn Fastabend if (data && data[IFLA_MACVLAN_FLAGS]) 1336df8ef8f3SJohn Fastabend vlan->flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]); 1337df8ef8f3SJohn Fastabend 1338eb06acdcSSridhar Samudrala if (vlan->mode == MACVLAN_MODE_PASSTHRU) { 1339aa5fd0fbSGao Feng if (port->count) { 1340aa5fd0fbSGao Feng err = -EINVAL; 1341aa5fd0fbSGao Feng goto destroy_macvlan_port; 1342aa5fd0fbSGao Feng } 1343eb06acdcSSridhar Samudrala port->passthru = true; 13448b98604eSBjørn Mork eth_hw_addr_inherit(dev, lowerdev); 1345eb06acdcSSridhar Samudrala } 1346eb06acdcSSridhar Samudrala 134779cf79abSMichael Braun if (data && data[IFLA_MACVLAN_MACADDR_MODE]) { 1348aa5fd0fbSGao Feng if (vlan->mode != MACVLAN_MODE_SOURCE) { 1349aa5fd0fbSGao Feng err = -EINVAL; 1350aa5fd0fbSGao Feng goto destroy_macvlan_port; 1351aa5fd0fbSGao Feng } 135279cf79abSMichael Braun macmode = nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE]); 135379cf79abSMichael Braun err = macvlan_changelink_sources(vlan, macmode, data); 135479cf79abSMichael Braun if (err) 1355aa5fd0fbSGao Feng goto destroy_macvlan_port; 135679cf79abSMichael Braun } 135779cf79abSMichael Braun 135847d4ab91SJohn Fastabend err = register_netdevice(dev); 135947d4ab91SJohn Fastabend if (err < 0) 1360aa5fd0fbSGao Feng goto destroy_macvlan_port; 136147d4ab91SJohn Fastabend 1362a6cc0cfaSJohn Fastabend dev->priv_flags |= IFF_MACVLAN; 13637cd43db7SJiri Pirko err = netdev_upper_dev_link(lowerdev, dev); 13647cd43db7SJiri Pirko if (err) 1365da37705cSCong Wang goto unregister_netdev; 1366b863ceb7SPatrick McHardy 1367233c7df0SJiri Pirko list_add_tail_rcu(&vlan->list, &port->vlans); 1368fc4a7489SPatrick Mullaney netif_stacked_transfer_operstate(lowerdev, dev); 1369de7d244dSNikolay Aleksandrov linkwatch_fire_event(dev); 1370f16d3d57SJiri Pirko 1371b863ceb7SPatrick McHardy return 0; 1372f16d3d57SJiri Pirko 1373da37705cSCong Wang unregister_netdev: 1374da37705cSCong Wang unregister_netdevice(dev); 1375aa5fd0fbSGao Feng destroy_macvlan_port: 1376aa5fd0fbSGao Feng if (create) 1377aa5fd0fbSGao Feng macvlan_port_destroy(port->dev); 1378f16d3d57SJiri Pirko return err; 1379b863ceb7SPatrick McHardy } 1380fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_common_newlink); 1381b863ceb7SPatrick McHardy 1382fc0663d6SArnd Bergmann static int macvlan_newlink(struct net *src_net, struct net_device *dev, 1383fc0663d6SArnd Bergmann struct nlattr *tb[], struct nlattr *data[]) 1384fc0663d6SArnd Bergmann { 13852f6a1b66SVlad Yasevich return macvlan_common_newlink(src_net, dev, tb, data); 1386fc0663d6SArnd Bergmann } 1387fc0663d6SArnd Bergmann 1388fc0663d6SArnd Bergmann void macvlan_dellink(struct net_device *dev, struct list_head *head) 1389b863ceb7SPatrick McHardy { 1390b863ceb7SPatrick McHardy struct macvlan_dev *vlan = netdev_priv(dev); 1391b863ceb7SPatrick McHardy 139279cf79abSMichael Braun if (vlan->mode == MACVLAN_MODE_SOURCE) 139379cf79abSMichael Braun macvlan_flush_sources(vlan->port, vlan); 1394233c7df0SJiri Pirko list_del_rcu(&vlan->list); 139523289a37SEric Dumazet unregister_netdevice_queue(dev, head); 13967cd43db7SJiri Pirko netdev_upper_dev_unlink(vlan->lowerdev, dev); 1397b863ceb7SPatrick McHardy } 1398fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_dellink); 1399b863ceb7SPatrick McHardy 140027c0b1a8SArnd Bergmann static int macvlan_changelink(struct net_device *dev, 140127c0b1a8SArnd Bergmann struct nlattr *tb[], struct nlattr *data[]) 140227c0b1a8SArnd Bergmann { 140327c0b1a8SArnd Bergmann struct macvlan_dev *vlan = netdev_priv(dev); 1404266e8347SMichael S. Tsirkin enum macvlan_mode mode; 1405266e8347SMichael S. Tsirkin bool set_mode = false; 140679cf79abSMichael Braun enum macvlan_macaddr_mode macmode; 140779cf79abSMichael Braun int ret; 1408266e8347SMichael S. Tsirkin 1409266e8347SMichael S. Tsirkin /* Validate mode, but don't set yet: setting flags may fail. */ 1410266e8347SMichael S. Tsirkin if (data && data[IFLA_MACVLAN_MODE]) { 1411266e8347SMichael S. Tsirkin set_mode = true; 1412266e8347SMichael S. Tsirkin mode = nla_get_u32(data[IFLA_MACVLAN_MODE]); 1413266e8347SMichael S. Tsirkin /* Passthrough mode can't be set or cleared dynamically */ 1414266e8347SMichael S. Tsirkin if ((mode == MACVLAN_MODE_PASSTHRU) != 1415266e8347SMichael S. Tsirkin (vlan->mode == MACVLAN_MODE_PASSTHRU)) 1416266e8347SMichael S. Tsirkin return -EINVAL; 141779cf79abSMichael Braun if (vlan->mode == MACVLAN_MODE_SOURCE && 141879cf79abSMichael Braun vlan->mode != mode) 141979cf79abSMichael Braun macvlan_flush_sources(vlan->port, vlan); 1420266e8347SMichael S. Tsirkin } 142199ffc3e7SMichael S. Tsirkin 1422df8ef8f3SJohn Fastabend if (data && data[IFLA_MACVLAN_FLAGS]) { 1423df8ef8f3SJohn Fastabend __u16 flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]); 1424df8ef8f3SJohn Fastabend bool promisc = (flags ^ vlan->flags) & MACVLAN_FLAG_NOPROMISC; 142599ffc3e7SMichael S. Tsirkin if (vlan->port->passthru && promisc) { 142699ffc3e7SMichael S. Tsirkin int err; 1427df8ef8f3SJohn Fastabend 142899ffc3e7SMichael S. Tsirkin if (flags & MACVLAN_FLAG_NOPROMISC) 142999ffc3e7SMichael S. Tsirkin err = dev_set_promiscuity(vlan->lowerdev, -1); 143099ffc3e7SMichael S. Tsirkin else 143199ffc3e7SMichael S. Tsirkin err = dev_set_promiscuity(vlan->lowerdev, 1); 143299ffc3e7SMichael S. Tsirkin if (err < 0) 143399ffc3e7SMichael S. Tsirkin return err; 143499ffc3e7SMichael S. Tsirkin } 1435df8ef8f3SJohn Fastabend vlan->flags = flags; 1436df8ef8f3SJohn Fastabend } 1437266e8347SMichael S. Tsirkin if (set_mode) 1438266e8347SMichael S. Tsirkin vlan->mode = mode; 143979cf79abSMichael Braun if (data && data[IFLA_MACVLAN_MACADDR_MODE]) { 144079cf79abSMichael Braun if (vlan->mode != MACVLAN_MODE_SOURCE) 144179cf79abSMichael Braun return -EINVAL; 144279cf79abSMichael Braun macmode = nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE]); 144379cf79abSMichael Braun ret = macvlan_changelink_sources(vlan, macmode, data); 144479cf79abSMichael Braun if (ret) 144579cf79abSMichael Braun return ret; 144679cf79abSMichael Braun } 144727c0b1a8SArnd Bergmann return 0; 144827c0b1a8SArnd Bergmann } 144927c0b1a8SArnd Bergmann 145079cf79abSMichael Braun static size_t macvlan_get_size_mac(const struct macvlan_dev *vlan) 145179cf79abSMichael Braun { 145279cf79abSMichael Braun if (vlan->macaddr_count == 0) 145379cf79abSMichael Braun return 0; 145479cf79abSMichael Braun return nla_total_size(0) /* IFLA_MACVLAN_MACADDR_DATA */ 145579cf79abSMichael Braun + vlan->macaddr_count * nla_total_size(sizeof(u8) * ETH_ALEN); 145679cf79abSMichael Braun } 145779cf79abSMichael Braun 145827c0b1a8SArnd Bergmann static size_t macvlan_get_size(const struct net_device *dev) 145927c0b1a8SArnd Bergmann { 146079cf79abSMichael Braun struct macvlan_dev *vlan = netdev_priv(dev); 146179cf79abSMichael Braun 146201fe944fSEric Dumazet return (0 146301fe944fSEric Dumazet + nla_total_size(4) /* IFLA_MACVLAN_MODE */ 146401fe944fSEric Dumazet + nla_total_size(2) /* IFLA_MACVLAN_FLAGS */ 146579cf79abSMichael Braun + nla_total_size(4) /* IFLA_MACVLAN_MACADDR_COUNT */ 146679cf79abSMichael Braun + macvlan_get_size_mac(vlan) /* IFLA_MACVLAN_MACADDR */ 146701fe944fSEric Dumazet ); 146827c0b1a8SArnd Bergmann } 146927c0b1a8SArnd Bergmann 147079cf79abSMichael Braun static int macvlan_fill_info_macaddr(struct sk_buff *skb, 147179cf79abSMichael Braun const struct macvlan_dev *vlan, 147279cf79abSMichael Braun const int i) 147379cf79abSMichael Braun { 147479cf79abSMichael Braun struct hlist_head *h = &vlan->port->vlan_source_hash[i]; 147579cf79abSMichael Braun struct macvlan_source_entry *entry; 147679cf79abSMichael Braun 147779cf79abSMichael Braun hlist_for_each_entry_rcu(entry, h, hlist) { 147879cf79abSMichael Braun if (entry->vlan != vlan) 147979cf79abSMichael Braun continue; 148079cf79abSMichael Braun if (nla_put(skb, IFLA_MACVLAN_MACADDR, ETH_ALEN, entry->addr)) 148179cf79abSMichael Braun return 1; 148279cf79abSMichael Braun } 148379cf79abSMichael Braun return 0; 148479cf79abSMichael Braun } 148579cf79abSMichael Braun 148627c0b1a8SArnd Bergmann static int macvlan_fill_info(struct sk_buff *skb, 148727c0b1a8SArnd Bergmann const struct net_device *dev) 148827c0b1a8SArnd Bergmann { 148927c0b1a8SArnd Bergmann struct macvlan_dev *vlan = netdev_priv(dev); 149079cf79abSMichael Braun int i; 149179cf79abSMichael Braun struct nlattr *nest; 149227c0b1a8SArnd Bergmann 1493ead9a76cSDavid S. Miller if (nla_put_u32(skb, IFLA_MACVLAN_MODE, vlan->mode)) 1494ead9a76cSDavid S. Miller goto nla_put_failure; 1495df8ef8f3SJohn Fastabend if (nla_put_u16(skb, IFLA_MACVLAN_FLAGS, vlan->flags)) 1496df8ef8f3SJohn Fastabend goto nla_put_failure; 149779cf79abSMichael Braun if (nla_put_u32(skb, IFLA_MACVLAN_MACADDR_COUNT, vlan->macaddr_count)) 149879cf79abSMichael Braun goto nla_put_failure; 149979cf79abSMichael Braun if (vlan->macaddr_count > 0) { 150079cf79abSMichael Braun nest = nla_nest_start(skb, IFLA_MACVLAN_MACADDR_DATA); 150179cf79abSMichael Braun if (nest == NULL) 150279cf79abSMichael Braun goto nla_put_failure; 150379cf79abSMichael Braun 150479cf79abSMichael Braun for (i = 0; i < MACVLAN_HASH_SIZE; i++) { 150579cf79abSMichael Braun if (macvlan_fill_info_macaddr(skb, vlan, i)) 150679cf79abSMichael Braun goto nla_put_failure; 150779cf79abSMichael Braun } 150879cf79abSMichael Braun nla_nest_end(skb, nest); 150979cf79abSMichael Braun } 151027c0b1a8SArnd Bergmann return 0; 151127c0b1a8SArnd Bergmann 151227c0b1a8SArnd Bergmann nla_put_failure: 151327c0b1a8SArnd Bergmann return -EMSGSIZE; 151427c0b1a8SArnd Bergmann } 151527c0b1a8SArnd Bergmann 151627c0b1a8SArnd Bergmann static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = { 151727c0b1a8SArnd Bergmann [IFLA_MACVLAN_MODE] = { .type = NLA_U32 }, 1518df8ef8f3SJohn Fastabend [IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 }, 151979cf79abSMichael Braun [IFLA_MACVLAN_MACADDR_MODE] = { .type = NLA_U32 }, 152079cf79abSMichael Braun [IFLA_MACVLAN_MACADDR] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 152179cf79abSMichael Braun [IFLA_MACVLAN_MACADDR_DATA] = { .type = NLA_NESTED }, 152279cf79abSMichael Braun [IFLA_MACVLAN_MACADDR_COUNT] = { .type = NLA_U32 }, 152327c0b1a8SArnd Bergmann }; 152427c0b1a8SArnd Bergmann 1525fc0663d6SArnd Bergmann int macvlan_link_register(struct rtnl_link_ops *ops) 1526fc0663d6SArnd Bergmann { 1527fc0663d6SArnd Bergmann /* common fields */ 1528fc0663d6SArnd Bergmann ops->priv_size = sizeof(struct macvlan_dev); 1529fc0663d6SArnd Bergmann ops->validate = macvlan_validate; 1530fc0663d6SArnd Bergmann ops->maxtype = IFLA_MACVLAN_MAX; 1531fc0663d6SArnd Bergmann ops->policy = macvlan_policy; 1532fc0663d6SArnd Bergmann ops->changelink = macvlan_changelink; 1533fc0663d6SArnd Bergmann ops->get_size = macvlan_get_size; 1534fc0663d6SArnd Bergmann ops->fill_info = macvlan_fill_info; 1535fc0663d6SArnd Bergmann 1536fc0663d6SArnd Bergmann return rtnl_link_register(ops); 1537fc0663d6SArnd Bergmann }; 1538fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_link_register); 1539fc0663d6SArnd Bergmann 1540eaca400fSNicolas Dichtel static struct net *macvlan_get_link_net(const struct net_device *dev) 1541eaca400fSNicolas Dichtel { 1542eaca400fSNicolas Dichtel return dev_net(macvlan_dev_real_dev(dev)); 1543eaca400fSNicolas Dichtel } 1544eaca400fSNicolas Dichtel 1545fc0663d6SArnd Bergmann static struct rtnl_link_ops macvlan_link_ops = { 1546b863ceb7SPatrick McHardy .kind = "macvlan", 15478a35747aSHerbert Xu .setup = macvlan_setup, 1548b863ceb7SPatrick McHardy .newlink = macvlan_newlink, 1549b863ceb7SPatrick McHardy .dellink = macvlan_dellink, 1550eaca400fSNicolas Dichtel .get_link_net = macvlan_get_link_net, 1551b863ceb7SPatrick McHardy }; 1552b863ceb7SPatrick McHardy 1553b863ceb7SPatrick McHardy static int macvlan_device_event(struct notifier_block *unused, 1554b863ceb7SPatrick McHardy unsigned long event, void *ptr) 1555b863ceb7SPatrick McHardy { 1556351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 1557b863ceb7SPatrick McHardy struct macvlan_dev *vlan, *next; 1558b863ceb7SPatrick McHardy struct macvlan_port *port; 1559226bd341SEric Dumazet LIST_HEAD(list_kill); 1560b863ceb7SPatrick McHardy 1561a35e2c1bSJiri Pirko if (!macvlan_port_exists(dev)) 1562b863ceb7SPatrick McHardy return NOTIFY_DONE; 1563b863ceb7SPatrick McHardy 1564e052f7e6SEric Dumazet port = macvlan_port_get_rtnl(dev); 1565a35e2c1bSJiri Pirko 1566b863ceb7SPatrick McHardy switch (event) { 1567de7d244dSNikolay Aleksandrov case NETDEV_UP: 1568b863ceb7SPatrick McHardy case NETDEV_CHANGE: 1569b863ceb7SPatrick McHardy list_for_each_entry(vlan, &port->vlans, list) 1570fc4a7489SPatrick Mullaney netif_stacked_transfer_operstate(vlan->lowerdev, 1571fc4a7489SPatrick Mullaney vlan->dev); 1572b863ceb7SPatrick McHardy break; 1573b863ceb7SPatrick McHardy case NETDEV_FEAT_CHANGE: 1574b863ceb7SPatrick McHardy list_for_each_entry(vlan, &port->vlans, list) { 15758c2acc53SPatrick McHardy vlan->dev->gso_max_size = dev->gso_max_size; 1576f6773c5eSEric Dumazet vlan->dev->gso_max_segs = dev->gso_max_segs; 1577797f87f8SFlorian Westphal netdev_update_features(vlan->dev); 1578b863ceb7SPatrick McHardy } 1579b863ceb7SPatrick McHardy break; 15803763e7efSdingtianhong case NETDEV_CHANGEMTU: 15813763e7efSdingtianhong list_for_each_entry(vlan, &port->vlans, list) { 15823763e7efSdingtianhong if (vlan->dev->mtu <= dev->mtu) 15833763e7efSdingtianhong continue; 15843763e7efSdingtianhong dev_set_mtu(vlan->dev, dev->mtu); 15853763e7efSdingtianhong } 15863763e7efSdingtianhong break; 1587e289fd28Sdingtianhong case NETDEV_CHANGEADDR: 1588e289fd28Sdingtianhong if (!port->passthru) 1589e289fd28Sdingtianhong return NOTIFY_DONE; 1590e289fd28Sdingtianhong 1591e289fd28Sdingtianhong vlan = list_first_entry_or_null(&port->vlans, 1592e289fd28Sdingtianhong struct macvlan_dev, 1593e289fd28Sdingtianhong list); 1594e289fd28Sdingtianhong 1595e289fd28Sdingtianhong if (macvlan_sync_address(vlan->dev, dev->dev_addr)) 1596e289fd28Sdingtianhong return NOTIFY_BAD; 1597e289fd28Sdingtianhong 1598e289fd28Sdingtianhong break; 1599b863ceb7SPatrick McHardy case NETDEV_UNREGISTER: 16003b27e105SDavid Lamparter /* twiddle thumbs on netns device moves */ 16013b27e105SDavid Lamparter if (dev->reg_state != NETREG_UNREGISTERING) 16023b27e105SDavid Lamparter break; 16033b27e105SDavid Lamparter 1604b863ceb7SPatrick McHardy list_for_each_entry_safe(vlan, next, &port->vlans, list) 1605226bd341SEric Dumazet vlan->dev->rtnl_link_ops->dellink(vlan->dev, &list_kill); 1606226bd341SEric Dumazet unregister_netdevice_many(&list_kill); 1607b863ceb7SPatrick McHardy break; 16081c01fe14SJiri Pirko case NETDEV_PRE_TYPE_CHANGE: 16091c01fe14SJiri Pirko /* Forbid underlaying device to change its type. */ 16101c01fe14SJiri Pirko return NOTIFY_BAD; 16114c991255SVlad Yasevich 16124c991255SVlad Yasevich case NETDEV_NOTIFY_PEERS: 16134c991255SVlad Yasevich case NETDEV_BONDING_FAILOVER: 16144c991255SVlad Yasevich case NETDEV_RESEND_IGMP: 16154c991255SVlad Yasevich /* Propagate to all vlans */ 16164c991255SVlad Yasevich list_for_each_entry(vlan, &port->vlans, list) 16174c991255SVlad Yasevich call_netdevice_notifiers(event, vlan->dev); 1618b863ceb7SPatrick McHardy } 1619b863ceb7SPatrick McHardy return NOTIFY_DONE; 1620b863ceb7SPatrick McHardy } 1621b863ceb7SPatrick McHardy 1622b863ceb7SPatrick McHardy static struct notifier_block macvlan_notifier_block __read_mostly = { 1623b863ceb7SPatrick McHardy .notifier_call = macvlan_device_event, 1624b863ceb7SPatrick McHardy }; 1625b863ceb7SPatrick McHardy 1626b863ceb7SPatrick McHardy static int __init macvlan_init_module(void) 1627b863ceb7SPatrick McHardy { 1628b863ceb7SPatrick McHardy int err; 1629b863ceb7SPatrick McHardy 1630b863ceb7SPatrick McHardy register_netdevice_notifier(&macvlan_notifier_block); 1631b863ceb7SPatrick McHardy 1632fc0663d6SArnd Bergmann err = macvlan_link_register(&macvlan_link_ops); 1633b863ceb7SPatrick McHardy if (err < 0) 1634b863ceb7SPatrick McHardy goto err1; 1635b863ceb7SPatrick McHardy return 0; 1636b863ceb7SPatrick McHardy err1: 1637b863ceb7SPatrick McHardy unregister_netdevice_notifier(&macvlan_notifier_block); 1638b863ceb7SPatrick McHardy return err; 1639b863ceb7SPatrick McHardy } 1640b863ceb7SPatrick McHardy 1641b863ceb7SPatrick McHardy static void __exit macvlan_cleanup_module(void) 1642b863ceb7SPatrick McHardy { 1643b863ceb7SPatrick McHardy rtnl_link_unregister(&macvlan_link_ops); 1644b863ceb7SPatrick McHardy unregister_netdevice_notifier(&macvlan_notifier_block); 1645b863ceb7SPatrick McHardy } 1646b863ceb7SPatrick McHardy 1647b863ceb7SPatrick McHardy module_init(macvlan_init_module); 1648b863ceb7SPatrick McHardy module_exit(macvlan_cleanup_module); 1649b863ceb7SPatrick McHardy 1650b863ceb7SPatrick McHardy MODULE_LICENSE("GPL"); 1651b863ceb7SPatrick McHardy MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); 1652b863ceb7SPatrick McHardy MODULE_DESCRIPTION("Driver for MAC address based VLANs"); 1653b863ceb7SPatrick McHardy MODULE_ALIAS_RTNL_LINK("macvlan"); 1654