xref: /openbmc/linux/drivers/net/macvlan.c (revision b97d6790d03b763eca08847a9a5869a4291b9f9a)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2b863ceb7SPatrick McHardy /*
3b863ceb7SPatrick McHardy  * Copyright (c) 2007 Patrick McHardy <kaber@trash.net>
4b863ceb7SPatrick McHardy  *
5b863ceb7SPatrick McHardy  * The code this is based on carried the following copyright notice:
6b863ceb7SPatrick McHardy  * ---
7b863ceb7SPatrick McHardy  * (C) Copyright 2001-2006
8b863ceb7SPatrick McHardy  * Alex Zeffertt, Cambridge Broadband Ltd, ajz@cambridgebroadband.com
9b863ceb7SPatrick McHardy  * Re-worked by Ben Greear <greearb@candelatech.com>
10b863ceb7SPatrick McHardy  * ---
11b863ceb7SPatrick McHardy  */
12b863ceb7SPatrick McHardy #include <linux/kernel.h>
13b863ceb7SPatrick McHardy #include <linux/types.h>
14b863ceb7SPatrick McHardy #include <linux/module.h>
15b863ceb7SPatrick McHardy #include <linux/init.h>
16b863ceb7SPatrick McHardy #include <linux/errno.h>
17b863ceb7SPatrick McHardy #include <linux/slab.h>
18b863ceb7SPatrick McHardy #include <linux/string.h>
1982524746SFranck Bui-Huu #include <linux/rculist.h>
20b863ceb7SPatrick McHardy #include <linux/notifier.h>
21b863ceb7SPatrick McHardy #include <linux/netdevice.h>
22b863ceb7SPatrick McHardy #include <linux/etherdevice.h>
23254c0a2bSHangbin Liu #include <linux/net_tstamp.h>
24b863ceb7SPatrick McHardy #include <linux/ethtool.h>
25b863ceb7SPatrick McHardy #include <linux/if_arp.h>
2687002b03SJiri Pirko #include <linux/if_vlan.h>
27b863ceb7SPatrick McHardy #include <linux/if_link.h>
28b863ceb7SPatrick McHardy #include <linux/if_macvlan.h>
29cd431e73SEric Dumazet #include <linux/hash.h>
30412ca155SHerbert Xu #include <linux/workqueue.h>
31b863ceb7SPatrick McHardy #include <net/rtnetlink.h>
32618e1b74SArnd Bergmann #include <net/xfrm.h>
33688cea83Sdingtianhong #include <linux/netpoll.h>
34254c0a2bSHangbin Liu #include <linux/phy.h>
35b863ceb7SPatrick McHardy 
3679cf79abSMichael Braun #define MACVLAN_HASH_BITS	8
3779cf79abSMichael Braun #define MACVLAN_HASH_SIZE	(1<<MACVLAN_HASH_BITS)
38d4bff72cSThomas Karlsson #define MACVLAN_DEFAULT_BC_QUEUE_LEN	1000
39b863ceb7SPatrick McHardy 
4043c2d578SVlad Yasevich #define MACVLAN_F_PASSTHRU	1
4118c8c54dSVlad Yasevich #define MACVLAN_F_ADDRCHANGE	2
4243c2d578SVlad Yasevich 
43b863ceb7SPatrick McHardy struct macvlan_port {
44b863ceb7SPatrick McHardy 	struct net_device	*dev;
45b863ceb7SPatrick McHardy 	struct hlist_head	vlan_hash[MACVLAN_HASH_SIZE];
46b863ceb7SPatrick McHardy 	struct list_head	vlans;
47412ca155SHerbert Xu 	struct sk_buff_head	bc_queue;
48412ca155SHerbert Xu 	struct work_struct	bc_work;
49d4bff72cSThomas Karlsson 	u32			bc_queue_len_used;
50954d1fa1SHerbert Xu 	int			bc_cutoff;
5143c2d578SVlad Yasevich 	u32			flags;
525e3c516bSDavid S. Miller 	int			count;
5379cf79abSMichael Braun 	struct hlist_head	vlan_source_hash[MACVLAN_HASH_SIZE];
54d45276e7SHerbert Xu 	DECLARE_BITMAP(bc_filter, MACVLAN_MC_FILTER_SZ);
559c127a01SHerbert Xu 	DECLARE_BITMAP(mc_filter, MACVLAN_MC_FILTER_SZ);
5618c8c54dSVlad Yasevich 	unsigned char           perm_addr[ETH_ALEN];
5779cf79abSMichael Braun };
5879cf79abSMichael Braun 
5979cf79abSMichael Braun struct macvlan_source_entry {
6079cf79abSMichael Braun 	struct hlist_node	hlist;
6179cf79abSMichael Braun 	struct macvlan_dev	*vlan;
6279cf79abSMichael Braun 	unsigned char		addr[6+2] __aligned(sizeof(u16));
6379cf79abSMichael Braun 	struct rcu_head		rcu;
64b863ceb7SPatrick McHardy };
65b863ceb7SPatrick McHardy 
66412ca155SHerbert Xu struct macvlan_skb_cb {
67412ca155SHerbert Xu 	const struct macvlan_dev *src;
68412ca155SHerbert Xu };
69412ca155SHerbert Xu 
70412ca155SHerbert Xu #define MACVLAN_SKB_CB(__skb) ((struct macvlan_skb_cb *)&((__skb)->cb[0]))
71412ca155SHerbert Xu 
72d5cd9244SEric W. Biederman static void macvlan_port_destroy(struct net_device *dev);
73d4bff72cSThomas Karlsson static void update_port_bc_queue_len(struct macvlan_port *port);
74d5cd9244SEric W. Biederman 
macvlan_passthru(const struct macvlan_port * port)7543c2d578SVlad Yasevich static inline bool macvlan_passthru(const struct macvlan_port *port)
7643c2d578SVlad Yasevich {
7743c2d578SVlad Yasevich 	return port->flags & MACVLAN_F_PASSTHRU;
7843c2d578SVlad Yasevich }
7943c2d578SVlad Yasevich 
macvlan_set_passthru(struct macvlan_port * port)8043c2d578SVlad Yasevich static inline void macvlan_set_passthru(struct macvlan_port *port)
8143c2d578SVlad Yasevich {
8243c2d578SVlad Yasevich 	port->flags |= MACVLAN_F_PASSTHRU;
8343c2d578SVlad Yasevich }
8443c2d578SVlad Yasevich 
macvlan_addr_change(const struct macvlan_port * port)8518c8c54dSVlad Yasevich static inline bool macvlan_addr_change(const struct macvlan_port *port)
8618c8c54dSVlad Yasevich {
8718c8c54dSVlad Yasevich 	return port->flags & MACVLAN_F_ADDRCHANGE;
8818c8c54dSVlad Yasevich }
8918c8c54dSVlad Yasevich 
macvlan_set_addr_change(struct macvlan_port * port)9018c8c54dSVlad Yasevich static inline void macvlan_set_addr_change(struct macvlan_port *port)
9118c8c54dSVlad Yasevich {
9218c8c54dSVlad Yasevich 	port->flags |= MACVLAN_F_ADDRCHANGE;
9318c8c54dSVlad Yasevich }
9418c8c54dSVlad Yasevich 
macvlan_clear_addr_change(struct macvlan_port * port)9518c8c54dSVlad Yasevich static inline void macvlan_clear_addr_change(struct macvlan_port *port)
9618c8c54dSVlad Yasevich {
9718c8c54dSVlad Yasevich 	port->flags &= ~MACVLAN_F_ADDRCHANGE;
9818c8c54dSVlad Yasevich }
9918c8c54dSVlad Yasevich 
10079cf79abSMichael Braun /* Hash Ethernet address */
macvlan_eth_hash(const unsigned char * addr)10179cf79abSMichael Braun static u32 macvlan_eth_hash(const unsigned char *addr)
10279cf79abSMichael Braun {
10379cf79abSMichael Braun 	u64 value = get_unaligned((u64 *)addr);
10479cf79abSMichael Braun 
10579cf79abSMichael Braun 	/* only want 6 bytes */
10679cf79abSMichael Braun #ifdef __BIG_ENDIAN
10779cf79abSMichael Braun 	value >>= 16;
10879cf79abSMichael Braun #else
10979cf79abSMichael Braun 	value <<= 16;
11079cf79abSMichael Braun #endif
11179cf79abSMichael Braun 	return hash_64(value, MACVLAN_HASH_BITS);
11279cf79abSMichael Braun }
11379cf79abSMichael Braun 
macvlan_port_get_rcu(const struct net_device * dev)114e052f7e6SEric Dumazet static struct macvlan_port *macvlan_port_get_rcu(const struct net_device *dev)
115e052f7e6SEric Dumazet {
116e052f7e6SEric Dumazet 	return rcu_dereference(dev->rx_handler_data);
117e052f7e6SEric Dumazet }
118e052f7e6SEric Dumazet 
macvlan_port_get_rtnl(const struct net_device * dev)119e052f7e6SEric Dumazet static struct macvlan_port *macvlan_port_get_rtnl(const struct net_device *dev)
120e052f7e6SEric Dumazet {
121e052f7e6SEric Dumazet 	return rtnl_dereference(dev->rx_handler_data);
122e052f7e6SEric Dumazet }
123e052f7e6SEric Dumazet 
macvlan_hash_lookup(const struct macvlan_port * port,const unsigned char * addr)124b863ceb7SPatrick McHardy static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
125b863ceb7SPatrick McHardy 					       const unsigned char *addr)
126b863ceb7SPatrick McHardy {
127b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan;
12879cf79abSMichael Braun 	u32 idx = macvlan_eth_hash(addr);
129b863ceb7SPatrick McHardy 
13058e64a31SWei Yongjun 	hlist_for_each_entry_rcu(vlan, &port->vlan_hash[idx], hlist,
13158e64a31SWei Yongjun 				 lockdep_rtnl_is_held()) {
132a6700db1SJoe Perches 		if (ether_addr_equal_64bits(vlan->dev->dev_addr, addr))
133b863ceb7SPatrick McHardy 			return vlan;
134b863ceb7SPatrick McHardy 	}
135b863ceb7SPatrick McHardy 	return NULL;
136b863ceb7SPatrick McHardy }
137b863ceb7SPatrick McHardy 
macvlan_hash_lookup_source(const struct macvlan_dev * vlan,const unsigned char * addr)13879cf79abSMichael Braun static struct macvlan_source_entry *macvlan_hash_lookup_source(
13979cf79abSMichael Braun 	const struct macvlan_dev *vlan,
14079cf79abSMichael Braun 	const unsigned char *addr)
14179cf79abSMichael Braun {
14279cf79abSMichael Braun 	struct macvlan_source_entry *entry;
14379cf79abSMichael Braun 	u32 idx = macvlan_eth_hash(addr);
14479cf79abSMichael Braun 	struct hlist_head *h = &vlan->port->vlan_source_hash[idx];
14579cf79abSMichael Braun 
1465df1341eSChuang Wang 	hlist_for_each_entry_rcu(entry, h, hlist, lockdep_rtnl_is_held()) {
14779cf79abSMichael Braun 		if (ether_addr_equal_64bits(entry->addr, addr) &&
14879cf79abSMichael Braun 		    entry->vlan == vlan)
14979cf79abSMichael Braun 			return entry;
15079cf79abSMichael Braun 	}
15179cf79abSMichael Braun 	return NULL;
15279cf79abSMichael Braun }
15379cf79abSMichael Braun 
macvlan_hash_add_source(struct macvlan_dev * vlan,const unsigned char * addr)15479cf79abSMichael Braun static int macvlan_hash_add_source(struct macvlan_dev *vlan,
15579cf79abSMichael Braun 				   const unsigned char *addr)
15679cf79abSMichael Braun {
15779cf79abSMichael Braun 	struct macvlan_port *port = vlan->port;
15879cf79abSMichael Braun 	struct macvlan_source_entry *entry;
15979cf79abSMichael Braun 	struct hlist_head *h;
16079cf79abSMichael Braun 
16179cf79abSMichael Braun 	entry = macvlan_hash_lookup_source(vlan, addr);
16279cf79abSMichael Braun 	if (entry)
16379cf79abSMichael Braun 		return 0;
16479cf79abSMichael Braun 
16579cf79abSMichael Braun 	entry = kmalloc(sizeof(*entry), GFP_KERNEL);
16679cf79abSMichael Braun 	if (!entry)
16779cf79abSMichael Braun 		return -ENOMEM;
16879cf79abSMichael Braun 
16979cf79abSMichael Braun 	ether_addr_copy(entry->addr, addr);
17079cf79abSMichael Braun 	entry->vlan = vlan;
17179cf79abSMichael Braun 	h = &port->vlan_source_hash[macvlan_eth_hash(addr)];
17279cf79abSMichael Braun 	hlist_add_head_rcu(&entry->hlist, h);
17379cf79abSMichael Braun 	vlan->macaddr_count++;
17479cf79abSMichael Braun 
17579cf79abSMichael Braun 	return 0;
17679cf79abSMichael Braun }
17779cf79abSMichael Braun 
macvlan_hash_add(struct macvlan_dev * vlan)178f9ac30f0SEric Biederman static void macvlan_hash_add(struct macvlan_dev *vlan)
179f9ac30f0SEric Biederman {
180f9ac30f0SEric Biederman 	struct macvlan_port *port = vlan->port;
181f9ac30f0SEric Biederman 	const unsigned char *addr = vlan->dev->dev_addr;
18279cf79abSMichael Braun 	u32 idx = macvlan_eth_hash(addr);
183f9ac30f0SEric Biederman 
18479cf79abSMichael Braun 	hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[idx]);
18579cf79abSMichael Braun }
18679cf79abSMichael Braun 
macvlan_hash_del_source(struct macvlan_source_entry * entry)18779cf79abSMichael Braun static void macvlan_hash_del_source(struct macvlan_source_entry *entry)
18879cf79abSMichael Braun {
18979cf79abSMichael Braun 	hlist_del_rcu(&entry->hlist);
19079cf79abSMichael Braun 	kfree_rcu(entry, rcu);
191f9ac30f0SEric Biederman }
192f9ac30f0SEric Biederman 
macvlan_hash_del(struct macvlan_dev * vlan,bool sync)193449f4544SEric Dumazet static void macvlan_hash_del(struct macvlan_dev *vlan, bool sync)
194f9ac30f0SEric Biederman {
195f9ac30f0SEric Biederman 	hlist_del_rcu(&vlan->hlist);
196449f4544SEric Dumazet 	if (sync)
197f9ac30f0SEric Biederman 		synchronize_rcu();
198f9ac30f0SEric Biederman }
199f9ac30f0SEric Biederman 
macvlan_hash_change_addr(struct macvlan_dev * vlan,const unsigned char * addr)200f9ac30f0SEric Biederman static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
201f9ac30f0SEric Biederman 					const unsigned char *addr)
202f9ac30f0SEric Biederman {
203449f4544SEric Dumazet 	macvlan_hash_del(vlan, true);
204f9ac30f0SEric Biederman 	/* Now that we are unhashed it is safe to change the device
205f9ac30f0SEric Biederman 	 * address without confusing packet delivery.
206f9ac30f0SEric Biederman 	 */
2072f23e5ceSJakub Kicinski 	eth_hw_addr_set(vlan->dev, addr);
208f9ac30f0SEric Biederman 	macvlan_hash_add(vlan);
209f9ac30f0SEric Biederman }
210f9ac30f0SEric Biederman 
macvlan_addr_busy(const struct macvlan_port * port,const unsigned char * addr)211d94d0254SGao Feng static bool macvlan_addr_busy(const struct macvlan_port *port,
212f9ac30f0SEric Biederman 			      const unsigned char *addr)
213f9ac30f0SEric Biederman {
21418c8c54dSVlad Yasevich 	/* Test to see if the specified address is
215f9ac30f0SEric Biederman 	 * currently in use by the underlying device or
216f9ac30f0SEric Biederman 	 * another macvlan.
217f9ac30f0SEric Biederman 	 */
21818c8c54dSVlad Yasevich 	if (!macvlan_passthru(port) && !macvlan_addr_change(port) &&
219e696cda7SVlad Yasevich 	    ether_addr_equal_64bits(port->dev->dev_addr, addr))
220d94d0254SGao Feng 		return true;
221f9ac30f0SEric Biederman 
222f9ac30f0SEric Biederman 	if (macvlan_hash_lookup(port, addr))
223d94d0254SGao Feng 		return true;
224f9ac30f0SEric Biederman 
225d94d0254SGao Feng 	return false;
226f9ac30f0SEric Biederman }
227f9ac30f0SEric Biederman 
228a1e514c5SArnd Bergmann 
macvlan_broadcast_one(struct sk_buff * skb,const struct macvlan_dev * vlan,const struct ethhdr * eth,bool local)229fc0663d6SArnd Bergmann static int macvlan_broadcast_one(struct sk_buff *skb,
230fc0663d6SArnd Bergmann 				 const struct macvlan_dev *vlan,
231618e1b74SArnd Bergmann 				 const struct ethhdr *eth, bool local)
232a1e514c5SArnd Bergmann {
233fc0663d6SArnd Bergmann 	struct net_device *dev = vlan->dev;
234a1e514c5SArnd Bergmann 
235618e1b74SArnd Bergmann 	if (local)
236412ca155SHerbert Xu 		return __dev_forward_skb(dev, skb);
237618e1b74SArnd Bergmann 
238a1e514c5SArnd Bergmann 	skb->dev = dev;
239a6700db1SJoe Perches 	if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast))
240a1e514c5SArnd Bergmann 		skb->pkt_type = PACKET_BROADCAST;
241a1e514c5SArnd Bergmann 	else
242a1e514c5SArnd Bergmann 		skb->pkt_type = PACKET_MULTICAST;
243a1e514c5SArnd Bergmann 
244412ca155SHerbert Xu 	return 0;
245a1e514c5SArnd Bergmann }
246a1e514c5SArnd Bergmann 
macvlan_hash_mix(const struct macvlan_dev * vlan)2473807ff58SEric Dumazet static u32 macvlan_hash_mix(const struct macvlan_dev *vlan)
2483807ff58SEric Dumazet {
2493807ff58SEric Dumazet 	return (u32)(((unsigned long)vlan) >> L1_CACHE_SHIFT);
2503807ff58SEric Dumazet }
2513807ff58SEric Dumazet 
2523807ff58SEric Dumazet 
mc_hash(const struct macvlan_dev * vlan,const unsigned char * addr)2533807ff58SEric Dumazet static unsigned int mc_hash(const struct macvlan_dev *vlan,
2543807ff58SEric Dumazet 			    const unsigned char *addr)
255cd431e73SEric Dumazet {
256cd431e73SEric Dumazet 	u32 val = __get_unaligned_cpu32(addr + 2);
257cd431e73SEric Dumazet 
2583807ff58SEric Dumazet 	val ^= macvlan_hash_mix(vlan);
259cd431e73SEric Dumazet 	return hash_32(val, MACVLAN_MC_FILTER_BITS);
260cd431e73SEric Dumazet }
261cd431e73SEric Dumazet 
macvlan_broadcast(struct sk_buff * skb,const struct macvlan_port * port,struct net_device * src,enum macvlan_mode mode)262b863ceb7SPatrick McHardy static void macvlan_broadcast(struct sk_buff *skb,
263618e1b74SArnd Bergmann 			      const struct macvlan_port *port,
264618e1b74SArnd Bergmann 			      struct net_device *src,
265618e1b74SArnd Bergmann 			      enum macvlan_mode mode)
266b863ceb7SPatrick McHardy {
2671712b2ffSEric Dumazet 	const struct ethhdr *eth = eth_hdr(skb);
268b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan;
269b863ceb7SPatrick McHardy 	struct sk_buff *nskb;
270b863ceb7SPatrick McHardy 	unsigned int i;
271a1e514c5SArnd Bergmann 	int err;
2723807ff58SEric Dumazet 	unsigned int hash;
273b863ceb7SPatrick McHardy 
274efbbced3SPatrick McHardy 	if (skb->protocol == htons(ETH_P_PAUSE))
275efbbced3SPatrick McHardy 		return;
276efbbced3SPatrick McHardy 
277bb23ffa1SChristophe JAILLET 	hash_for_each_rcu(port->vlan_hash, i, vlan, hlist) {
278618e1b74SArnd Bergmann 		if (vlan->dev == src || !(vlan->mode & mode))
279618e1b74SArnd Bergmann 			continue;
280618e1b74SArnd Bergmann 
2813807ff58SEric Dumazet 		hash = mc_hash(vlan, eth->h_dest);
282cd431e73SEric Dumazet 		if (!test_bit(hash, vlan->mc_filter))
283cd431e73SEric Dumazet 			continue;
284de9e8f3fSHerbert Xu 
285de9e8f3fSHerbert Xu 		err = NET_RX_DROP;
286b863ceb7SPatrick McHardy 		nskb = skb_clone(skb, GFP_ATOMIC);
287de9e8f3fSHerbert Xu 		if (likely(nskb))
288bb23ffa1SChristophe JAILLET 			err = macvlan_broadcast_one(nskb, vlan, eth,
289412ca155SHerbert Xu 					mode == MACVLAN_MODE_BRIDGE) ?:
290566214f4SSebastian Andrzej Siewior 			      netif_rx(nskb);
291a1e514c5SArnd Bergmann 		macvlan_count_rx(vlan, skb->len + ETH_HLEN,
2924c979935Sjbaron@akamai.com 				 err == NET_RX_SUCCESS, true);
293b863ceb7SPatrick McHardy 	}
294b863ceb7SPatrick McHardy }
295b863ceb7SPatrick McHardy 
macvlan_multicast_rx(const struct macvlan_port * port,const struct macvlan_dev * src,struct sk_buff * skb)296d45276e7SHerbert Xu static void macvlan_multicast_rx(const struct macvlan_port *port,
297d45276e7SHerbert Xu 				 const struct macvlan_dev *src,
298d45276e7SHerbert Xu 				 struct sk_buff *skb)
299412ca155SHerbert Xu {
300412ca155SHerbert Xu 	if (!src)
301412ca155SHerbert Xu 		/* frame comes from an external address */
302412ca155SHerbert Xu 		macvlan_broadcast(skb, port, NULL,
303412ca155SHerbert Xu 				  MACVLAN_MODE_PRIVATE |
304412ca155SHerbert Xu 				  MACVLAN_MODE_VEPA    |
305412ca155SHerbert Xu 				  MACVLAN_MODE_PASSTHRU|
306412ca155SHerbert Xu 				  MACVLAN_MODE_BRIDGE);
307412ca155SHerbert Xu 	else if (src->mode == MACVLAN_MODE_VEPA)
308412ca155SHerbert Xu 		/* flood to everyone except source */
309412ca155SHerbert Xu 		macvlan_broadcast(skb, port, src->dev,
310412ca155SHerbert Xu 				  MACVLAN_MODE_VEPA |
311412ca155SHerbert Xu 				  MACVLAN_MODE_BRIDGE);
312412ca155SHerbert Xu 	else
313412ca155SHerbert Xu 		/*
314412ca155SHerbert Xu 		 * flood only to VEPA ports, bridge ports
315412ca155SHerbert Xu 		 * already saw the frame on the way out.
316412ca155SHerbert Xu 		 */
317412ca155SHerbert Xu 		macvlan_broadcast(skb, port, src->dev,
318412ca155SHerbert Xu 				  MACVLAN_MODE_VEPA);
319d45276e7SHerbert Xu }
320412ca155SHerbert Xu 
macvlan_process_broadcast(struct work_struct * w)321d45276e7SHerbert Xu static void macvlan_process_broadcast(struct work_struct *w)
322d45276e7SHerbert Xu {
323d45276e7SHerbert Xu 	struct macvlan_port *port = container_of(w, struct macvlan_port,
324d45276e7SHerbert Xu 						 bc_work);
325d45276e7SHerbert Xu 	struct sk_buff *skb;
326d45276e7SHerbert Xu 	struct sk_buff_head list;
327d45276e7SHerbert Xu 
328d45276e7SHerbert Xu 	__skb_queue_head_init(&list);
329d45276e7SHerbert Xu 
330d45276e7SHerbert Xu 	spin_lock_bh(&port->bc_queue.lock);
331d45276e7SHerbert Xu 	skb_queue_splice_tail_init(&port->bc_queue, &list);
332d45276e7SHerbert Xu 	spin_unlock_bh(&port->bc_queue.lock);
333d45276e7SHerbert Xu 
334d45276e7SHerbert Xu 	while ((skb = __skb_dequeue(&list))) {
335d45276e7SHerbert Xu 		const struct macvlan_dev *src = MACVLAN_SKB_CB(skb)->src;
336d45276e7SHerbert Xu 
337d45276e7SHerbert Xu 		rcu_read_lock();
338d45276e7SHerbert Xu 		macvlan_multicast_rx(port, src, skb);
339412ca155SHerbert Xu 		rcu_read_unlock();
340412ca155SHerbert Xu 
341260916dfSHerbert Xu 		if (src)
342260916dfSHerbert Xu 			dev_put(src->dev);
343bf97403aSYang Wei 		consume_skb(skb);
344ce9a4186SMahesh Bandewar 
345ce9a4186SMahesh Bandewar 		cond_resched();
346412ca155SHerbert Xu 	}
347412ca155SHerbert Xu }
348412ca155SHerbert Xu 
macvlan_broadcast_enqueue(struct macvlan_port * port,const struct macvlan_dev * src,struct sk_buff * skb)349412ca155SHerbert Xu static void macvlan_broadcast_enqueue(struct macvlan_port *port,
350260916dfSHerbert Xu 				      const struct macvlan_dev *src,
351412ca155SHerbert Xu 				      struct sk_buff *skb)
352412ca155SHerbert Xu {
353e676f197SHerbert Xu 	struct sk_buff *nskb;
354412ca155SHerbert Xu 	int err = -ENOMEM;
355412ca155SHerbert Xu 
356e676f197SHerbert Xu 	nskb = skb_clone(skb, GFP_ATOMIC);
357e676f197SHerbert Xu 	if (!nskb)
358412ca155SHerbert Xu 		goto err;
359412ca155SHerbert Xu 
360260916dfSHerbert Xu 	MACVLAN_SKB_CB(nskb)->src = src;
361260916dfSHerbert Xu 
362412ca155SHerbert Xu 	spin_lock(&port->bc_queue.lock);
363d4bff72cSThomas Karlsson 	if (skb_queue_len(&port->bc_queue) < port->bc_queue_len_used) {
364260916dfSHerbert Xu 		if (src)
365260916dfSHerbert Xu 			dev_hold(src->dev);
366e676f197SHerbert Xu 		__skb_queue_tail(&port->bc_queue, nskb);
367412ca155SHerbert Xu 		err = 0;
368412ca155SHerbert Xu 	}
369412ca155SHerbert Xu 	spin_unlock(&port->bc_queue.lock);
370412ca155SHerbert Xu 
3713d6642eaSzhangxiangqian 	queue_work(system_unbound_wq, &port->bc_work);
3721d7ea556SMenglong Dong 
373412ca155SHerbert Xu 	if (err)
374e676f197SHerbert Xu 		goto free_nskb;
375412ca155SHerbert Xu 
376412ca155SHerbert Xu 	return;
377412ca155SHerbert Xu 
378e676f197SHerbert Xu free_nskb:
379e676f197SHerbert Xu 	kfree_skb(nskb);
380412ca155SHerbert Xu err:
381625788b5SEric Dumazet 	dev_core_stats_rx_dropped_inc(skb->dev);
382412ca155SHerbert Xu }
383412ca155SHerbert Xu 
macvlan_flush_sources(struct macvlan_port * port,struct macvlan_dev * vlan)38479cf79abSMichael Braun static void macvlan_flush_sources(struct macvlan_port *port,
38579cf79abSMichael Braun 				  struct macvlan_dev *vlan)
38679cf79abSMichael Braun {
387bb23ffa1SChristophe JAILLET 	struct macvlan_source_entry *entry;
388bb23ffa1SChristophe JAILLET 	struct hlist_node *next;
38979cf79abSMichael Braun 	int i;
39079cf79abSMichael Braun 
391bb23ffa1SChristophe JAILLET 	hash_for_each_safe(port->vlan_source_hash, i, next, entry, hlist)
39279cf79abSMichael Braun 		if (entry->vlan == vlan)
39379cf79abSMichael Braun 			macvlan_hash_del_source(entry);
394bb23ffa1SChristophe JAILLET 
39579cf79abSMichael Braun 	vlan->macaddr_count = 0;
39679cf79abSMichael Braun }
39779cf79abSMichael Braun 
macvlan_forward_source_one(struct sk_buff * skb,struct macvlan_dev * vlan)39879cf79abSMichael Braun static void macvlan_forward_source_one(struct sk_buff *skb,
39979cf79abSMichael Braun 				       struct macvlan_dev *vlan)
40079cf79abSMichael Braun {
40179cf79abSMichael Braun 	struct sk_buff *nskb;
40279cf79abSMichael Braun 	struct net_device *dev;
40379cf79abSMichael Braun 	int len;
40479cf79abSMichael Braun 	int ret;
40579cf79abSMichael Braun 
40679cf79abSMichael Braun 	dev = vlan->dev;
40779cf79abSMichael Braun 	if (unlikely(!(dev->flags & IFF_UP)))
40879cf79abSMichael Braun 		return;
40979cf79abSMichael Braun 
41079cf79abSMichael Braun 	nskb = skb_clone(skb, GFP_ATOMIC);
41179cf79abSMichael Braun 	if (!nskb)
41279cf79abSMichael Braun 		return;
41379cf79abSMichael Braun 
41479cf79abSMichael Braun 	len = nskb->len + ETH_HLEN;
41579cf79abSMichael Braun 	nskb->dev = dev;
416c8c41ea1SAlexander Duyck 
417c8c41ea1SAlexander Duyck 	if (ether_addr_equal_64bits(eth_hdr(skb)->h_dest, dev->dev_addr))
41879cf79abSMichael Braun 		nskb->pkt_type = PACKET_HOST;
41979cf79abSMichael Braun 
420baebdf48SSebastian Andrzej Siewior 	ret = __netif_rx(nskb);
4214c979935Sjbaron@akamai.com 	macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, false);
42279cf79abSMichael Braun }
42379cf79abSMichael Braun 
macvlan_forward_source(struct sk_buff * skb,struct macvlan_port * port,const unsigned char * addr)424427f0c8cSJethro Beekman static bool macvlan_forward_source(struct sk_buff *skb,
42579cf79abSMichael Braun 				   struct macvlan_port *port,
42679cf79abSMichael Braun 				   const unsigned char *addr)
42779cf79abSMichael Braun {
42879cf79abSMichael Braun 	struct macvlan_source_entry *entry;
42979cf79abSMichael Braun 	u32 idx = macvlan_eth_hash(addr);
43079cf79abSMichael Braun 	struct hlist_head *h = &port->vlan_source_hash[idx];
431427f0c8cSJethro Beekman 	bool consume = false;
43279cf79abSMichael Braun 
43379cf79abSMichael Braun 	hlist_for_each_entry_rcu(entry, h, hlist) {
434427f0c8cSJethro Beekman 		if (ether_addr_equal_64bits(entry->addr, addr)) {
435427f0c8cSJethro Beekman 			if (entry->vlan->flags & MACVLAN_FLAG_NODST)
436427f0c8cSJethro Beekman 				consume = true;
43779cf79abSMichael Braun 			macvlan_forward_source_one(skb, entry->vlan);
43879cf79abSMichael Braun 		}
43979cf79abSMichael Braun 	}
44079cf79abSMichael Braun 
441427f0c8cSJethro Beekman 	return consume;
442427f0c8cSJethro Beekman }
443427f0c8cSJethro Beekman 
444b863ceb7SPatrick McHardy /* called under rcu_read_lock() from netif_receive_skb */
macvlan_handle_frame(struct sk_buff ** pskb)4458a4eb573SJiri Pirko static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb)
446b863ceb7SPatrick McHardy {
447ab95bfe0SJiri Pirko 	struct macvlan_port *port;
4488a4eb573SJiri Pirko 	struct sk_buff *skb = *pskb;
449b863ceb7SPatrick McHardy 	const struct ethhdr *eth = eth_hdr(skb);
450b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan;
451618e1b74SArnd Bergmann 	const struct macvlan_dev *src;
452b863ceb7SPatrick McHardy 	struct net_device *dev;
453ba01877fSSridhar Samudrala 	unsigned int len = 0;
454d1dd9119Sjbaron@akamai.com 	int ret;
455d1dd9119Sjbaron@akamai.com 	rx_handler_result_t handle_res;
456b863ceb7SPatrick McHardy 
45781f3dc93SAlexander Sverdlin 	/* Packets from dev_loopback_xmit() do not have L2 header, bail out */
45881f3dc93SAlexander Sverdlin 	if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
45981f3dc93SAlexander Sverdlin 		return RX_HANDLER_PASS;
46081f3dc93SAlexander Sverdlin 
461a35e2c1bSJiri Pirko 	port = macvlan_port_get_rcu(skb->dev);
462b863ceb7SPatrick McHardy 	if (is_multicast_ether_addr(eth->h_dest)) {
4639c127a01SHerbert Xu 		unsigned int hash;
4649c127a01SHerbert Xu 
46519bcf9f2SEric W. Biederman 		skb = ip_check_defrag(dev_net(skb->dev), skb, IP_DEFRAG_MACVLAN);
466bc416d97SEric Dumazet 		if (!skb)
467bc416d97SEric Dumazet 			return RX_HANDLER_CONSUMED;
468e639b8d8SSabrina Dubroca 		*pskb = skb;
4694ec7ac12SEric Dumazet 		eth = eth_hdr(skb);
470e16b8598SMartin Willi 		if (macvlan_forward_source(skb, port, eth->h_source)) {
471e16b8598SMartin Willi 			kfree_skb(skb);
472427f0c8cSJethro Beekman 			return RX_HANDLER_CONSUMED;
473e16b8598SMartin Willi 		}
474618e1b74SArnd Bergmann 		src = macvlan_hash_lookup(port, eth->h_source);
475412ca155SHerbert Xu 		if (src && src->mode != MACVLAN_MODE_VEPA &&
476412ca155SHerbert Xu 		    src->mode != MACVLAN_MODE_BRIDGE) {
477729e72a1Sstephen hemminger 			/* forward to original port. */
478729e72a1Sstephen hemminger 			vlan = src;
479412ca155SHerbert Xu 			ret = macvlan_broadcast_one(skb, vlan, eth, 0) ?:
480baebdf48SSebastian Andrzej Siewior 			      __netif_rx(skb);
481d1dd9119Sjbaron@akamai.com 			handle_res = RX_HANDLER_CONSUMED;
482729e72a1Sstephen hemminger 			goto out;
483729e72a1Sstephen hemminger 		}
484729e72a1Sstephen hemminger 
4859c127a01SHerbert Xu 		hash = mc_hash(NULL, eth->h_dest);
486d45276e7SHerbert Xu 		if (test_bit(hash, port->bc_filter))
487260916dfSHerbert Xu 			macvlan_broadcast_enqueue(port, src, skb);
488d45276e7SHerbert Xu 		else if (test_bit(hash, port->mc_filter))
489d45276e7SHerbert Xu 			macvlan_multicast_rx(port, src, skb);
490412ca155SHerbert Xu 
4918a4eb573SJiri Pirko 		return RX_HANDLER_PASS;
492b863ceb7SPatrick McHardy 	}
493b863ceb7SPatrick McHardy 
494e16b8598SMartin Willi 	if (macvlan_forward_source(skb, port, eth->h_source)) {
495e16b8598SMartin Willi 		kfree_skb(skb);
496427f0c8cSJethro Beekman 		return RX_HANDLER_CONSUMED;
497e16b8598SMartin Willi 	}
49843c2d578SVlad Yasevich 	if (macvlan_passthru(port))
499233c7df0SJiri Pirko 		vlan = list_first_or_null_rcu(&port->vlans,
500233c7df0SJiri Pirko 					      struct macvlan_dev, list);
501eb06acdcSSridhar Samudrala 	else
502b863ceb7SPatrick McHardy 		vlan = macvlan_hash_lookup(port, eth->h_dest);
503dd6b9c2cSAlexander Duyck 	if (!vlan || vlan->mode == MACVLAN_MODE_SOURCE)
5048a4eb573SJiri Pirko 		return RX_HANDLER_PASS;
505b863ceb7SPatrick McHardy 
506b863ceb7SPatrick McHardy 	dev = vlan->dev;
507b863ceb7SPatrick McHardy 	if (unlikely(!(dev->flags & IFF_UP))) {
508b863ceb7SPatrick McHardy 		kfree_skb(skb);
5098a4eb573SJiri Pirko 		return RX_HANDLER_CONSUMED;
510b863ceb7SPatrick McHardy 	}
511a1e514c5SArnd Bergmann 	len = skb->len + ETH_HLEN;
512b863ceb7SPatrick McHardy 	skb = skb_share_check(skb, GFP_ATOMIC);
513d1dd9119Sjbaron@akamai.com 	if (!skb) {
514d1dd9119Sjbaron@akamai.com 		ret = NET_RX_DROP;
515d1dd9119Sjbaron@akamai.com 		handle_res = RX_HANDLER_CONSUMED;
516ba01877fSSridhar Samudrala 		goto out;
517d1dd9119Sjbaron@akamai.com 	}
518b863ceb7SPatrick McHardy 
519e639b8d8SSabrina Dubroca 	*pskb = skb;
520b863ceb7SPatrick McHardy 	skb->dev = dev;
521b863ceb7SPatrick McHardy 	skb->pkt_type = PACKET_HOST;
522b863ceb7SPatrick McHardy 
523d1dd9119Sjbaron@akamai.com 	ret = NET_RX_SUCCESS;
524d1dd9119Sjbaron@akamai.com 	handle_res = RX_HANDLER_ANOTHER;
525ba01877fSSridhar Samudrala out:
5264c979935Sjbaron@akamai.com 	macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, false);
527d1dd9119Sjbaron@akamai.com 	return handle_res;
528b863ceb7SPatrick McHardy }
529b863ceb7SPatrick McHardy 
macvlan_queue_xmit(struct sk_buff * skb,struct net_device * dev)530618e1b74SArnd Bergmann static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
531618e1b74SArnd Bergmann {
532618e1b74SArnd Bergmann 	const struct macvlan_dev *vlan = netdev_priv(dev);
533618e1b74SArnd Bergmann 	const struct macvlan_port *port = vlan->port;
534618e1b74SArnd Bergmann 	const struct macvlan_dev *dest;
535618e1b74SArnd Bergmann 
536618e1b74SArnd Bergmann 	if (vlan->mode == MACVLAN_MODE_BRIDGE) {
5371712b2ffSEric Dumazet 		const struct ethhdr *eth = skb_eth_hdr(skb);
538618e1b74SArnd Bergmann 
539618e1b74SArnd Bergmann 		/* send to other bridge ports directly */
540618e1b74SArnd Bergmann 		if (is_multicast_ether_addr(eth->h_dest)) {
5411712b2ffSEric Dumazet 			skb_reset_mac_header(skb);
542618e1b74SArnd Bergmann 			macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE);
543618e1b74SArnd Bergmann 			goto xmit_world;
544618e1b74SArnd Bergmann 		}
545618e1b74SArnd Bergmann 
546618e1b74SArnd Bergmann 		dest = macvlan_hash_lookup(port, eth->h_dest);
547618e1b74SArnd Bergmann 		if (dest && dest->mode == MACVLAN_MODE_BRIDGE) {
548a37dd333SDavid Ward 			/* send to lowerdev first for its network taps */
549cb2d0f3eSDavid Ward 			dev_forward_skb(vlan->lowerdev, skb);
550618e1b74SArnd Bergmann 
551618e1b74SArnd Bergmann 			return NET_XMIT_SUCCESS;
552618e1b74SArnd Bergmann 		}
553618e1b74SArnd Bergmann 	}
554618e1b74SArnd Bergmann xmit_world:
55559b9997bSDavid S. Miller 	skb->dev = vlan->lowerdev;
556eadec877SAlexander Duyck 	return dev_queue_xmit_accel(skb,
557eadec877SAlexander Duyck 				    netdev_get_sb_channel(dev) ? dev : NULL);
558618e1b74SArnd Bergmann }
559618e1b74SArnd Bergmann 
macvlan_netpoll_send_skb(struct macvlan_dev * vlan,struct sk_buff * skb)560688cea83Sdingtianhong static inline netdev_tx_t macvlan_netpoll_send_skb(struct macvlan_dev *vlan, struct sk_buff *skb)
561688cea83Sdingtianhong {
562688cea83Sdingtianhong #ifdef CONFIG_NET_POLL_CONTROLLER
563f78ed220SEric Dumazet 	return netpoll_send_skb(vlan->netpoll, skb);
564688cea83Sdingtianhong #else
565688cea83Sdingtianhong 	BUG();
566688cea83Sdingtianhong 	return NETDEV_TX_OK;
567f78ed220SEric Dumazet #endif
568688cea83Sdingtianhong }
569688cea83Sdingtianhong 
macvlan_start_xmit(struct sk_buff * skb,struct net_device * dev)5700db901bdSstephen hemminger static netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
571424efe9cSStephen Hemminger 				      struct net_device *dev)
572b863ceb7SPatrick McHardy {
57381d4e91cSAlexander Duyck 	struct macvlan_dev *vlan = netdev_priv(dev);
574b863ceb7SPatrick McHardy 	unsigned int len = skb->len;
575b863ceb7SPatrick McHardy 	int ret;
576688cea83Sdingtianhong 
577688cea83Sdingtianhong 	if (unlikely(netpoll_tx_running(dev)))
578688cea83Sdingtianhong 		return macvlan_netpoll_send_skb(vlan, skb);
579b863ceb7SPatrick McHardy 
580618e1b74SArnd Bergmann 	ret = macvlan_queue_xmit(skb, dev);
581a6cc0cfaSJohn Fastabend 
5822d6c9ffcSEric Dumazet 	if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
583cdf3e274SLi RongQing 		struct vlan_pcpu_stats *pcpu_stats;
5842c114553SEric Dumazet 
5858ffab51bSEric Dumazet 		pcpu_stats = this_cpu_ptr(vlan->pcpu_stats);
5868ffab51bSEric Dumazet 		u64_stats_update_begin(&pcpu_stats->syncp);
58709cca53cSEric Dumazet 		u64_stats_inc(&pcpu_stats->tx_packets);
58809cca53cSEric Dumazet 		u64_stats_add(&pcpu_stats->tx_bytes, len);
5898ffab51bSEric Dumazet 		u64_stats_update_end(&pcpu_stats->syncp);
5908ffab51bSEric Dumazet 	} else {
5918ffab51bSEric Dumazet 		this_cpu_inc(vlan->pcpu_stats->tx_dropped);
5928ffab51bSEric Dumazet 	}
593cbbef5e1SPatrick McHardy 	return ret;
594b863ceb7SPatrick McHardy }
595b863ceb7SPatrick McHardy 
macvlan_hard_header(struct sk_buff * skb,struct net_device * dev,unsigned short type,const void * daddr,const void * saddr,unsigned len)596b863ceb7SPatrick McHardy static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
5973b04dddeSStephen Hemminger 			       unsigned short type, const void *daddr,
5983b04dddeSStephen Hemminger 			       const void *saddr, unsigned len)
599b863ceb7SPatrick McHardy {
600b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan = netdev_priv(dev);
601b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
602b863ceb7SPatrick McHardy 
6030c4e8581SStephen Hemminger 	return dev_hard_header(skb, lowerdev, type, daddr,
604b863ceb7SPatrick McHardy 			       saddr ? : dev->dev_addr, len);
605b863ceb7SPatrick McHardy }
606b863ceb7SPatrick McHardy 
6073b04dddeSStephen Hemminger static const struct header_ops macvlan_hard_header_ops = {
6083b04dddeSStephen Hemminger 	.create  	= macvlan_hard_header,
6093b04dddeSStephen Hemminger 	.parse		= eth_header_parse,
6103b04dddeSStephen Hemminger 	.cache		= eth_header_cache,
6113b04dddeSStephen Hemminger 	.cache_update	= eth_header_cache_update,
6123b04dddeSStephen Hemminger };
6133b04dddeSStephen Hemminger 
macvlan_open(struct net_device * dev)614b863ceb7SPatrick McHardy static int macvlan_open(struct net_device *dev)
615b863ceb7SPatrick McHardy {
616b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
617b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
618b863ceb7SPatrick McHardy 	int err;
619b863ceb7SPatrick McHardy 
62043c2d578SVlad Yasevich 	if (macvlan_passthru(vlan->port)) {
62178738141SMichael S. Tsirkin 		if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) {
62278738141SMichael S. Tsirkin 			err = dev_set_promiscuity(lowerdev, 1);
62378738141SMichael S. Tsirkin 			if (err < 0)
62478738141SMichael S. Tsirkin 				goto out;
62578738141SMichael S. Tsirkin 		}
626eb06acdcSSridhar Samudrala 		goto hash_add;
627eb06acdcSSridhar Samudrala 	}
628eb06acdcSSridhar Samudrala 
62959f997b0SMatteo Croce 	err = -EADDRINUSE;
6307d775f63SAlexander Duyck 	if (macvlan_addr_busy(vlan->port, dev->dev_addr))
6317d775f63SAlexander Duyck 		goto out;
6327d775f63SAlexander Duyck 
6337d775f63SAlexander Duyck 	/* Attempt to populate accel_priv which is used to offload the L2
6347d775f63SAlexander Duyck 	 * forwarding requests for unicast packets.
6357d775f63SAlexander Duyck 	 */
63681d4e91cSAlexander Duyck 	if (lowerdev->features & NETIF_F_HW_L2FW_DOFFLOAD)
6377d775f63SAlexander Duyck 		vlan->accel_priv =
638a6cc0cfaSJohn Fastabend 		      lowerdev->netdev_ops->ndo_dfwd_add_station(lowerdev, dev);
639a6cc0cfaSJohn Fastabend 
64081d4e91cSAlexander Duyck 	/* If earlier attempt to offload failed, or accel_priv is not
64181d4e91cSAlexander Duyck 	 * populated we must add the unicast address to the lower device.
642a6cc0cfaSJohn Fastabend 	 */
64381d4e91cSAlexander Duyck 	if (IS_ERR_OR_NULL(vlan->accel_priv)) {
6447d775f63SAlexander Duyck 		vlan->accel_priv = NULL;
645a748ee24SJiri Pirko 		err = dev_uc_add(lowerdev, dev->dev_addr);
646b863ceb7SPatrick McHardy 		if (err < 0)
647b89fb7daSWang Chen 			goto out;
64881d4e91cSAlexander Duyck 	}
64981d4e91cSAlexander Duyck 
650b89fb7daSWang Chen 	if (dev->flags & IFF_ALLMULTI) {
651b89fb7daSWang Chen 		err = dev_set_allmulti(lowerdev, 1);
652b89fb7daSWang Chen 		if (err < 0)
653b89fb7daSWang Chen 			goto del_unicast;
654b89fb7daSWang Chen 	}
655eb06acdcSSridhar Samudrala 
656efdbd2b3SVlad Yasevich 	if (dev->flags & IFF_PROMISC) {
657efdbd2b3SVlad Yasevich 		err = dev_set_promiscuity(lowerdev, 1);
658efdbd2b3SVlad Yasevich 		if (err < 0)
659efdbd2b3SVlad Yasevich 			goto clear_multi;
660efdbd2b3SVlad Yasevich 	}
661efdbd2b3SVlad Yasevich 
662eb06acdcSSridhar Samudrala hash_add:
663f9ac30f0SEric Biederman 	macvlan_hash_add(vlan);
664b863ceb7SPatrick McHardy 	return 0;
665b89fb7daSWang Chen 
666efdbd2b3SVlad Yasevich clear_multi:
667c3891fa2SGao Feng 	if (dev->flags & IFF_ALLMULTI)
668efdbd2b3SVlad Yasevich 		dev_set_allmulti(lowerdev, -1);
669b89fb7daSWang Chen del_unicast:
6707d775f63SAlexander Duyck 	if (vlan->accel_priv) {
671a6cc0cfaSJohn Fastabend 		lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev,
6727d775f63SAlexander Duyck 							   vlan->accel_priv);
6737d775f63SAlexander Duyck 		vlan->accel_priv = NULL;
67481d4e91cSAlexander Duyck 	} else {
67581d4e91cSAlexander Duyck 		dev_uc_del(lowerdev, dev->dev_addr);
676a6cc0cfaSJohn Fastabend 	}
67781d4e91cSAlexander Duyck out:
678b89fb7daSWang Chen 	return err;
679b863ceb7SPatrick McHardy }
680b863ceb7SPatrick McHardy 
macvlan_stop(struct net_device * dev)681b863ceb7SPatrick McHardy static int macvlan_stop(struct net_device *dev)
682b863ceb7SPatrick McHardy {
683b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
684b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
685b863ceb7SPatrick McHardy 
6867d775f63SAlexander Duyck 	if (vlan->accel_priv) {
687a6cc0cfaSJohn Fastabend 		lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev,
6887d775f63SAlexander Duyck 							   vlan->accel_priv);
6897d775f63SAlexander Duyck 		vlan->accel_priv = NULL;
690a6cc0cfaSJohn Fastabend 	}
691a6cc0cfaSJohn Fastabend 
692df8ef8f3SJohn Fastabend 	dev_uc_unsync(lowerdev, dev);
693df8ef8f3SJohn Fastabend 	dev_mc_unsync(lowerdev, dev);
694df8ef8f3SJohn Fastabend 
69543c2d578SVlad Yasevich 	if (macvlan_passthru(vlan->port)) {
696df8ef8f3SJohn Fastabend 		if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC))
697eb06acdcSSridhar Samudrala 			dev_set_promiscuity(lowerdev, -1);
698eb06acdcSSridhar Samudrala 		goto hash_del;
699eb06acdcSSridhar Samudrala 	}
700eb06acdcSSridhar Samudrala 
701b863ceb7SPatrick McHardy 	if (dev->flags & IFF_ALLMULTI)
702b863ceb7SPatrick McHardy 		dev_set_allmulti(lowerdev, -1);
703b863ceb7SPatrick McHardy 
704efdbd2b3SVlad Yasevich 	if (dev->flags & IFF_PROMISC)
705efdbd2b3SVlad Yasevich 		dev_set_promiscuity(lowerdev, -1);
706efdbd2b3SVlad Yasevich 
707a748ee24SJiri Pirko 	dev_uc_del(lowerdev, dev->dev_addr);
708b863ceb7SPatrick McHardy 
709eb06acdcSSridhar Samudrala hash_del:
710449f4544SEric Dumazet 	macvlan_hash_del(vlan, !dev->dismantle);
711b863ceb7SPatrick McHardy 	return 0;
712b863ceb7SPatrick McHardy }
713b863ceb7SPatrick McHardy 
macvlan_sync_address(struct net_device * dev,const unsigned char * addr)7148bc7823eSJakub Kicinski static int macvlan_sync_address(struct net_device *dev,
7158bc7823eSJakub Kicinski 				const unsigned char *addr)
716ad5d20a6SPatrick McHardy {
717ad5d20a6SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
718ad5d20a6SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
71918c8c54dSVlad Yasevich 	struct macvlan_port *port = vlan->port;
720ad5d20a6SPatrick McHardy 	int err;
721ad5d20a6SPatrick McHardy 
722f9ac30f0SEric Biederman 	if (!(dev->flags & IFF_UP)) {
723f9ac30f0SEric Biederman 		/* Just copy in the new address */
724e35b8d7dSJakub Kicinski 		eth_hw_addr_set(dev, addr);
725f9ac30f0SEric Biederman 	} else {
726f9ac30f0SEric Biederman 		/* Rehash and update the device filters */
727e289fd28Sdingtianhong 		if (macvlan_addr_busy(vlan->port, addr))
72859f997b0SMatteo Croce 			return -EADDRINUSE;
729ad5d20a6SPatrick McHardy 
73018c8c54dSVlad Yasevich 		if (!macvlan_passthru(port)) {
731e289fd28Sdingtianhong 			err = dev_uc_add(lowerdev, addr);
732ccffad25SJiri Pirko 			if (err)
733ad5d20a6SPatrick McHardy 				return err;
734f9ac30f0SEric Biederman 
735a748ee24SJiri Pirko 			dev_uc_del(lowerdev, dev->dev_addr);
736e289fd28Sdingtianhong 		}
737ad5d20a6SPatrick McHardy 
738e289fd28Sdingtianhong 		macvlan_hash_change_addr(vlan, addr);
739f9ac30f0SEric Biederman 	}
74018c8c54dSVlad Yasevich 	if (macvlan_passthru(port) && !macvlan_addr_change(port)) {
74118c8c54dSVlad Yasevich 		/* Since addr_change isn't set, we are here due to lower
74218c8c54dSVlad Yasevich 		 * device change.  Save the lower-dev address so we can
74318c8c54dSVlad Yasevich 		 * restore it later.
74418c8c54dSVlad Yasevich 		 */
74518c8c54dSVlad Yasevich 		ether_addr_copy(vlan->port->perm_addr,
74618c8c54dSVlad Yasevich 				lowerdev->dev_addr);
74718c8c54dSVlad Yasevich 	}
74818c8c54dSVlad Yasevich 	macvlan_clear_addr_change(port);
749ad5d20a6SPatrick McHardy 	return 0;
750ad5d20a6SPatrick McHardy }
751ad5d20a6SPatrick McHardy 
macvlan_set_mac_address(struct net_device * dev,void * p)752e289fd28Sdingtianhong static int macvlan_set_mac_address(struct net_device *dev, void *p)
753e289fd28Sdingtianhong {
754e289fd28Sdingtianhong 	struct macvlan_dev *vlan = netdev_priv(dev);
755e289fd28Sdingtianhong 	struct sockaddr *addr = p;
756e289fd28Sdingtianhong 
757e289fd28Sdingtianhong 	if (!is_valid_ether_addr(addr->sa_data))
758e289fd28Sdingtianhong 		return -EADDRNOTAVAIL;
759e289fd28Sdingtianhong 
760e26f43faSVlad Yasevich 	/* If the addresses are the same, this is a no-op */
761e26f43faSVlad Yasevich 	if (ether_addr_equal(dev->dev_addr, addr->sa_data))
762e26f43faSVlad Yasevich 		return 0;
763e26f43faSVlad Yasevich 
764e289fd28Sdingtianhong 	if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
76518c8c54dSVlad Yasevich 		macvlan_set_addr_change(vlan->port);
7663a37a963SPetr Machata 		return dev_set_mac_address(vlan->lowerdev, addr, NULL);
767e289fd28Sdingtianhong 	}
768e289fd28Sdingtianhong 
76959f997b0SMatteo Croce 	if (macvlan_addr_busy(vlan->port, addr->sa_data))
77059f997b0SMatteo Croce 		return -EADDRINUSE;
77159f997b0SMatteo Croce 
772e289fd28Sdingtianhong 	return macvlan_sync_address(dev, addr->sa_data);
773e289fd28Sdingtianhong }
774e289fd28Sdingtianhong 
macvlan_change_rx_flags(struct net_device * dev,int change)775b863ceb7SPatrick McHardy static void macvlan_change_rx_flags(struct net_device *dev, int change)
776b863ceb7SPatrick McHardy {
777b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
778b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
779b863ceb7SPatrick McHardy 
780bbeb0eadSPeter Christensen 	if (dev->flags & IFF_UP) {
781b863ceb7SPatrick McHardy 		if (change & IFF_ALLMULTI)
782b863ceb7SPatrick McHardy 			dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
783*7bd48f8aSVlad Buslov 		if (!macvlan_passthru(vlan->port) && change & IFF_PROMISC)
784efdbd2b3SVlad Yasevich 			dev_set_promiscuity(lowerdev,
785efdbd2b3SVlad Yasevich 					    dev->flags & IFF_PROMISC ? 1 : -1);
786efdbd2b3SVlad Yasevich 
787b863ceb7SPatrick McHardy 	}
788bbeb0eadSPeter Christensen }
789b863ceb7SPatrick McHardy 
macvlan_compute_filter(unsigned long * mc_filter,struct net_device * dev,struct macvlan_dev * vlan,int cutoff)7909c127a01SHerbert Xu static void macvlan_compute_filter(unsigned long *mc_filter,
7919c127a01SHerbert Xu 				   struct net_device *dev,
792d45276e7SHerbert Xu 				   struct macvlan_dev *vlan, int cutoff)
793b863ceb7SPatrick McHardy {
794cd431e73SEric Dumazet 	if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
7959c127a01SHerbert Xu 		bitmap_fill(mc_filter, MACVLAN_MC_FILTER_SZ);
796cd431e73SEric Dumazet 	} else {
797cd431e73SEric Dumazet 		DECLARE_BITMAP(filter, MACVLAN_MC_FILTER_SZ);
798d45276e7SHerbert Xu 		struct netdev_hw_addr *ha;
799cd431e73SEric Dumazet 
800cd431e73SEric Dumazet 		bitmap_zero(filter, MACVLAN_MC_FILTER_SZ);
801cd431e73SEric Dumazet 		netdev_for_each_mc_addr(ha, dev) {
802ae63ad9bSHerbert Xu 			if (!vlan && ha->synced <= cutoff)
803d45276e7SHerbert Xu 				continue;
804d45276e7SHerbert Xu 
8053807ff58SEric Dumazet 			__set_bit(mc_hash(vlan, ha->addr), filter);
806cd431e73SEric Dumazet 		}
807d5270430SEric Dumazet 
8083807ff58SEric Dumazet 		__set_bit(mc_hash(vlan, dev->broadcast), filter);
809d5270430SEric Dumazet 
8109c127a01SHerbert Xu 		bitmap_copy(mc_filter, filter, MACVLAN_MC_FILTER_SZ);
811cd431e73SEric Dumazet 	}
8129c127a01SHerbert Xu }
8139c127a01SHerbert Xu 
macvlan_recompute_bc_filter(struct macvlan_dev * vlan)814954d1fa1SHerbert Xu static void macvlan_recompute_bc_filter(struct macvlan_dev *vlan)
815954d1fa1SHerbert Xu {
816ae63ad9bSHerbert Xu 	if (vlan->port->bc_cutoff < 0) {
817ae63ad9bSHerbert Xu 		bitmap_zero(vlan->port->bc_filter, MACVLAN_MC_FILTER_SZ);
818ae63ad9bSHerbert Xu 		return;
819ae63ad9bSHerbert Xu 	}
820ae63ad9bSHerbert Xu 
821954d1fa1SHerbert Xu 	macvlan_compute_filter(vlan->port->bc_filter, vlan->lowerdev, NULL,
822954d1fa1SHerbert Xu 			       vlan->port->bc_cutoff);
823954d1fa1SHerbert Xu }
824954d1fa1SHerbert Xu 
macvlan_set_mac_lists(struct net_device * dev)8259c127a01SHerbert Xu static void macvlan_set_mac_lists(struct net_device *dev)
8269c127a01SHerbert Xu {
8279c127a01SHerbert Xu 	struct macvlan_dev *vlan = netdev_priv(dev);
8289c127a01SHerbert Xu 
829d45276e7SHerbert Xu 	macvlan_compute_filter(vlan->mc_filter, dev, vlan, 0);
8309c127a01SHerbert Xu 
831df8ef8f3SJohn Fastabend 	dev_uc_sync(vlan->lowerdev, dev);
832b863ceb7SPatrick McHardy 	dev_mc_sync(vlan->lowerdev, dev);
8339c127a01SHerbert Xu 
8349c127a01SHerbert Xu 	/* This is slightly inaccurate as we're including the subscription
8359c127a01SHerbert Xu 	 * list of vlan->lowerdev too.
8369c127a01SHerbert Xu 	 *
8379c127a01SHerbert Xu 	 * Bug alert: This only works if everyone has the same broadcast
8389c127a01SHerbert Xu 	 * address as lowerdev.  As soon as someone changes theirs this
8399c127a01SHerbert Xu 	 * will break.
8409c127a01SHerbert Xu 	 *
8419c127a01SHerbert Xu 	 * However, this is already broken as when you change your broadcast
8429c127a01SHerbert Xu 	 * address we don't get called.
8439c127a01SHerbert Xu 	 *
8449c127a01SHerbert Xu 	 * The solution is to maintain a list of broadcast addresses like
8459c127a01SHerbert Xu 	 * we do for uc/mc, if you care.
8469c127a01SHerbert Xu 	 */
847d45276e7SHerbert Xu 	macvlan_compute_filter(vlan->port->mc_filter, vlan->lowerdev, NULL,
848d45276e7SHerbert Xu 			       0);
849954d1fa1SHerbert Xu 	macvlan_recompute_bc_filter(vlan);
850954d1fa1SHerbert Xu }
851954d1fa1SHerbert Xu 
update_port_bc_cutoff(struct macvlan_dev * vlan,int cutoff)852954d1fa1SHerbert Xu static void update_port_bc_cutoff(struct macvlan_dev *vlan, int cutoff)
853954d1fa1SHerbert Xu {
854954d1fa1SHerbert Xu 	if (vlan->port->bc_cutoff == cutoff)
855954d1fa1SHerbert Xu 		return;
856954d1fa1SHerbert Xu 
857954d1fa1SHerbert Xu 	vlan->port->bc_cutoff = cutoff;
858954d1fa1SHerbert Xu 	macvlan_recompute_bc_filter(vlan);
859b863ceb7SPatrick McHardy }
860b863ceb7SPatrick McHardy 
macvlan_change_mtu(struct net_device * dev,int new_mtu)861b863ceb7SPatrick McHardy static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
862b863ceb7SPatrick McHardy {
863b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
864b863ceb7SPatrick McHardy 
86591572088SJarod Wilson 	if (vlan->lowerdev->mtu < new_mtu)
866b863ceb7SPatrick McHardy 		return -EINVAL;
867b863ceb7SPatrick McHardy 	dev->mtu = new_mtu;
868b863ceb7SPatrick McHardy 	return 0;
869b863ceb7SPatrick McHardy }
870b863ceb7SPatrick McHardy 
macvlan_hwtstamp_get(struct net_device * dev,struct kernel_hwtstamp_config * cfg)8710bca3f7fSMaxim Georgiev static int macvlan_hwtstamp_get(struct net_device *dev,
8720bca3f7fSMaxim Georgiev 				struct kernel_hwtstamp_config *cfg)
873254c0a2bSHangbin Liu {
874254c0a2bSHangbin Liu 	struct net_device *real_dev = macvlan_dev_real_dev(dev);
875254c0a2bSHangbin Liu 
8760bca3f7fSMaxim Georgiev 	return generic_hwtstamp_get_lower(real_dev, cfg);
877254c0a2bSHangbin Liu }
878254c0a2bSHangbin Liu 
macvlan_hwtstamp_set(struct net_device * dev,struct kernel_hwtstamp_config * cfg,struct netlink_ext_ack * extack)8790bca3f7fSMaxim Georgiev static int macvlan_hwtstamp_set(struct net_device *dev,
8800bca3f7fSMaxim Georgiev 				struct kernel_hwtstamp_config *cfg,
8810bca3f7fSMaxim Georgiev 				struct netlink_ext_ack *extack)
8820bca3f7fSMaxim Georgiev {
8830bca3f7fSMaxim Georgiev 	struct net_device *real_dev = macvlan_dev_real_dev(dev);
884254c0a2bSHangbin Liu 
8850bca3f7fSMaxim Georgiev 	if (!net_eq(dev_net(dev), &init_net))
8860bca3f7fSMaxim Georgiev 		return -EOPNOTSUPP;
8870bca3f7fSMaxim Georgiev 
8880bca3f7fSMaxim Georgiev 	return generic_hwtstamp_set_lower(real_dev, cfg, extack);
889254c0a2bSHangbin Liu }
890254c0a2bSHangbin Liu 
891b863ceb7SPatrick McHardy /*
892b863ceb7SPatrick McHardy  * macvlan network devices have devices nesting below it and are a special
893b863ceb7SPatrick McHardy  * "super class" of normal network devices; split their locks off into a
894b863ceb7SPatrick McHardy  * separate class since they always nest.
895b863ceb7SPatrick McHardy  */
896845e0ebbSCong Wang static struct lock_class_key macvlan_netdev_addr_lock_key;
897845e0ebbSCong Wang 
89870957eaeSVlad Yasevich #define ALWAYS_ON_OFFLOADS \
89970957eaeSVlad Yasevich 	(NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE | \
900f21e5077SDimitris Michailidis 	 NETIF_F_GSO_ROBUST | NETIF_F_GSO_ENCAP_ALL)
9018b4703e9SVlad Yasevich 
90270957eaeSVlad Yasevich #define ALWAYS_ON_FEATURES (ALWAYS_ON_OFFLOADS | NETIF_F_LLTX)
90370957eaeSVlad Yasevich 
904b863ceb7SPatrick McHardy #define MACVLAN_FEATURES \
905a188222bSTom Herbert 	(NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
906fb652fdfSDavid S. Miller 	 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_LRO | \
9078d13e670SJohn Fastabend 	 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
90828d2b136SPatrick McHardy 	 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
909b863ceb7SPatrick McHardy 
910b863ceb7SPatrick McHardy #define MACVLAN_STATE_MASK \
911b863ceb7SPatrick McHardy 	((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
912b863ceb7SPatrick McHardy 
macvlan_set_lockdep_class(struct net_device * dev)913845e0ebbSCong Wang static void macvlan_set_lockdep_class(struct net_device *dev)
914845e0ebbSCong Wang {
915845e0ebbSCong Wang 	netdev_lockdep_set_classes(dev);
916be74294fSCong Wang 	lockdep_set_class(&dev->addr_list_lock,
917be74294fSCong Wang 			  &macvlan_netdev_addr_lock_key);
918845e0ebbSCong Wang }
919845e0ebbSCong Wang 
macvlan_init(struct net_device * dev)920b863ceb7SPatrick McHardy static int macvlan_init(struct net_device *dev)
921b863ceb7SPatrick McHardy {
922b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
923291ac684SZiyang Xuan 	struct net_device *lowerdev = vlan->lowerdev;
92430837960SFrancesco Ruggeri 	struct macvlan_port *port = vlan->port;
925b863ceb7SPatrick McHardy 
926b863ceb7SPatrick McHardy 	dev->state		= (dev->state & ~MACVLAN_STATE_MASK) |
927b863ceb7SPatrick McHardy 				  (lowerdev->state & MACVLAN_STATE_MASK);
928b863ceb7SPatrick McHardy 	dev->features 		= lowerdev->features & MACVLAN_FEATURES;
9298b4703e9SVlad Yasevich 	dev->features		|= ALWAYS_ON_FEATURES;
93062dbe830SMichal Kubeček 	dev->hw_features	|= NETIF_F_LRO;
931081e83a7SVlad Yasevich 	dev->vlan_features	= lowerdev->vlan_features & MACVLAN_FEATURES;
93270957eaeSVlad Yasevich 	dev->vlan_features	|= ALWAYS_ON_OFFLOADS;
933f21e5077SDimitris Michailidis 	dev->hw_enc_features    |= dev->features;
9346df6398fSJakub Kicinski 	netif_inherit_tso_max(dev, lowerdev);
935ef5c8996Ssg.tweak@gmail.com 	dev->hard_header_len	= lowerdev->hard_header_len;
936845e0ebbSCong Wang 	macvlan_set_lockdep_class(dev);
9371a33e10eSCong Wang 
9381c213bd2SWANG Cong 	vlan->pcpu_stats = netdev_alloc_pcpu_stats(struct vlan_pcpu_stats);
9398ffab51bSEric Dumazet 	if (!vlan->pcpu_stats)
940fccaf710SEric Dumazet 		return -ENOMEM;
941fccaf710SEric Dumazet 
94230837960SFrancesco Ruggeri 	port->count += 1;
94330837960SFrancesco Ruggeri 
944291ac684SZiyang Xuan 	/* Get macvlan's reference to lowerdev */
945d62607c3SJakub Kicinski 	netdev_hold(lowerdev, &vlan->dev_tracker, GFP_KERNEL);
946291ac684SZiyang Xuan 
947b863ceb7SPatrick McHardy 	return 0;
948b863ceb7SPatrick McHardy }
949b863ceb7SPatrick McHardy 
macvlan_uninit(struct net_device * dev)950fccaf710SEric Dumazet static void macvlan_uninit(struct net_device *dev)
951fccaf710SEric Dumazet {
952fccaf710SEric Dumazet 	struct macvlan_dev *vlan = netdev_priv(dev);
953d5cd9244SEric W. Biederman 	struct macvlan_port *port = vlan->port;
954fccaf710SEric Dumazet 
9558ffab51bSEric Dumazet 	free_percpu(vlan->pcpu_stats);
956d5cd9244SEric W. Biederman 
95779cf79abSMichael Braun 	macvlan_flush_sources(port, vlan);
9585e3c516bSDavid S. Miller 	port->count -= 1;
9595e3c516bSDavid S. Miller 	if (!port->count)
960d5cd9244SEric W. Biederman 		macvlan_port_destroy(port->dev);
961fccaf710SEric Dumazet }
962fccaf710SEric Dumazet 
macvlan_dev_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats)963bc1f4470Sstephen hemminger static void macvlan_dev_get_stats64(struct net_device *dev,
96428172739SEric Dumazet 				    struct rtnl_link_stats64 *stats)
965fccaf710SEric Dumazet {
966fccaf710SEric Dumazet 	struct macvlan_dev *vlan = netdev_priv(dev);
967fccaf710SEric Dumazet 
9688ffab51bSEric Dumazet 	if (vlan->pcpu_stats) {
969cdf3e274SLi RongQing 		struct vlan_pcpu_stats *p;
9708ffab51bSEric Dumazet 		u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
9718ffab51bSEric Dumazet 		u32 rx_errors = 0, tx_dropped = 0;
972bc66154eSEric Dumazet 		unsigned int start;
973fccaf710SEric Dumazet 		int i;
974fccaf710SEric Dumazet 
975fccaf710SEric Dumazet 		for_each_possible_cpu(i) {
9768ffab51bSEric Dumazet 			p = per_cpu_ptr(vlan->pcpu_stats, i);
977bc66154eSEric Dumazet 			do {
978068c38adSThomas Gleixner 				start = u64_stats_fetch_begin(&p->syncp);
97909cca53cSEric Dumazet 				rx_packets	= u64_stats_read(&p->rx_packets);
98009cca53cSEric Dumazet 				rx_bytes	= u64_stats_read(&p->rx_bytes);
98109cca53cSEric Dumazet 				rx_multicast	= u64_stats_read(&p->rx_multicast);
98209cca53cSEric Dumazet 				tx_packets	= u64_stats_read(&p->tx_packets);
98309cca53cSEric Dumazet 				tx_bytes	= u64_stats_read(&p->tx_bytes);
984068c38adSThomas Gleixner 			} while (u64_stats_fetch_retry(&p->syncp, start));
9858ffab51bSEric Dumazet 
9868ffab51bSEric Dumazet 			stats->rx_packets	+= rx_packets;
9878ffab51bSEric Dumazet 			stats->rx_bytes		+= rx_bytes;
9888ffab51bSEric Dumazet 			stats->multicast	+= rx_multicast;
9898ffab51bSEric Dumazet 			stats->tx_packets	+= tx_packets;
9908ffab51bSEric Dumazet 			stats->tx_bytes		+= tx_bytes;
9918ffab51bSEric Dumazet 			/* rx_errors & tx_dropped are u32, updated
9928ffab51bSEric Dumazet 			 * without syncp protection.
9938ffab51bSEric Dumazet 			 */
99409cca53cSEric Dumazet 			rx_errors	+= READ_ONCE(p->rx_errors);
99509cca53cSEric Dumazet 			tx_dropped	+= READ_ONCE(p->tx_dropped);
996fccaf710SEric Dumazet 		}
9978ffab51bSEric Dumazet 		stats->rx_errors	= rx_errors;
9988ffab51bSEric Dumazet 		stats->rx_dropped	= rx_errors;
9998ffab51bSEric Dumazet 		stats->tx_dropped	= tx_dropped;
1000fccaf710SEric Dumazet 	}
1001fccaf710SEric Dumazet }
1002fccaf710SEric Dumazet 
macvlan_vlan_rx_add_vid(struct net_device * dev,__be16 proto,u16 vid)10038e586137SJiri Pirko static int macvlan_vlan_rx_add_vid(struct net_device *dev,
100480d5c368SPatrick McHardy 				   __be16 proto, u16 vid)
10058d13e670SJohn Fastabend {
10068d13e670SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
10078d13e670SJohn Fastabend 	struct net_device *lowerdev = vlan->lowerdev;
10088d13e670SJohn Fastabend 
100980d5c368SPatrick McHardy 	return vlan_vid_add(lowerdev, proto, vid);
10108d13e670SJohn Fastabend }
10118d13e670SJohn Fastabend 
macvlan_vlan_rx_kill_vid(struct net_device * dev,__be16 proto,u16 vid)10128e586137SJiri Pirko static int macvlan_vlan_rx_kill_vid(struct net_device *dev,
101380d5c368SPatrick McHardy 				    __be16 proto, u16 vid)
10148d13e670SJohn Fastabend {
10158d13e670SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
10168d13e670SJohn Fastabend 	struct net_device *lowerdev = vlan->lowerdev;
10178d13e670SJohn Fastabend 
101880d5c368SPatrick McHardy 	vlan_vid_del(lowerdev, proto, vid);
10198e586137SJiri Pirko 	return 0;
10208d13e670SJohn Fastabend }
10218d13e670SJohn Fastabend 
macvlan_fdb_add(struct ndmsg * ndm,struct nlattr * tb[],struct net_device * dev,const unsigned char * addr,u16 vid,u16 flags,struct netlink_ext_ack * extack)1022edc7d573Sstephen hemminger static int macvlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1023df8ef8f3SJohn Fastabend 			   struct net_device *dev,
1024f6f6424bSJiri Pirko 			   const unsigned char *addr, u16 vid,
102587b0984eSPetr Machata 			   u16 flags,
102687b0984eSPetr Machata 			   struct netlink_ext_ack *extack)
1027df8ef8f3SJohn Fastabend {
1028df8ef8f3SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
1029df8ef8f3SJohn Fastabend 	int err = -EINVAL;
1030df8ef8f3SJohn Fastabend 
10318a50f11cSVlad Yasevich 	/* Support unicast filter only on passthru devices.
10328a50f11cSVlad Yasevich 	 * Multicast filter should be allowed on all devices.
10338a50f11cSVlad Yasevich 	 */
103443c2d578SVlad Yasevich 	if (!macvlan_passthru(vlan->port) && is_unicast_ether_addr(addr))
1035df8ef8f3SJohn Fastabend 		return -EOPNOTSUPP;
1036df8ef8f3SJohn Fastabend 
1037ab2cfbb2SThomas Richter 	if (flags & NLM_F_REPLACE)
1038ab2cfbb2SThomas Richter 		return -EOPNOTSUPP;
1039ab2cfbb2SThomas Richter 
1040df8ef8f3SJohn Fastabend 	if (is_unicast_ether_addr(addr))
1041df8ef8f3SJohn Fastabend 		err = dev_uc_add_excl(dev, addr);
1042df8ef8f3SJohn Fastabend 	else if (is_multicast_ether_addr(addr))
1043df8ef8f3SJohn Fastabend 		err = dev_mc_add_excl(dev, addr);
1044df8ef8f3SJohn Fastabend 
1045df8ef8f3SJohn Fastabend 	return err;
1046df8ef8f3SJohn Fastabend }
1047df8ef8f3SJohn Fastabend 
macvlan_fdb_del(struct ndmsg * ndm,struct nlattr * tb[],struct net_device * dev,const unsigned char * addr,u16 vid,struct netlink_ext_ack * extack)10481690be63SVlad Yasevich static int macvlan_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
1049df8ef8f3SJohn Fastabend 			   struct net_device *dev,
1050ca4567f1SAlaa Mohamed 			   const unsigned char *addr, u16 vid,
1051ca4567f1SAlaa Mohamed 			   struct netlink_ext_ack *extack)
1052df8ef8f3SJohn Fastabend {
1053df8ef8f3SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
1054df8ef8f3SJohn Fastabend 	int err = -EINVAL;
1055df8ef8f3SJohn Fastabend 
10568a50f11cSVlad Yasevich 	/* Support unicast filter only on passthru devices.
10578a50f11cSVlad Yasevich 	 * Multicast filter should be allowed on all devices.
10588a50f11cSVlad Yasevich 	 */
105943c2d578SVlad Yasevich 	if (!macvlan_passthru(vlan->port) && is_unicast_ether_addr(addr))
1060df8ef8f3SJohn Fastabend 		return -EOPNOTSUPP;
1061df8ef8f3SJohn Fastabend 
1062df8ef8f3SJohn Fastabend 	if (is_unicast_ether_addr(addr))
1063df8ef8f3SJohn Fastabend 		err = dev_uc_del(dev, addr);
1064df8ef8f3SJohn Fastabend 	else if (is_multicast_ether_addr(addr))
1065df8ef8f3SJohn Fastabend 		err = dev_mc_del(dev, addr);
1066df8ef8f3SJohn Fastabend 
1067df8ef8f3SJohn Fastabend 	return err;
1068df8ef8f3SJohn Fastabend }
1069df8ef8f3SJohn Fastabend 
macvlan_ethtool_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * drvinfo)1070b863ceb7SPatrick McHardy static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
1071b863ceb7SPatrick McHardy 					struct ethtool_drvinfo *drvinfo)
1072b863ceb7SPatrick McHardy {
1073fb3ceec1SWolfram Sang 	strscpy(drvinfo->driver, "macvlan", sizeof(drvinfo->driver));
1074fb3ceec1SWolfram Sang 	strscpy(drvinfo->version, "0.1", sizeof(drvinfo->version));
1075b863ceb7SPatrick McHardy }
1076b863ceb7SPatrick McHardy 
macvlan_ethtool_get_link_ksettings(struct net_device * dev,struct ethtool_link_ksettings * cmd)107785f95819SDavid Decotigny static int macvlan_ethtool_get_link_ksettings(struct net_device *dev,
107885f95819SDavid Decotigny 					      struct ethtool_link_ksettings *cmd)
10799edb8bb6SStephen Hemminger {
10809edb8bb6SStephen Hemminger 	const struct macvlan_dev *vlan = netdev_priv(dev);
10814bc71cb9SJiri Pirko 
108285f95819SDavid Decotigny 	return __ethtool_get_link_ksettings(vlan->lowerdev, cmd);
10839edb8bb6SStephen Hemminger }
10849edb8bb6SStephen Hemminger 
macvlan_ethtool_get_ts_info(struct net_device * dev,struct ethtool_ts_info * info)1085254c0a2bSHangbin Liu static int macvlan_ethtool_get_ts_info(struct net_device *dev,
1086254c0a2bSHangbin Liu 				       struct ethtool_ts_info *info)
1087254c0a2bSHangbin Liu {
1088254c0a2bSHangbin Liu 	struct net_device *real_dev = macvlan_dev_real_dev(dev);
1089254c0a2bSHangbin Liu 	const struct ethtool_ops *ops = real_dev->ethtool_ops;
1090254c0a2bSHangbin Liu 	struct phy_device *phydev = real_dev->phydev;
1091254c0a2bSHangbin Liu 
1092d25de984SRichard Cochran 	if (phy_has_tsinfo(phydev)) {
1093d25de984SRichard Cochran 		return phy_ts_info(phydev, info);
1094254c0a2bSHangbin Liu 	} else if (ops->get_ts_info) {
1095254c0a2bSHangbin Liu 		return ops->get_ts_info(real_dev, info);
1096254c0a2bSHangbin Liu 	} else {
1097254c0a2bSHangbin Liu 		info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE |
1098254c0a2bSHangbin Liu 			SOF_TIMESTAMPING_SOFTWARE;
1099254c0a2bSHangbin Liu 		info->phc_index = -1;
1100254c0a2bSHangbin Liu 	}
1101254c0a2bSHangbin Liu 
1102254c0a2bSHangbin Liu 	return 0;
1103254c0a2bSHangbin Liu }
1104254c0a2bSHangbin Liu 
macvlan_fix_features(struct net_device * dev,netdev_features_t features)11052be5c767SVlad Yasevich static netdev_features_t macvlan_fix_features(struct net_device *dev,
11062be5c767SVlad Yasevich 					      netdev_features_t features)
11072be5c767SVlad Yasevich {
11082be5c767SVlad Yasevich 	struct macvlan_dev *vlan = netdev_priv(dev);
110962dbe830SMichal Kubeček 	netdev_features_t lowerdev_features = vlan->lowerdev->features;
1110797f87f8SFlorian Westphal 	netdev_features_t mask;
11112be5c767SVlad Yasevich 
1112797f87f8SFlorian Westphal 	features |= NETIF_F_ALL_FOR_ALL;
1113797f87f8SFlorian Westphal 	features &= (vlan->set_features | ~MACVLAN_FEATURES);
1114797f87f8SFlorian Westphal 	mask = features;
1115797f87f8SFlorian Westphal 
111662dbe830SMichal Kubeček 	lowerdev_features &= (features | ~NETIF_F_LRO);
111762dbe830SMichal Kubeček 	features = netdev_increment_features(lowerdev_features, features, mask);
11188b4703e9SVlad Yasevich 	features |= ALWAYS_ON_FEATURES;
111913fbcc8dSShannon Nelson 	features &= (ALWAYS_ON_FEATURES | MACVLAN_FEATURES);
1120797f87f8SFlorian Westphal 
1121797f87f8SFlorian Westphal 	return features;
11222be5c767SVlad Yasevich }
11232be5c767SVlad Yasevich 
1124688cea83Sdingtianhong #ifdef CONFIG_NET_POLL_CONTROLLER
macvlan_dev_poll_controller(struct net_device * dev)1125688cea83Sdingtianhong static void macvlan_dev_poll_controller(struct net_device *dev)
1126688cea83Sdingtianhong {
1127688cea83Sdingtianhong 	return;
1128688cea83Sdingtianhong }
1129688cea83Sdingtianhong 
macvlan_dev_netpoll_setup(struct net_device * dev,struct netpoll_info * npinfo)1130688cea83Sdingtianhong static int macvlan_dev_netpoll_setup(struct net_device *dev, struct netpoll_info *npinfo)
1131688cea83Sdingtianhong {
1132688cea83Sdingtianhong 	struct macvlan_dev *vlan = netdev_priv(dev);
1133688cea83Sdingtianhong 	struct net_device *real_dev = vlan->lowerdev;
1134688cea83Sdingtianhong 	struct netpoll *netpoll;
1135419a38ceSMenglong Dong 	int err;
1136688cea83Sdingtianhong 
1137688cea83Sdingtianhong 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
1138688cea83Sdingtianhong 	err = -ENOMEM;
1139688cea83Sdingtianhong 	if (!netpoll)
1140688cea83Sdingtianhong 		goto out;
1141688cea83Sdingtianhong 
1142688cea83Sdingtianhong 	err = __netpoll_setup(netpoll, real_dev);
1143688cea83Sdingtianhong 	if (err) {
1144688cea83Sdingtianhong 		kfree(netpoll);
1145688cea83Sdingtianhong 		goto out;
1146688cea83Sdingtianhong 	}
1147688cea83Sdingtianhong 
1148688cea83Sdingtianhong 	vlan->netpoll = netpoll;
1149688cea83Sdingtianhong 
1150688cea83Sdingtianhong out:
1151688cea83Sdingtianhong 	return err;
1152688cea83Sdingtianhong }
1153688cea83Sdingtianhong 
macvlan_dev_netpoll_cleanup(struct net_device * dev)1154688cea83Sdingtianhong static void macvlan_dev_netpoll_cleanup(struct net_device *dev)
1155688cea83Sdingtianhong {
1156688cea83Sdingtianhong 	struct macvlan_dev *vlan = netdev_priv(dev);
1157688cea83Sdingtianhong 	struct netpoll *netpoll = vlan->netpoll;
1158688cea83Sdingtianhong 
1159688cea83Sdingtianhong 	if (!netpoll)
1160688cea83Sdingtianhong 		return;
1161688cea83Sdingtianhong 
1162688cea83Sdingtianhong 	vlan->netpoll = NULL;
1163688cea83Sdingtianhong 
1164c9fbd71fSDebabrata Banerjee 	__netpoll_free(netpoll);
1165688cea83Sdingtianhong }
1166688cea83Sdingtianhong #endif	/* CONFIG_NET_POLL_CONTROLLER */
1167688cea83Sdingtianhong 
macvlan_dev_get_iflink(const struct net_device * dev)1168ef5fa6bcSNicolas Dichtel static int macvlan_dev_get_iflink(const struct net_device *dev)
1169ef5fa6bcSNicolas Dichtel {
1170ef5fa6bcSNicolas Dichtel 	struct macvlan_dev *vlan = netdev_priv(dev);
1171ef5fa6bcSNicolas Dichtel 
1172ef5fa6bcSNicolas Dichtel 	return vlan->lowerdev->ifindex;
1173ef5fa6bcSNicolas Dichtel }
1174ef5fa6bcSNicolas Dichtel 
1175b863ceb7SPatrick McHardy static const struct ethtool_ops macvlan_ethtool_ops = {
1176b863ceb7SPatrick McHardy 	.get_link		= ethtool_op_get_link,
117785f95819SDavid Decotigny 	.get_link_ksettings	= macvlan_ethtool_get_link_ksettings,
1178b863ceb7SPatrick McHardy 	.get_drvinfo		= macvlan_ethtool_get_drvinfo,
1179254c0a2bSHangbin Liu 	.get_ts_info		= macvlan_ethtool_get_ts_info,
1180b863ceb7SPatrick McHardy };
1181b863ceb7SPatrick McHardy 
118254a30c97SStephen Hemminger static const struct net_device_ops macvlan_netdev_ops = {
118354a30c97SStephen Hemminger 	.ndo_init		= macvlan_init,
1184fccaf710SEric Dumazet 	.ndo_uninit		= macvlan_uninit,
118554a30c97SStephen Hemminger 	.ndo_open		= macvlan_open,
118654a30c97SStephen Hemminger 	.ndo_stop		= macvlan_stop,
118700829823SStephen Hemminger 	.ndo_start_xmit		= macvlan_start_xmit,
118854a30c97SStephen Hemminger 	.ndo_change_mtu		= macvlan_change_mtu,
11892be5c767SVlad Yasevich 	.ndo_fix_features	= macvlan_fix_features,
119054a30c97SStephen Hemminger 	.ndo_change_rx_flags	= macvlan_change_rx_flags,
119154a30c97SStephen Hemminger 	.ndo_set_mac_address	= macvlan_set_mac_address,
1192df8ef8f3SJohn Fastabend 	.ndo_set_rx_mode	= macvlan_set_mac_lists,
1193bc66154eSEric Dumazet 	.ndo_get_stats64	= macvlan_dev_get_stats64,
119454a30c97SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
11958d13e670SJohn Fastabend 	.ndo_vlan_rx_add_vid	= macvlan_vlan_rx_add_vid,
11968d13e670SJohn Fastabend 	.ndo_vlan_rx_kill_vid	= macvlan_vlan_rx_kill_vid,
1197df8ef8f3SJohn Fastabend 	.ndo_fdb_add		= macvlan_fdb_add,
1198df8ef8f3SJohn Fastabend 	.ndo_fdb_del		= macvlan_fdb_del,
1199df8ef8f3SJohn Fastabend 	.ndo_fdb_dump		= ndo_dflt_fdb_dump,
1200688cea83Sdingtianhong #ifdef CONFIG_NET_POLL_CONTROLLER
1201688cea83Sdingtianhong 	.ndo_poll_controller	= macvlan_dev_poll_controller,
1202688cea83Sdingtianhong 	.ndo_netpoll_setup	= macvlan_dev_netpoll_setup,
1203688cea83Sdingtianhong 	.ndo_netpoll_cleanup	= macvlan_dev_netpoll_cleanup,
1204688cea83Sdingtianhong #endif
1205ef5fa6bcSNicolas Dichtel 	.ndo_get_iflink		= macvlan_dev_get_iflink,
1206f56e67b5SToshiaki Makita 	.ndo_features_check	= passthru_features_check,
12070bca3f7fSMaxim Georgiev 	.ndo_hwtstamp_get	= macvlan_hwtstamp_get,
12080bca3f7fSMaxim Georgiev 	.ndo_hwtstamp_set	= macvlan_hwtstamp_set,
120954a30c97SStephen Hemminger };
121054a30c97SStephen Hemminger 
macvlan_dev_free(struct net_device * dev)1211291ac684SZiyang Xuan static void macvlan_dev_free(struct net_device *dev)
1212291ac684SZiyang Xuan {
1213291ac684SZiyang Xuan 	struct macvlan_dev *vlan = netdev_priv(dev);
1214291ac684SZiyang Xuan 
1215291ac684SZiyang Xuan 	/* Get rid of the macvlan's reference to lowerdev */
1216d62607c3SJakub Kicinski 	netdev_put(vlan->lowerdev, &vlan->dev_tracker);
1217291ac684SZiyang Xuan }
1218291ac684SZiyang Xuan 
macvlan_common_setup(struct net_device * dev)12198a35747aSHerbert Xu void macvlan_common_setup(struct net_device *dev)
1220b863ceb7SPatrick McHardy {
1221b863ceb7SPatrick McHardy 	ether_setup(dev);
1222b863ceb7SPatrick McHardy 
1223b64085b0SEric Dumazet 	/* ether_setup() has set dev->min_mtu to ETH_MIN_MTU. */
122491572088SJarod Wilson 	dev->max_mtu		= ETH_MAX_MTU;
122502875878SEric Dumazet 	dev->priv_flags	       &= ~IFF_TX_SKB_SHARING;
122602875878SEric Dumazet 	netif_keep_dst(dev);
12272106efdaSJakub Kicinski 	dev->priv_flags	       |= IFF_UNICAST_FLT | IFF_CHANGE_PROTO_DOWN;
122854a30c97SStephen Hemminger 	dev->netdev_ops		= &macvlan_netdev_ops;
1229cf124db5SDavid S. Miller 	dev->needs_free_netdev	= true;
1230291ac684SZiyang Xuan 	dev->priv_destructor	= macvlan_dev_free;
123171740129SLutz Jaenicke 	dev->header_ops		= &macvlan_hard_header_ops;
1232b863ceb7SPatrick McHardy 	dev->ethtool_ops	= &macvlan_ethtool_ops;
12338a35747aSHerbert Xu }
12348a35747aSHerbert Xu EXPORT_SYMBOL_GPL(macvlan_common_setup);
12358a35747aSHerbert Xu 
macvlan_setup(struct net_device * dev)12368a35747aSHerbert Xu static void macvlan_setup(struct net_device *dev)
12378a35747aSHerbert Xu {
12388a35747aSHerbert Xu 	macvlan_common_setup(dev);
12397009212bSZhang Shengju 	dev->priv_flags |= IFF_NO_QUEUE;
1240b863ceb7SPatrick McHardy }
1241b863ceb7SPatrick McHardy 
macvlan_port_create(struct net_device * dev)1242b863ceb7SPatrick McHardy static int macvlan_port_create(struct net_device *dev)
1243b863ceb7SPatrick McHardy {
1244b863ceb7SPatrick McHardy 	struct macvlan_port *port;
1245b863ceb7SPatrick McHardy 	unsigned int i;
1246ab95bfe0SJiri Pirko 	int err;
1247b863ceb7SPatrick McHardy 
1248b863ceb7SPatrick McHardy 	if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
1249b863ceb7SPatrick McHardy 		return -EINVAL;
1250b863ceb7SPatrick McHardy 
1251322dc6e0SMahesh Bandewar 	if (netdev_is_rx_handler_busy(dev))
1252d6b00fecSMahesh Bandewar 		return -EBUSY;
1253d6b00fecSMahesh Bandewar 
1254b863ceb7SPatrick McHardy 	port = kzalloc(sizeof(*port), GFP_KERNEL);
1255b863ceb7SPatrick McHardy 	if (port == NULL)
1256b863ceb7SPatrick McHardy 		return -ENOMEM;
1257b863ceb7SPatrick McHardy 
1258b863ceb7SPatrick McHardy 	port->dev = dev;
125918c8c54dSVlad Yasevich 	ether_addr_copy(port->perm_addr, dev->dev_addr);
1260b863ceb7SPatrick McHardy 	INIT_LIST_HEAD(&port->vlans);
1261b863ceb7SPatrick McHardy 	for (i = 0; i < MACVLAN_HASH_SIZE; i++)
1262b863ceb7SPatrick McHardy 		INIT_HLIST_HEAD(&port->vlan_hash[i]);
126379cf79abSMichael Braun 	for (i = 0; i < MACVLAN_HASH_SIZE; i++)
126479cf79abSMichael Braun 		INIT_HLIST_HEAD(&port->vlan_source_hash[i]);
1265ab95bfe0SJiri Pirko 
1266d4bff72cSThomas Karlsson 	port->bc_queue_len_used = 0;
1267954d1fa1SHerbert Xu 	port->bc_cutoff = 1;
1268412ca155SHerbert Xu 	skb_queue_head_init(&port->bc_queue);
1269412ca155SHerbert Xu 	INIT_WORK(&port->bc_work, macvlan_process_broadcast);
1270412ca155SHerbert Xu 
1271a35e2c1bSJiri Pirko 	err = netdev_rx_handler_register(dev, macvlan_handle_frame, port);
1272a35e2c1bSJiri Pirko 	if (err)
1273ab95bfe0SJiri Pirko 		kfree(port);
1274d9351561SEric Dumazet 	else
1275a35e2c1bSJiri Pirko 		dev->priv_flags |= IFF_MACVLAN_PORT;
1276ab95bfe0SJiri Pirko 	return err;
1277b863ceb7SPatrick McHardy }
1278b863ceb7SPatrick McHardy 
macvlan_port_destroy(struct net_device * dev)1279b863ceb7SPatrick McHardy static void macvlan_port_destroy(struct net_device *dev)
1280b863ceb7SPatrick McHardy {
1281e052f7e6SEric Dumazet 	struct macvlan_port *port = macvlan_port_get_rtnl(dev);
1282f6478218SHerbert Xu 	struct sk_buff *skb;
1283b863ceb7SPatrick McHardy 
1284a35e2c1bSJiri Pirko 	dev->priv_flags &= ~IFF_MACVLAN_PORT;
1285ab95bfe0SJiri Pirko 	netdev_rx_handler_unregister(dev);
1286fe0ca732SEric Dumazet 
1287fe0ca732SEric Dumazet 	/* After this point, no packet can schedule bc_work anymore,
1288fe0ca732SEric Dumazet 	 * but we need to cancel it and purge left skbs if any.
1289fe0ca732SEric Dumazet 	 */
1290fe0ca732SEric Dumazet 	cancel_work_sync(&port->bc_work);
1291f6478218SHerbert Xu 
1292f6478218SHerbert Xu 	while ((skb = __skb_dequeue(&port->bc_queue))) {
1293f6478218SHerbert Xu 		const struct macvlan_dev *src = MACVLAN_SKB_CB(skb)->src;
1294f6478218SHerbert Xu 
1295f6478218SHerbert Xu 		if (src)
1296f6478218SHerbert Xu 			dev_put(src->dev);
1297f6478218SHerbert Xu 
1298f6478218SHerbert Xu 		kfree_skb(skb);
1299f6478218SHerbert Xu 	}
1300fe0ca732SEric Dumazet 
130118c8c54dSVlad Yasevich 	/* If the lower device address has been changed by passthru
130218c8c54dSVlad Yasevich 	 * macvlan, put it back.
130318c8c54dSVlad Yasevich 	 */
130418c8c54dSVlad Yasevich 	if (macvlan_passthru(port) &&
130518c8c54dSVlad Yasevich 	    !ether_addr_equal(port->dev->dev_addr, port->perm_addr)) {
130618c8c54dSVlad Yasevich 		struct sockaddr sa;
130718c8c54dSVlad Yasevich 
130818c8c54dSVlad Yasevich 		sa.sa_family = port->dev->type;
130918c8c54dSVlad Yasevich 		memcpy(&sa.sa_data, port->perm_addr, port->dev->addr_len);
13103a37a963SPetr Machata 		dev_set_mac_address(port->dev, &sa, NULL);
131118c8c54dSVlad Yasevich 	}
131218c8c54dSVlad Yasevich 
1313a1f5315cSGao Feng 	kfree(port);
1314b863ceb7SPatrick McHardy }
1315b863ceb7SPatrick McHardy 
macvlan_validate(struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)1316a8b8a889SMatthias Schiffer static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[],
1317a8b8a889SMatthias Schiffer 			    struct netlink_ext_ack *extack)
1318b863ceb7SPatrick McHardy {
13198b61fba5SAlvin Šipraga 	struct nlattr *nla, *head;
13208b61fba5SAlvin Šipraga 	int rem, len;
13218b61fba5SAlvin Šipraga 
1322b863ceb7SPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
1323b863ceb7SPatrick McHardy 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1324b863ceb7SPatrick McHardy 			return -EINVAL;
1325b863ceb7SPatrick McHardy 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1326b863ceb7SPatrick McHardy 			return -EADDRNOTAVAIL;
1327b863ceb7SPatrick McHardy 	}
132827c0b1a8SArnd Bergmann 
132907850a4fSZhang Shengju 	if (!data)
133007850a4fSZhang Shengju 		return 0;
133107850a4fSZhang Shengju 
133207850a4fSZhang Shengju 	if (data[IFLA_MACVLAN_FLAGS] &&
1333427f0c8cSJethro Beekman 	    nla_get_u16(data[IFLA_MACVLAN_FLAGS]) & ~(MACVLAN_FLAG_NOPROMISC |
1334427f0c8cSJethro Beekman 						      MACVLAN_FLAG_NODST))
133515127478SMichael S. Tsirkin 		return -EINVAL;
133615127478SMichael S. Tsirkin 
133707850a4fSZhang Shengju 	if (data[IFLA_MACVLAN_MODE]) {
133827c0b1a8SArnd Bergmann 		switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) {
133927c0b1a8SArnd Bergmann 		case MACVLAN_MODE_PRIVATE:
134027c0b1a8SArnd Bergmann 		case MACVLAN_MODE_VEPA:
134127c0b1a8SArnd Bergmann 		case MACVLAN_MODE_BRIDGE:
1342eb06acdcSSridhar Samudrala 		case MACVLAN_MODE_PASSTHRU:
134379cf79abSMichael Braun 		case MACVLAN_MODE_SOURCE:
134427c0b1a8SArnd Bergmann 			break;
134527c0b1a8SArnd Bergmann 		default:
134627c0b1a8SArnd Bergmann 			return -EINVAL;
134727c0b1a8SArnd Bergmann 		}
134827c0b1a8SArnd Bergmann 	}
134979cf79abSMichael Braun 
135007850a4fSZhang Shengju 	if (data[IFLA_MACVLAN_MACADDR_MODE]) {
135179cf79abSMichael Braun 		switch (nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE])) {
135279cf79abSMichael Braun 		case MACVLAN_MACADDR_ADD:
135379cf79abSMichael Braun 		case MACVLAN_MACADDR_DEL:
135479cf79abSMichael Braun 		case MACVLAN_MACADDR_FLUSH:
135579cf79abSMichael Braun 		case MACVLAN_MACADDR_SET:
135679cf79abSMichael Braun 			break;
135779cf79abSMichael Braun 		default:
135879cf79abSMichael Braun 			return -EINVAL;
135979cf79abSMichael Braun 		}
136079cf79abSMichael Braun 	}
136179cf79abSMichael Braun 
136207850a4fSZhang Shengju 	if (data[IFLA_MACVLAN_MACADDR]) {
136379cf79abSMichael Braun 		if (nla_len(data[IFLA_MACVLAN_MACADDR]) != ETH_ALEN)
136479cf79abSMichael Braun 			return -EINVAL;
136579cf79abSMichael Braun 
136679cf79abSMichael Braun 		if (!is_valid_ether_addr(nla_data(data[IFLA_MACVLAN_MACADDR])))
136779cf79abSMichael Braun 			return -EADDRNOTAVAIL;
136879cf79abSMichael Braun 	}
136979cf79abSMichael Braun 
13708b61fba5SAlvin Šipraga 	if (data[IFLA_MACVLAN_MACADDR_DATA]) {
13718b61fba5SAlvin Šipraga 		head = nla_data(data[IFLA_MACVLAN_MACADDR_DATA]);
13728b61fba5SAlvin Šipraga 		len = nla_len(data[IFLA_MACVLAN_MACADDR_DATA]);
13738b61fba5SAlvin Šipraga 
13748b61fba5SAlvin Šipraga 		nla_for_each_attr(nla, head, len, rem) {
13758b61fba5SAlvin Šipraga 			if (nla_type(nla) != IFLA_MACVLAN_MACADDR ||
13768b61fba5SAlvin Šipraga 			    nla_len(nla) != ETH_ALEN)
13778b61fba5SAlvin Šipraga 				return -EINVAL;
13788b61fba5SAlvin Šipraga 
13798b61fba5SAlvin Šipraga 			if (!is_valid_ether_addr(nla_data(nla)))
13808b61fba5SAlvin Šipraga 				return -EADDRNOTAVAIL;
13818b61fba5SAlvin Šipraga 		}
13828b61fba5SAlvin Šipraga 	}
13838b61fba5SAlvin Šipraga 
138407850a4fSZhang Shengju 	if (data[IFLA_MACVLAN_MACADDR_COUNT])
138579cf79abSMichael Braun 		return -EINVAL;
138679cf79abSMichael Braun 
138779cf79abSMichael Braun 	return 0;
138879cf79abSMichael Braun }
138979cf79abSMichael Braun 
139050a77e5fSLee Jones /*
139179cf79abSMichael Braun  * reconfigure list of remote source mac address
139279cf79abSMichael Braun  * (only for macvlan devices in source mode)
139379cf79abSMichael Braun  * Note regarding alignment: all netlink data is aligned to 4 Byte, which
139479cf79abSMichael Braun  * suffices for both ether_addr_copy and ether_addr_equal_64bits usage.
139579cf79abSMichael Braun  */
macvlan_changelink_sources(struct macvlan_dev * vlan,u32 mode,struct nlattr * data[])139679cf79abSMichael Braun static int macvlan_changelink_sources(struct macvlan_dev *vlan, u32 mode,
139779cf79abSMichael Braun 				      struct nlattr *data[])
139879cf79abSMichael Braun {
139979cf79abSMichael Braun 	char *addr = NULL;
140079cf79abSMichael Braun 	int ret, rem, len;
140179cf79abSMichael Braun 	struct nlattr *nla, *head;
140279cf79abSMichael Braun 	struct macvlan_source_entry *entry;
140379cf79abSMichael Braun 
140479cf79abSMichael Braun 	if (data[IFLA_MACVLAN_MACADDR])
140579cf79abSMichael Braun 		addr = nla_data(data[IFLA_MACVLAN_MACADDR]);
140679cf79abSMichael Braun 
140779cf79abSMichael Braun 	if (mode == MACVLAN_MACADDR_ADD) {
140879cf79abSMichael Braun 		if (!addr)
140979cf79abSMichael Braun 			return -EINVAL;
141079cf79abSMichael Braun 
141179cf79abSMichael Braun 		return macvlan_hash_add_source(vlan, addr);
141279cf79abSMichael Braun 
141379cf79abSMichael Braun 	} else if (mode == MACVLAN_MACADDR_DEL) {
141479cf79abSMichael Braun 		if (!addr)
141579cf79abSMichael Braun 			return -EINVAL;
141679cf79abSMichael Braun 
141779cf79abSMichael Braun 		entry = macvlan_hash_lookup_source(vlan, addr);
141879cf79abSMichael Braun 		if (entry) {
141979cf79abSMichael Braun 			macvlan_hash_del_source(entry);
142079cf79abSMichael Braun 			vlan->macaddr_count--;
142179cf79abSMichael Braun 		}
142279cf79abSMichael Braun 	} else if (mode == MACVLAN_MACADDR_FLUSH) {
142379cf79abSMichael Braun 		macvlan_flush_sources(vlan->port, vlan);
142479cf79abSMichael Braun 	} else if (mode == MACVLAN_MACADDR_SET) {
142579cf79abSMichael Braun 		macvlan_flush_sources(vlan->port, vlan);
142679cf79abSMichael Braun 
142779cf79abSMichael Braun 		if (addr) {
142879cf79abSMichael Braun 			ret = macvlan_hash_add_source(vlan, addr);
142979cf79abSMichael Braun 			if (ret)
143079cf79abSMichael Braun 				return ret;
143179cf79abSMichael Braun 		}
143279cf79abSMichael Braun 
143389430ef3SYunjian Wang 		if (!data[IFLA_MACVLAN_MACADDR_DATA])
143479cf79abSMichael Braun 			return 0;
143579cf79abSMichael Braun 
143679cf79abSMichael Braun 		head = nla_data(data[IFLA_MACVLAN_MACADDR_DATA]);
143779cf79abSMichael Braun 		len = nla_len(data[IFLA_MACVLAN_MACADDR_DATA]);
143879cf79abSMichael Braun 
143979cf79abSMichael Braun 		nla_for_each_attr(nla, head, len, rem) {
144079cf79abSMichael Braun 			addr = nla_data(nla);
144179cf79abSMichael Braun 			ret = macvlan_hash_add_source(vlan, addr);
144279cf79abSMichael Braun 			if (ret)
144379cf79abSMichael Braun 				return ret;
144479cf79abSMichael Braun 		}
144579cf79abSMichael Braun 	} else {
144679cf79abSMichael Braun 		return -EINVAL;
144779cf79abSMichael Braun 	}
144879cf79abSMichael Braun 
1449b863ceb7SPatrick McHardy 	return 0;
1450b863ceb7SPatrick McHardy }
1451b863ceb7SPatrick McHardy 
macvlan_common_newlink(struct net * src_net,struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)1452fc0663d6SArnd Bergmann int macvlan_common_newlink(struct net *src_net, struct net_device *dev,
145342ab19eeSDavid Ahern 			   struct nlattr *tb[], struct nlattr *data[],
145442ab19eeSDavid Ahern 			   struct netlink_ext_ack *extack)
1455b863ceb7SPatrick McHardy {
1456b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
1457b863ceb7SPatrick McHardy 	struct macvlan_port *port;
1458b863ceb7SPatrick McHardy 	struct net_device *lowerdev;
1459b863ceb7SPatrick McHardy 	int err;
146079cf79abSMichael Braun 	int macmode;
1461aa5fd0fbSGao Feng 	bool create = false;
1462b863ceb7SPatrick McHardy 
1463b863ceb7SPatrick McHardy 	if (!tb[IFLA_LINK])
1464b863ceb7SPatrick McHardy 		return -EINVAL;
1465b863ceb7SPatrick McHardy 
146681adee47SEric W. Biederman 	lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
1467b863ceb7SPatrick McHardy 	if (lowerdev == NULL)
1468b863ceb7SPatrick McHardy 		return -ENODEV;
1469b863ceb7SPatrick McHardy 
1470d70f2cf5SKevin Wallace 	/* When creating macvlans or macvtaps on top of other macvlans - use
1471b0832a29SEric Biederman 	 * the real device as the lowerdev.
1472a6ca5f1dSPatrick McHardy 	 */
1473d70f2cf5SKevin Wallace 	if (netif_is_macvlan(lowerdev))
1474d70f2cf5SKevin Wallace 		lowerdev = macvlan_dev_real_dev(lowerdev);
1475a6ca5f1dSPatrick McHardy 
1476b863ceb7SPatrick McHardy 	if (!tb[IFLA_MTU])
1477b863ceb7SPatrick McHardy 		dev->mtu = lowerdev->mtu;
1478b863ceb7SPatrick McHardy 	else if (dev->mtu > lowerdev->mtu)
1479b863ceb7SPatrick McHardy 		return -EINVAL;
1480b863ceb7SPatrick McHardy 
148191572088SJarod Wilson 	/* MTU range: 68 - lowerdev->max_mtu */
148291572088SJarod Wilson 	dev->min_mtu = ETH_MIN_MTU;
148391572088SJarod Wilson 	dev->max_mtu = lowerdev->max_mtu;
148491572088SJarod Wilson 
1485b863ceb7SPatrick McHardy 	if (!tb[IFLA_ADDRESS])
14867ce5d222SDanny Kukawka 		eth_hw_addr_random(dev);
1487b863ceb7SPatrick McHardy 
1488913564fbSJulian Wiedmann 	if (!netif_is_macvlan_port(lowerdev)) {
1489b863ceb7SPatrick McHardy 		err = macvlan_port_create(lowerdev);
1490b863ceb7SPatrick McHardy 		if (err < 0)
1491b863ceb7SPatrick McHardy 			return err;
1492aa5fd0fbSGao Feng 		create = true;
1493b863ceb7SPatrick McHardy 	}
1494e052f7e6SEric Dumazet 	port = macvlan_port_get_rtnl(lowerdev);
1495b863ceb7SPatrick McHardy 
1496eb06acdcSSridhar Samudrala 	/* Only 1 macvlan device can be created in passthru mode */
149743c2d578SVlad Yasevich 	if (macvlan_passthru(port)) {
1498aa5fd0fbSGao Feng 		/* The macvlan port must be not created this time,
1499aa5fd0fbSGao Feng 		 * still goto destroy_macvlan_port for readability.
1500aa5fd0fbSGao Feng 		 */
1501aa5fd0fbSGao Feng 		err = -EINVAL;
1502aa5fd0fbSGao Feng 		goto destroy_macvlan_port;
1503aa5fd0fbSGao Feng 	}
1504eb06acdcSSridhar Samudrala 
1505b863ceb7SPatrick McHardy 	vlan->lowerdev = lowerdev;
1506b863ceb7SPatrick McHardy 	vlan->dev      = dev;
1507b863ceb7SPatrick McHardy 	vlan->port     = port;
15082be5c767SVlad Yasevich 	vlan->set_features = MACVLAN_FEATURES;
1509b863ceb7SPatrick McHardy 
151027c0b1a8SArnd Bergmann 	vlan->mode     = MACVLAN_MODE_VEPA;
151127c0b1a8SArnd Bergmann 	if (data && data[IFLA_MACVLAN_MODE])
151227c0b1a8SArnd Bergmann 		vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
151327c0b1a8SArnd Bergmann 
1514df8ef8f3SJohn Fastabend 	if (data && data[IFLA_MACVLAN_FLAGS])
1515df8ef8f3SJohn Fastabend 		vlan->flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
1516df8ef8f3SJohn Fastabend 
1517eb06acdcSSridhar Samudrala 	if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
1518aa5fd0fbSGao Feng 		if (port->count) {
1519aa5fd0fbSGao Feng 			err = -EINVAL;
1520aa5fd0fbSGao Feng 			goto destroy_macvlan_port;
1521aa5fd0fbSGao Feng 		}
152243c2d578SVlad Yasevich 		macvlan_set_passthru(port);
15238b98604eSBjørn Mork 		eth_hw_addr_inherit(dev, lowerdev);
1524eb06acdcSSridhar Samudrala 	}
1525eb06acdcSSridhar Samudrala 
152679cf79abSMichael Braun 	if (data && data[IFLA_MACVLAN_MACADDR_MODE]) {
1527aa5fd0fbSGao Feng 		if (vlan->mode != MACVLAN_MODE_SOURCE) {
1528aa5fd0fbSGao Feng 			err = -EINVAL;
1529aa5fd0fbSGao Feng 			goto destroy_macvlan_port;
1530aa5fd0fbSGao Feng 		}
153179cf79abSMichael Braun 		macmode = nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE]);
153279cf79abSMichael Braun 		err = macvlan_changelink_sources(vlan, macmode, data);
153379cf79abSMichael Braun 		if (err)
1534aa5fd0fbSGao Feng 			goto destroy_macvlan_port;
153579cf79abSMichael Braun 	}
153679cf79abSMichael Braun 
1537d4bff72cSThomas Karlsson 	vlan->bc_queue_len_req = MACVLAN_DEFAULT_BC_QUEUE_LEN;
1538d4bff72cSThomas Karlsson 	if (data && data[IFLA_MACVLAN_BC_QUEUE_LEN])
1539d4bff72cSThomas Karlsson 		vlan->bc_queue_len_req = nla_get_u32(data[IFLA_MACVLAN_BC_QUEUE_LEN]);
1540d4bff72cSThomas Karlsson 
1541954d1fa1SHerbert Xu 	if (data && data[IFLA_MACVLAN_BC_CUTOFF])
1542954d1fa1SHerbert Xu 		update_port_bc_cutoff(
1543954d1fa1SHerbert Xu 			vlan, nla_get_s32(data[IFLA_MACVLAN_BC_CUTOFF]));
1544954d1fa1SHerbert Xu 
154547d4ab91SJohn Fastabend 	err = register_netdevice(dev);
154647d4ab91SJohn Fastabend 	if (err < 0)
1547aa5fd0fbSGao Feng 		goto destroy_macvlan_port;
154847d4ab91SJohn Fastabend 
1549a6cc0cfaSJohn Fastabend 	dev->priv_flags |= IFF_MACVLAN;
155042ab19eeSDavid Ahern 	err = netdev_upper_dev_link(lowerdev, dev, extack);
15517cd43db7SJiri Pirko 	if (err)
1552da37705cSCong Wang 		goto unregister_netdev;
1553b863ceb7SPatrick McHardy 
1554233c7df0SJiri Pirko 	list_add_tail_rcu(&vlan->list, &port->vlans);
1555d4bff72cSThomas Karlsson 	update_port_bc_queue_len(vlan->port);
1556fc4a7489SPatrick Mullaney 	netif_stacked_transfer_operstate(lowerdev, dev);
1557de7d244dSNikolay Aleksandrov 	linkwatch_fire_event(dev);
1558f16d3d57SJiri Pirko 
1559b863ceb7SPatrick McHardy 	return 0;
1560f16d3d57SJiri Pirko 
1561da37705cSCong Wang unregister_netdev:
1562d02fd6e7SGao Feng 	/* macvlan_uninit would free the macvlan port */
1563da37705cSCong Wang 	unregister_netdevice(dev);
1564d02fd6e7SGao Feng 	return err;
1565aa5fd0fbSGao Feng destroy_macvlan_port:
1566d02fd6e7SGao Feng 	/* the macvlan port may be freed by macvlan_uninit when fail to register.
1567d02fd6e7SGao Feng 	 * so we destroy the macvlan port only when it's valid.
1568d02fd6e7SGao Feng 	 */
156923569b56SChuang Wang 	if (create && macvlan_port_get_rtnl(lowerdev)) {
157023569b56SChuang Wang 		macvlan_flush_sources(port, vlan);
1571aa5fd0fbSGao Feng 		macvlan_port_destroy(port->dev);
157223569b56SChuang Wang 	}
1573f16d3d57SJiri Pirko 	return err;
1574b863ceb7SPatrick McHardy }
1575fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_common_newlink);
1576b863ceb7SPatrick McHardy 
macvlan_newlink(struct net * src_net,struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)1577fc0663d6SArnd Bergmann static int macvlan_newlink(struct net *src_net, struct net_device *dev,
15787a3f4a18SMatthias Schiffer 			   struct nlattr *tb[], struct nlattr *data[],
15797a3f4a18SMatthias Schiffer 			   struct netlink_ext_ack *extack)
1580fc0663d6SArnd Bergmann {
158142ab19eeSDavid Ahern 	return macvlan_common_newlink(src_net, dev, tb, data, extack);
1582fc0663d6SArnd Bergmann }
1583fc0663d6SArnd Bergmann 
macvlan_dellink(struct net_device * dev,struct list_head * head)1584fc0663d6SArnd Bergmann void macvlan_dellink(struct net_device *dev, struct list_head *head)
1585b863ceb7SPatrick McHardy {
1586b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
1587b863ceb7SPatrick McHardy 
158879cf79abSMichael Braun 	if (vlan->mode == MACVLAN_MODE_SOURCE)
158979cf79abSMichael Braun 		macvlan_flush_sources(vlan->port, vlan);
1590233c7df0SJiri Pirko 	list_del_rcu(&vlan->list);
1591d4bff72cSThomas Karlsson 	update_port_bc_queue_len(vlan->port);
159223289a37SEric Dumazet 	unregister_netdevice_queue(dev, head);
15937cd43db7SJiri Pirko 	netdev_upper_dev_unlink(vlan->lowerdev, dev);
1594b863ceb7SPatrick McHardy }
1595fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_dellink);
1596b863ceb7SPatrick McHardy 
macvlan_changelink(struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)159727c0b1a8SArnd Bergmann static int macvlan_changelink(struct net_device *dev,
1598ad744b22SMatthias Schiffer 			      struct nlattr *tb[], struct nlattr *data[],
1599ad744b22SMatthias Schiffer 			      struct netlink_ext_ack *extack)
160027c0b1a8SArnd Bergmann {
160127c0b1a8SArnd Bergmann 	struct macvlan_dev *vlan = netdev_priv(dev);
1602266e8347SMichael S. Tsirkin 	enum macvlan_mode mode;
1603266e8347SMichael S. Tsirkin 	bool set_mode = false;
160479cf79abSMichael Braun 	enum macvlan_macaddr_mode macmode;
160579cf79abSMichael Braun 	int ret;
1606266e8347SMichael S. Tsirkin 
1607266e8347SMichael S. Tsirkin 	/* Validate mode, but don't set yet: setting flags may fail. */
1608266e8347SMichael S. Tsirkin 	if (data && data[IFLA_MACVLAN_MODE]) {
1609266e8347SMichael S. Tsirkin 		set_mode = true;
1610266e8347SMichael S. Tsirkin 		mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
1611266e8347SMichael S. Tsirkin 		/* Passthrough mode can't be set or cleared dynamically */
1612266e8347SMichael S. Tsirkin 		if ((mode == MACVLAN_MODE_PASSTHRU) !=
1613266e8347SMichael S. Tsirkin 		    (vlan->mode == MACVLAN_MODE_PASSTHRU))
1614266e8347SMichael S. Tsirkin 			return -EINVAL;
161579cf79abSMichael Braun 		if (vlan->mode == MACVLAN_MODE_SOURCE &&
161679cf79abSMichael Braun 		    vlan->mode != mode)
161779cf79abSMichael Braun 			macvlan_flush_sources(vlan->port, vlan);
1618266e8347SMichael S. Tsirkin 	}
161999ffc3e7SMichael S. Tsirkin 
1620df8ef8f3SJohn Fastabend 	if (data && data[IFLA_MACVLAN_FLAGS]) {
1621df8ef8f3SJohn Fastabend 		__u16 flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
1622df8ef8f3SJohn Fastabend 		bool promisc = (flags ^ vlan->flags) & MACVLAN_FLAG_NOPROMISC;
162343c2d578SVlad Yasevich 		if (macvlan_passthru(vlan->port) && promisc) {
162499ffc3e7SMichael S. Tsirkin 			int err;
1625df8ef8f3SJohn Fastabend 
162699ffc3e7SMichael S. Tsirkin 			if (flags & MACVLAN_FLAG_NOPROMISC)
162799ffc3e7SMichael S. Tsirkin 				err = dev_set_promiscuity(vlan->lowerdev, -1);
162899ffc3e7SMichael S. Tsirkin 			else
162999ffc3e7SMichael S. Tsirkin 				err = dev_set_promiscuity(vlan->lowerdev, 1);
163099ffc3e7SMichael S. Tsirkin 			if (err < 0)
163199ffc3e7SMichael S. Tsirkin 				return err;
163299ffc3e7SMichael S. Tsirkin 		}
1633df8ef8f3SJohn Fastabend 		vlan->flags = flags;
1634df8ef8f3SJohn Fastabend 	}
1635d4bff72cSThomas Karlsson 
1636d4bff72cSThomas Karlsson 	if (data && data[IFLA_MACVLAN_BC_QUEUE_LEN]) {
1637d4bff72cSThomas Karlsson 		vlan->bc_queue_len_req = nla_get_u32(data[IFLA_MACVLAN_BC_QUEUE_LEN]);
1638d4bff72cSThomas Karlsson 		update_port_bc_queue_len(vlan->port);
1639d4bff72cSThomas Karlsson 	}
1640d4bff72cSThomas Karlsson 
1641954d1fa1SHerbert Xu 	if (data && data[IFLA_MACVLAN_BC_CUTOFF])
1642954d1fa1SHerbert Xu 		update_port_bc_cutoff(
1643954d1fa1SHerbert Xu 			vlan, nla_get_s32(data[IFLA_MACVLAN_BC_CUTOFF]));
1644954d1fa1SHerbert Xu 
1645266e8347SMichael S. Tsirkin 	if (set_mode)
1646266e8347SMichael S. Tsirkin 		vlan->mode = mode;
164779cf79abSMichael Braun 	if (data && data[IFLA_MACVLAN_MACADDR_MODE]) {
164879cf79abSMichael Braun 		if (vlan->mode != MACVLAN_MODE_SOURCE)
164979cf79abSMichael Braun 			return -EINVAL;
165079cf79abSMichael Braun 		macmode = nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE]);
165179cf79abSMichael Braun 		ret = macvlan_changelink_sources(vlan, macmode, data);
165279cf79abSMichael Braun 		if (ret)
165379cf79abSMichael Braun 			return ret;
165479cf79abSMichael Braun 	}
165527c0b1a8SArnd Bergmann 	return 0;
165627c0b1a8SArnd Bergmann }
165727c0b1a8SArnd Bergmann 
macvlan_get_size_mac(const struct macvlan_dev * vlan)165879cf79abSMichael Braun static size_t macvlan_get_size_mac(const struct macvlan_dev *vlan)
165979cf79abSMichael Braun {
166079cf79abSMichael Braun 	if (vlan->macaddr_count == 0)
166179cf79abSMichael Braun 		return 0;
166279cf79abSMichael Braun 	return nla_total_size(0) /* IFLA_MACVLAN_MACADDR_DATA */
166379cf79abSMichael Braun 		+ vlan->macaddr_count * nla_total_size(sizeof(u8) * ETH_ALEN);
166479cf79abSMichael Braun }
166579cf79abSMichael Braun 
macvlan_get_size(const struct net_device * dev)166627c0b1a8SArnd Bergmann static size_t macvlan_get_size(const struct net_device *dev)
166727c0b1a8SArnd Bergmann {
166879cf79abSMichael Braun 	struct macvlan_dev *vlan = netdev_priv(dev);
166979cf79abSMichael Braun 
167001fe944fSEric Dumazet 	return (0
167101fe944fSEric Dumazet 		+ nla_total_size(4) /* IFLA_MACVLAN_MODE */
167201fe944fSEric Dumazet 		+ nla_total_size(2) /* IFLA_MACVLAN_FLAGS */
167379cf79abSMichael Braun 		+ nla_total_size(4) /* IFLA_MACVLAN_MACADDR_COUNT */
167479cf79abSMichael Braun 		+ macvlan_get_size_mac(vlan) /* IFLA_MACVLAN_MACADDR */
1675d4bff72cSThomas Karlsson 		+ nla_total_size(4) /* IFLA_MACVLAN_BC_QUEUE_LEN */
1676d4bff72cSThomas Karlsson 		+ nla_total_size(4) /* IFLA_MACVLAN_BC_QUEUE_LEN_USED */
167701fe944fSEric Dumazet 		);
167827c0b1a8SArnd Bergmann }
167927c0b1a8SArnd Bergmann 
macvlan_fill_info_macaddr(struct sk_buff * skb,const struct macvlan_dev * vlan,const int i)168079cf79abSMichael Braun static int macvlan_fill_info_macaddr(struct sk_buff *skb,
168179cf79abSMichael Braun 				     const struct macvlan_dev *vlan,
168279cf79abSMichael Braun 				     const int i)
168379cf79abSMichael Braun {
168479cf79abSMichael Braun 	struct hlist_head *h = &vlan->port->vlan_source_hash[i];
168579cf79abSMichael Braun 	struct macvlan_source_entry *entry;
168679cf79abSMichael Braun 
16875df1341eSChuang Wang 	hlist_for_each_entry_rcu(entry, h, hlist, lockdep_rtnl_is_held()) {
168879cf79abSMichael Braun 		if (entry->vlan != vlan)
168979cf79abSMichael Braun 			continue;
169079cf79abSMichael Braun 		if (nla_put(skb, IFLA_MACVLAN_MACADDR, ETH_ALEN, entry->addr))
169179cf79abSMichael Braun 			return 1;
169279cf79abSMichael Braun 	}
169379cf79abSMichael Braun 	return 0;
169479cf79abSMichael Braun }
169579cf79abSMichael Braun 
macvlan_fill_info(struct sk_buff * skb,const struct net_device * dev)169627c0b1a8SArnd Bergmann static int macvlan_fill_info(struct sk_buff *skb,
169727c0b1a8SArnd Bergmann 				const struct net_device *dev)
169827c0b1a8SArnd Bergmann {
169927c0b1a8SArnd Bergmann 	struct macvlan_dev *vlan = netdev_priv(dev);
1700d4bff72cSThomas Karlsson 	struct macvlan_port *port = vlan->port;
170179cf79abSMichael Braun 	int i;
170279cf79abSMichael Braun 	struct nlattr *nest;
170327c0b1a8SArnd Bergmann 
1704ead9a76cSDavid S. Miller 	if (nla_put_u32(skb, IFLA_MACVLAN_MODE, vlan->mode))
1705ead9a76cSDavid S. Miller 		goto nla_put_failure;
1706df8ef8f3SJohn Fastabend 	if (nla_put_u16(skb, IFLA_MACVLAN_FLAGS, vlan->flags))
1707df8ef8f3SJohn Fastabend 		goto nla_put_failure;
170879cf79abSMichael Braun 	if (nla_put_u32(skb, IFLA_MACVLAN_MACADDR_COUNT, vlan->macaddr_count))
170979cf79abSMichael Braun 		goto nla_put_failure;
171079cf79abSMichael Braun 	if (vlan->macaddr_count > 0) {
1711ae0be8deSMichal Kubecek 		nest = nla_nest_start_noflag(skb, IFLA_MACVLAN_MACADDR_DATA);
171279cf79abSMichael Braun 		if (nest == NULL)
171379cf79abSMichael Braun 			goto nla_put_failure;
171479cf79abSMichael Braun 
171579cf79abSMichael Braun 		for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
171679cf79abSMichael Braun 			if (macvlan_fill_info_macaddr(skb, vlan, i))
171779cf79abSMichael Braun 				goto nla_put_failure;
171879cf79abSMichael Braun 		}
171979cf79abSMichael Braun 		nla_nest_end(skb, nest);
172079cf79abSMichael Braun 	}
1721d4bff72cSThomas Karlsson 	if (nla_put_u32(skb, IFLA_MACVLAN_BC_QUEUE_LEN, vlan->bc_queue_len_req))
1722d4bff72cSThomas Karlsson 		goto nla_put_failure;
1723d4bff72cSThomas Karlsson 	if (nla_put_u32(skb, IFLA_MACVLAN_BC_QUEUE_LEN_USED, port->bc_queue_len_used))
1724d4bff72cSThomas Karlsson 		goto nla_put_failure;
1725954d1fa1SHerbert Xu 	if (port->bc_cutoff != 1 &&
1726954d1fa1SHerbert Xu 	    nla_put_s32(skb, IFLA_MACVLAN_BC_CUTOFF, port->bc_cutoff))
1727954d1fa1SHerbert Xu 		goto nla_put_failure;
172827c0b1a8SArnd Bergmann 	return 0;
172927c0b1a8SArnd Bergmann 
173027c0b1a8SArnd Bergmann nla_put_failure:
173127c0b1a8SArnd Bergmann 	return -EMSGSIZE;
173227c0b1a8SArnd Bergmann }
173327c0b1a8SArnd Bergmann 
173427c0b1a8SArnd Bergmann static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = {
173527c0b1a8SArnd Bergmann 	[IFLA_MACVLAN_MODE]  = { .type = NLA_U32 },
1736df8ef8f3SJohn Fastabend 	[IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 },
173779cf79abSMichael Braun 	[IFLA_MACVLAN_MACADDR_MODE] = { .type = NLA_U32 },
173879cf79abSMichael Braun 	[IFLA_MACVLAN_MACADDR] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
173979cf79abSMichael Braun 	[IFLA_MACVLAN_MACADDR_DATA] = { .type = NLA_NESTED },
174079cf79abSMichael Braun 	[IFLA_MACVLAN_MACADDR_COUNT] = { .type = NLA_U32 },
1741d4bff72cSThomas Karlsson 	[IFLA_MACVLAN_BC_QUEUE_LEN] = { .type = NLA_U32 },
1742d4bff72cSThomas Karlsson 	[IFLA_MACVLAN_BC_QUEUE_LEN_USED] = { .type = NLA_REJECT },
174355cef78cSLin Ma 	[IFLA_MACVLAN_BC_CUTOFF] = { .type = NLA_S32 },
174427c0b1a8SArnd Bergmann };
174527c0b1a8SArnd Bergmann 
macvlan_link_register(struct rtnl_link_ops * ops)1746fc0663d6SArnd Bergmann int macvlan_link_register(struct rtnl_link_ops *ops)
1747fc0663d6SArnd Bergmann {
1748fc0663d6SArnd Bergmann 	/* common fields */
1749fc0663d6SArnd Bergmann 	ops->validate		= macvlan_validate;
1750fc0663d6SArnd Bergmann 	ops->maxtype		= IFLA_MACVLAN_MAX;
1751fc0663d6SArnd Bergmann 	ops->policy		= macvlan_policy;
1752fc0663d6SArnd Bergmann 	ops->changelink		= macvlan_changelink;
1753fc0663d6SArnd Bergmann 	ops->get_size		= macvlan_get_size;
1754fc0663d6SArnd Bergmann 	ops->fill_info		= macvlan_fill_info;
1755fc0663d6SArnd Bergmann 
1756fc0663d6SArnd Bergmann 	return rtnl_link_register(ops);
1757fc0663d6SArnd Bergmann };
1758fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_link_register);
1759fc0663d6SArnd Bergmann 
macvlan_get_link_net(const struct net_device * dev)1760eaca400fSNicolas Dichtel static struct net *macvlan_get_link_net(const struct net_device *dev)
1761eaca400fSNicolas Dichtel {
1762eaca400fSNicolas Dichtel 	return dev_net(macvlan_dev_real_dev(dev));
1763eaca400fSNicolas Dichtel }
1764eaca400fSNicolas Dichtel 
1765fc0663d6SArnd Bergmann static struct rtnl_link_ops macvlan_link_ops = {
1766b863ceb7SPatrick McHardy 	.kind		= "macvlan",
17678a35747aSHerbert Xu 	.setup		= macvlan_setup,
1768b863ceb7SPatrick McHardy 	.newlink	= macvlan_newlink,
1769b863ceb7SPatrick McHardy 	.dellink	= macvlan_dellink,
1770eaca400fSNicolas Dichtel 	.get_link_net	= macvlan_get_link_net,
17716fe3faf8SSainath Grandhi 	.priv_size      = sizeof(struct macvlan_dev),
1772b863ceb7SPatrick McHardy };
1773b863ceb7SPatrick McHardy 
update_port_bc_queue_len(struct macvlan_port * port)1774d4bff72cSThomas Karlsson static void update_port_bc_queue_len(struct macvlan_port *port)
1775d4bff72cSThomas Karlsson {
1776d4bff72cSThomas Karlsson 	u32 max_bc_queue_len_req = 0;
1777d4bff72cSThomas Karlsson 	struct macvlan_dev *vlan;
1778d4bff72cSThomas Karlsson 
1779d4bff72cSThomas Karlsson 	list_for_each_entry(vlan, &port->vlans, list) {
1780d4bff72cSThomas Karlsson 		if (vlan->bc_queue_len_req > max_bc_queue_len_req)
1781d4bff72cSThomas Karlsson 			max_bc_queue_len_req = vlan->bc_queue_len_req;
1782d4bff72cSThomas Karlsson 	}
1783d4bff72cSThomas Karlsson 	port->bc_queue_len_used = max_bc_queue_len_req;
1784d4bff72cSThomas Karlsson }
1785d4bff72cSThomas Karlsson 
macvlan_device_event(struct notifier_block * unused,unsigned long event,void * ptr)1786b863ceb7SPatrick McHardy static int macvlan_device_event(struct notifier_block *unused,
1787b863ceb7SPatrick McHardy 				unsigned long event, void *ptr)
1788b863ceb7SPatrick McHardy {
1789351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1790b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan, *next;
1791b863ceb7SPatrick McHardy 	struct macvlan_port *port;
1792226bd341SEric Dumazet 	LIST_HEAD(list_kill);
1793b863ceb7SPatrick McHardy 
1794913564fbSJulian Wiedmann 	if (!netif_is_macvlan_port(dev))
1795b863ceb7SPatrick McHardy 		return NOTIFY_DONE;
1796b863ceb7SPatrick McHardy 
1797e052f7e6SEric Dumazet 	port = macvlan_port_get_rtnl(dev);
1798a35e2c1bSJiri Pirko 
1799b863ceb7SPatrick McHardy 	switch (event) {
1800de7d244dSNikolay Aleksandrov 	case NETDEV_UP:
180180fd2d6cSTravis Brown 	case NETDEV_DOWN:
1802b863ceb7SPatrick McHardy 	case NETDEV_CHANGE:
1803b863ceb7SPatrick McHardy 		list_for_each_entry(vlan, &port->vlans, list)
1804fc4a7489SPatrick Mullaney 			netif_stacked_transfer_operstate(vlan->lowerdev,
1805fc4a7489SPatrick Mullaney 							 vlan->dev);
1806b863ceb7SPatrick McHardy 		break;
1807b863ceb7SPatrick McHardy 	case NETDEV_FEAT_CHANGE:
1808b863ceb7SPatrick McHardy 		list_for_each_entry(vlan, &port->vlans, list) {
18096df6398fSJakub Kicinski 			netif_inherit_tso_max(vlan->dev, dev);
1810797f87f8SFlorian Westphal 			netdev_update_features(vlan->dev);
1811b863ceb7SPatrick McHardy 		}
1812b863ceb7SPatrick McHardy 		break;
18133763e7efSdingtianhong 	case NETDEV_CHANGEMTU:
18143763e7efSdingtianhong 		list_for_each_entry(vlan, &port->vlans, list) {
18153763e7efSdingtianhong 			if (vlan->dev->mtu <= dev->mtu)
18163763e7efSdingtianhong 				continue;
18173763e7efSdingtianhong 			dev_set_mtu(vlan->dev, dev->mtu);
18183763e7efSdingtianhong 		}
18193763e7efSdingtianhong 		break;
1820e289fd28Sdingtianhong 	case NETDEV_CHANGEADDR:
182143c2d578SVlad Yasevich 		if (!macvlan_passthru(port))
1822e289fd28Sdingtianhong 			return NOTIFY_DONE;
1823e289fd28Sdingtianhong 
1824e289fd28Sdingtianhong 		vlan = list_first_entry_or_null(&port->vlans,
1825e289fd28Sdingtianhong 						struct macvlan_dev,
1826e289fd28Sdingtianhong 						list);
1827e289fd28Sdingtianhong 
18284dee15b4STaehee Yoo 		if (vlan && macvlan_sync_address(vlan->dev, dev->dev_addr))
1829e289fd28Sdingtianhong 			return NOTIFY_BAD;
1830e289fd28Sdingtianhong 
1831e289fd28Sdingtianhong 		break;
1832b863ceb7SPatrick McHardy 	case NETDEV_UNREGISTER:
18333b27e105SDavid Lamparter 		/* twiddle thumbs on netns device moves */
18343b27e105SDavid Lamparter 		if (dev->reg_state != NETREG_UNREGISTERING)
18353b27e105SDavid Lamparter 			break;
18363b27e105SDavid Lamparter 
1837b863ceb7SPatrick McHardy 		list_for_each_entry_safe(vlan, next, &port->vlans, list)
1838226bd341SEric Dumazet 			vlan->dev->rtnl_link_ops->dellink(vlan->dev, &list_kill);
1839226bd341SEric Dumazet 		unregister_netdevice_many(&list_kill);
1840b863ceb7SPatrick McHardy 		break;
18411c01fe14SJiri Pirko 	case NETDEV_PRE_TYPE_CHANGE:
184226d3f69cSZheng Yongjun 		/* Forbid underlying device to change its type. */
18431c01fe14SJiri Pirko 		return NOTIFY_BAD;
18444c991255SVlad Yasevich 
18454c991255SVlad Yasevich 	case NETDEV_NOTIFY_PEERS:
18464c991255SVlad Yasevich 	case NETDEV_BONDING_FAILOVER:
18474c991255SVlad Yasevich 	case NETDEV_RESEND_IGMP:
18484c991255SVlad Yasevich 		/* Propagate to all vlans */
18494c991255SVlad Yasevich 		list_for_each_entry(vlan, &port->vlans, list)
18504c991255SVlad Yasevich 			call_netdevice_notifiers(event, vlan->dev);
1851b863ceb7SPatrick McHardy 	}
1852b863ceb7SPatrick McHardy 	return NOTIFY_DONE;
1853b863ceb7SPatrick McHardy }
1854b863ceb7SPatrick McHardy 
1855b863ceb7SPatrick McHardy static struct notifier_block macvlan_notifier_block __read_mostly = {
1856b863ceb7SPatrick McHardy 	.notifier_call	= macvlan_device_event,
1857b863ceb7SPatrick McHardy };
1858b863ceb7SPatrick McHardy 
macvlan_init_module(void)1859b863ceb7SPatrick McHardy static int __init macvlan_init_module(void)
1860b863ceb7SPatrick McHardy {
1861b863ceb7SPatrick McHardy 	int err;
1862b863ceb7SPatrick McHardy 
1863b863ceb7SPatrick McHardy 	register_netdevice_notifier(&macvlan_notifier_block);
1864b863ceb7SPatrick McHardy 
1865fc0663d6SArnd Bergmann 	err = macvlan_link_register(&macvlan_link_ops);
1866b863ceb7SPatrick McHardy 	if (err < 0)
1867b863ceb7SPatrick McHardy 		goto err1;
1868b863ceb7SPatrick McHardy 	return 0;
1869b863ceb7SPatrick McHardy err1:
1870b863ceb7SPatrick McHardy 	unregister_netdevice_notifier(&macvlan_notifier_block);
1871b863ceb7SPatrick McHardy 	return err;
1872b863ceb7SPatrick McHardy }
1873b863ceb7SPatrick McHardy 
macvlan_cleanup_module(void)1874b863ceb7SPatrick McHardy static void __exit macvlan_cleanup_module(void)
1875b863ceb7SPatrick McHardy {
1876b863ceb7SPatrick McHardy 	rtnl_link_unregister(&macvlan_link_ops);
1877b863ceb7SPatrick McHardy 	unregister_netdevice_notifier(&macvlan_notifier_block);
1878b863ceb7SPatrick McHardy }
1879b863ceb7SPatrick McHardy 
1880b863ceb7SPatrick McHardy module_init(macvlan_init_module);
1881b863ceb7SPatrick McHardy module_exit(macvlan_cleanup_module);
1882b863ceb7SPatrick McHardy 
1883b863ceb7SPatrick McHardy MODULE_LICENSE("GPL");
1884b863ceb7SPatrick McHardy MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
1885b863ceb7SPatrick McHardy MODULE_DESCRIPTION("Driver for MAC address based VLANs");
1886b863ceb7SPatrick McHardy MODULE_ALIAS_RTNL_LINK("macvlan");
1887