xref: /linux/drivers/net/macvlan.c (revision 8bc7823ed3bd5b87765e1b3d6f72c69624680921)
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;
5043c2d578SVlad Yasevich 	u32			flags;
515e3c516bSDavid S. Miller 	int			count;
5279cf79abSMichael Braun 	struct hlist_head	vlan_source_hash[MACVLAN_HASH_SIZE];
539c127a01SHerbert Xu 	DECLARE_BITMAP(mc_filter, MACVLAN_MC_FILTER_SZ);
5418c8c54dSVlad Yasevich 	unsigned char           perm_addr[ETH_ALEN];
5579cf79abSMichael Braun };
5679cf79abSMichael Braun 
5779cf79abSMichael Braun struct macvlan_source_entry {
5879cf79abSMichael Braun 	struct hlist_node	hlist;
5979cf79abSMichael Braun 	struct macvlan_dev	*vlan;
6079cf79abSMichael Braun 	unsigned char		addr[6+2] __aligned(sizeof(u16));
6179cf79abSMichael Braun 	struct rcu_head		rcu;
62b863ceb7SPatrick McHardy };
63b863ceb7SPatrick McHardy 
64412ca155SHerbert Xu struct macvlan_skb_cb {
65412ca155SHerbert Xu 	const struct macvlan_dev *src;
66412ca155SHerbert Xu };
67412ca155SHerbert Xu 
68412ca155SHerbert Xu #define MACVLAN_SKB_CB(__skb) ((struct macvlan_skb_cb *)&((__skb)->cb[0]))
69412ca155SHerbert Xu 
70d5cd9244SEric W. Biederman static void macvlan_port_destroy(struct net_device *dev);
71d4bff72cSThomas Karlsson static void update_port_bc_queue_len(struct macvlan_port *port);
72d5cd9244SEric W. Biederman 
7343c2d578SVlad Yasevich static inline bool macvlan_passthru(const struct macvlan_port *port)
7443c2d578SVlad Yasevich {
7543c2d578SVlad Yasevich 	return port->flags & MACVLAN_F_PASSTHRU;
7643c2d578SVlad Yasevich }
7743c2d578SVlad Yasevich 
7843c2d578SVlad Yasevich static inline void macvlan_set_passthru(struct macvlan_port *port)
7943c2d578SVlad Yasevich {
8043c2d578SVlad Yasevich 	port->flags |= MACVLAN_F_PASSTHRU;
8143c2d578SVlad Yasevich }
8243c2d578SVlad Yasevich 
8318c8c54dSVlad Yasevich static inline bool macvlan_addr_change(const struct macvlan_port *port)
8418c8c54dSVlad Yasevich {
8518c8c54dSVlad Yasevich 	return port->flags & MACVLAN_F_ADDRCHANGE;
8618c8c54dSVlad Yasevich }
8718c8c54dSVlad Yasevich 
8818c8c54dSVlad Yasevich static inline void macvlan_set_addr_change(struct macvlan_port *port)
8918c8c54dSVlad Yasevich {
9018c8c54dSVlad Yasevich 	port->flags |= MACVLAN_F_ADDRCHANGE;
9118c8c54dSVlad Yasevich }
9218c8c54dSVlad Yasevich 
9318c8c54dSVlad Yasevich static inline void macvlan_clear_addr_change(struct macvlan_port *port)
9418c8c54dSVlad Yasevich {
9518c8c54dSVlad Yasevich 	port->flags &= ~MACVLAN_F_ADDRCHANGE;
9618c8c54dSVlad Yasevich }
9718c8c54dSVlad Yasevich 
9879cf79abSMichael Braun /* Hash Ethernet address */
9979cf79abSMichael Braun static u32 macvlan_eth_hash(const unsigned char *addr)
10079cf79abSMichael Braun {
10179cf79abSMichael Braun 	u64 value = get_unaligned((u64 *)addr);
10279cf79abSMichael Braun 
10379cf79abSMichael Braun 	/* only want 6 bytes */
10479cf79abSMichael Braun #ifdef __BIG_ENDIAN
10579cf79abSMichael Braun 	value >>= 16;
10679cf79abSMichael Braun #else
10779cf79abSMichael Braun 	value <<= 16;
10879cf79abSMichael Braun #endif
10979cf79abSMichael Braun 	return hash_64(value, MACVLAN_HASH_BITS);
11079cf79abSMichael Braun }
11179cf79abSMichael Braun 
112e052f7e6SEric Dumazet static struct macvlan_port *macvlan_port_get_rcu(const struct net_device *dev)
113e052f7e6SEric Dumazet {
114e052f7e6SEric Dumazet 	return rcu_dereference(dev->rx_handler_data);
115e052f7e6SEric Dumazet }
116e052f7e6SEric Dumazet 
117e052f7e6SEric Dumazet static struct macvlan_port *macvlan_port_get_rtnl(const struct net_device *dev)
118e052f7e6SEric Dumazet {
119e052f7e6SEric Dumazet 	return rtnl_dereference(dev->rx_handler_data);
120e052f7e6SEric Dumazet }
121e052f7e6SEric Dumazet 
122b863ceb7SPatrick McHardy static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
123b863ceb7SPatrick McHardy 					       const unsigned char *addr)
124b863ceb7SPatrick McHardy {
125b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan;
12679cf79abSMichael Braun 	u32 idx = macvlan_eth_hash(addr);
127b863ceb7SPatrick McHardy 
12858e64a31SWei Yongjun 	hlist_for_each_entry_rcu(vlan, &port->vlan_hash[idx], hlist,
12958e64a31SWei Yongjun 				 lockdep_rtnl_is_held()) {
130a6700db1SJoe Perches 		if (ether_addr_equal_64bits(vlan->dev->dev_addr, addr))
131b863ceb7SPatrick McHardy 			return vlan;
132b863ceb7SPatrick McHardy 	}
133b863ceb7SPatrick McHardy 	return NULL;
134b863ceb7SPatrick McHardy }
135b863ceb7SPatrick McHardy 
13679cf79abSMichael Braun static struct macvlan_source_entry *macvlan_hash_lookup_source(
13779cf79abSMichael Braun 	const struct macvlan_dev *vlan,
13879cf79abSMichael Braun 	const unsigned char *addr)
13979cf79abSMichael Braun {
14079cf79abSMichael Braun 	struct macvlan_source_entry *entry;
14179cf79abSMichael Braun 	u32 idx = macvlan_eth_hash(addr);
14279cf79abSMichael Braun 	struct hlist_head *h = &vlan->port->vlan_source_hash[idx];
14379cf79abSMichael Braun 
14479cf79abSMichael Braun 	hlist_for_each_entry_rcu(entry, h, hlist) {
14579cf79abSMichael Braun 		if (ether_addr_equal_64bits(entry->addr, addr) &&
14679cf79abSMichael Braun 		    entry->vlan == vlan)
14779cf79abSMichael Braun 			return entry;
14879cf79abSMichael Braun 	}
14979cf79abSMichael Braun 	return NULL;
15079cf79abSMichael Braun }
15179cf79abSMichael Braun 
15279cf79abSMichael Braun static int macvlan_hash_add_source(struct macvlan_dev *vlan,
15379cf79abSMichael Braun 				   const unsigned char *addr)
15479cf79abSMichael Braun {
15579cf79abSMichael Braun 	struct macvlan_port *port = vlan->port;
15679cf79abSMichael Braun 	struct macvlan_source_entry *entry;
15779cf79abSMichael Braun 	struct hlist_head *h;
15879cf79abSMichael Braun 
15979cf79abSMichael Braun 	entry = macvlan_hash_lookup_source(vlan, addr);
16079cf79abSMichael Braun 	if (entry)
16179cf79abSMichael Braun 		return 0;
16279cf79abSMichael Braun 
16379cf79abSMichael Braun 	entry = kmalloc(sizeof(*entry), GFP_KERNEL);
16479cf79abSMichael Braun 	if (!entry)
16579cf79abSMichael Braun 		return -ENOMEM;
16679cf79abSMichael Braun 
16779cf79abSMichael Braun 	ether_addr_copy(entry->addr, addr);
16879cf79abSMichael Braun 	entry->vlan = vlan;
16979cf79abSMichael Braun 	h = &port->vlan_source_hash[macvlan_eth_hash(addr)];
17079cf79abSMichael Braun 	hlist_add_head_rcu(&entry->hlist, h);
17179cf79abSMichael Braun 	vlan->macaddr_count++;
17279cf79abSMichael Braun 
17379cf79abSMichael Braun 	return 0;
17479cf79abSMichael Braun }
17579cf79abSMichael Braun 
176f9ac30f0SEric Biederman static void macvlan_hash_add(struct macvlan_dev *vlan)
177f9ac30f0SEric Biederman {
178f9ac30f0SEric Biederman 	struct macvlan_port *port = vlan->port;
179f9ac30f0SEric Biederman 	const unsigned char *addr = vlan->dev->dev_addr;
18079cf79abSMichael Braun 	u32 idx = macvlan_eth_hash(addr);
181f9ac30f0SEric Biederman 
18279cf79abSMichael Braun 	hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[idx]);
18379cf79abSMichael Braun }
18479cf79abSMichael Braun 
18579cf79abSMichael Braun static void macvlan_hash_del_source(struct macvlan_source_entry *entry)
18679cf79abSMichael Braun {
18779cf79abSMichael Braun 	hlist_del_rcu(&entry->hlist);
18879cf79abSMichael Braun 	kfree_rcu(entry, rcu);
189f9ac30f0SEric Biederman }
190f9ac30f0SEric Biederman 
191449f4544SEric Dumazet static void macvlan_hash_del(struct macvlan_dev *vlan, bool sync)
192f9ac30f0SEric Biederman {
193f9ac30f0SEric Biederman 	hlist_del_rcu(&vlan->hlist);
194449f4544SEric Dumazet 	if (sync)
195f9ac30f0SEric Biederman 		synchronize_rcu();
196f9ac30f0SEric Biederman }
197f9ac30f0SEric Biederman 
198f9ac30f0SEric Biederman static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
199f9ac30f0SEric Biederman 					const unsigned char *addr)
200f9ac30f0SEric Biederman {
201449f4544SEric Dumazet 	macvlan_hash_del(vlan, true);
202f9ac30f0SEric Biederman 	/* Now that we are unhashed it is safe to change the device
203f9ac30f0SEric Biederman 	 * address without confusing packet delivery.
204f9ac30f0SEric Biederman 	 */
2052f23e5ceSJakub Kicinski 	eth_hw_addr_set(vlan->dev, addr);
206f9ac30f0SEric Biederman 	macvlan_hash_add(vlan);
207f9ac30f0SEric Biederman }
208f9ac30f0SEric Biederman 
209d94d0254SGao Feng static bool macvlan_addr_busy(const struct macvlan_port *port,
210f9ac30f0SEric Biederman 			      const unsigned char *addr)
211f9ac30f0SEric Biederman {
21218c8c54dSVlad Yasevich 	/* Test to see if the specified address is
213f9ac30f0SEric Biederman 	 * currently in use by the underlying device or
214f9ac30f0SEric Biederman 	 * another macvlan.
215f9ac30f0SEric Biederman 	 */
21618c8c54dSVlad Yasevich 	if (!macvlan_passthru(port) && !macvlan_addr_change(port) &&
217e696cda7SVlad Yasevich 	    ether_addr_equal_64bits(port->dev->dev_addr, addr))
218d94d0254SGao Feng 		return true;
219f9ac30f0SEric Biederman 
220f9ac30f0SEric Biederman 	if (macvlan_hash_lookup(port, addr))
221d94d0254SGao Feng 		return true;
222f9ac30f0SEric Biederman 
223d94d0254SGao Feng 	return false;
224f9ac30f0SEric Biederman }
225f9ac30f0SEric Biederman 
226a1e514c5SArnd Bergmann 
227fc0663d6SArnd Bergmann static int macvlan_broadcast_one(struct sk_buff *skb,
228fc0663d6SArnd Bergmann 				 const struct macvlan_dev *vlan,
229618e1b74SArnd Bergmann 				 const struct ethhdr *eth, bool local)
230a1e514c5SArnd Bergmann {
231fc0663d6SArnd Bergmann 	struct net_device *dev = vlan->dev;
232a1e514c5SArnd Bergmann 
233618e1b74SArnd Bergmann 	if (local)
234412ca155SHerbert Xu 		return __dev_forward_skb(dev, skb);
235618e1b74SArnd Bergmann 
236a1e514c5SArnd Bergmann 	skb->dev = dev;
237a6700db1SJoe Perches 	if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast))
238a1e514c5SArnd Bergmann 		skb->pkt_type = PACKET_BROADCAST;
239a1e514c5SArnd Bergmann 	else
240a1e514c5SArnd Bergmann 		skb->pkt_type = PACKET_MULTICAST;
241a1e514c5SArnd Bergmann 
242412ca155SHerbert Xu 	return 0;
243a1e514c5SArnd Bergmann }
244a1e514c5SArnd Bergmann 
2453807ff58SEric Dumazet static u32 macvlan_hash_mix(const struct macvlan_dev *vlan)
2463807ff58SEric Dumazet {
2473807ff58SEric Dumazet 	return (u32)(((unsigned long)vlan) >> L1_CACHE_SHIFT);
2483807ff58SEric Dumazet }
2493807ff58SEric Dumazet 
2503807ff58SEric Dumazet 
2513807ff58SEric Dumazet static unsigned int mc_hash(const struct macvlan_dev *vlan,
2523807ff58SEric Dumazet 			    const unsigned char *addr)
253cd431e73SEric Dumazet {
254cd431e73SEric Dumazet 	u32 val = __get_unaligned_cpu32(addr + 2);
255cd431e73SEric Dumazet 
2563807ff58SEric Dumazet 	val ^= macvlan_hash_mix(vlan);
257cd431e73SEric Dumazet 	return hash_32(val, MACVLAN_MC_FILTER_BITS);
258cd431e73SEric Dumazet }
259cd431e73SEric Dumazet 
260b863ceb7SPatrick McHardy static void macvlan_broadcast(struct sk_buff *skb,
261618e1b74SArnd Bergmann 			      const struct macvlan_port *port,
262618e1b74SArnd Bergmann 			      struct net_device *src,
263618e1b74SArnd Bergmann 			      enum macvlan_mode mode)
264b863ceb7SPatrick McHardy {
2651712b2ffSEric Dumazet 	const struct ethhdr *eth = eth_hdr(skb);
266b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan;
267b863ceb7SPatrick McHardy 	struct sk_buff *nskb;
268b863ceb7SPatrick McHardy 	unsigned int i;
269a1e514c5SArnd Bergmann 	int err;
2703807ff58SEric Dumazet 	unsigned int hash;
271b863ceb7SPatrick McHardy 
272efbbced3SPatrick McHardy 	if (skb->protocol == htons(ETH_P_PAUSE))
273efbbced3SPatrick McHardy 		return;
274efbbced3SPatrick McHardy 
275bb23ffa1SChristophe JAILLET 	hash_for_each_rcu(port->vlan_hash, i, vlan, hlist) {
276618e1b74SArnd Bergmann 		if (vlan->dev == src || !(vlan->mode & mode))
277618e1b74SArnd Bergmann 			continue;
278618e1b74SArnd Bergmann 
2793807ff58SEric Dumazet 		hash = mc_hash(vlan, eth->h_dest);
280cd431e73SEric Dumazet 		if (!test_bit(hash, vlan->mc_filter))
281cd431e73SEric Dumazet 			continue;
282de9e8f3fSHerbert Xu 
283de9e8f3fSHerbert Xu 		err = NET_RX_DROP;
284b863ceb7SPatrick McHardy 		nskb = skb_clone(skb, GFP_ATOMIC);
285de9e8f3fSHerbert Xu 		if (likely(nskb))
286bb23ffa1SChristophe JAILLET 			err = macvlan_broadcast_one(nskb, vlan, eth,
287412ca155SHerbert Xu 					mode == MACVLAN_MODE_BRIDGE) ?:
288412ca155SHerbert Xu 			      netif_rx_ni(nskb);
289a1e514c5SArnd Bergmann 		macvlan_count_rx(vlan, skb->len + ETH_HLEN,
2904c979935Sjbaron@akamai.com 				 err == NET_RX_SUCCESS, true);
291b863ceb7SPatrick McHardy 	}
292b863ceb7SPatrick McHardy }
293b863ceb7SPatrick McHardy 
294412ca155SHerbert Xu static void macvlan_process_broadcast(struct work_struct *w)
295412ca155SHerbert Xu {
296412ca155SHerbert Xu 	struct macvlan_port *port = container_of(w, struct macvlan_port,
297412ca155SHerbert Xu 						 bc_work);
298412ca155SHerbert Xu 	struct sk_buff *skb;
299412ca155SHerbert Xu 	struct sk_buff_head list;
300412ca155SHerbert Xu 
301fe0ca732SEric Dumazet 	__skb_queue_head_init(&list);
302412ca155SHerbert Xu 
303412ca155SHerbert Xu 	spin_lock_bh(&port->bc_queue.lock);
304412ca155SHerbert Xu 	skb_queue_splice_tail_init(&port->bc_queue, &list);
305412ca155SHerbert Xu 	spin_unlock_bh(&port->bc_queue.lock);
306412ca155SHerbert Xu 
307412ca155SHerbert Xu 	while ((skb = __skb_dequeue(&list))) {
308412ca155SHerbert Xu 		const struct macvlan_dev *src = MACVLAN_SKB_CB(skb)->src;
309412ca155SHerbert Xu 
310412ca155SHerbert Xu 		rcu_read_lock();
311412ca155SHerbert Xu 
312412ca155SHerbert Xu 		if (!src)
313412ca155SHerbert Xu 			/* frame comes from an external address */
314412ca155SHerbert Xu 			macvlan_broadcast(skb, port, NULL,
315412ca155SHerbert Xu 					  MACVLAN_MODE_PRIVATE |
316412ca155SHerbert Xu 					  MACVLAN_MODE_VEPA    |
317412ca155SHerbert Xu 					  MACVLAN_MODE_PASSTHRU|
318412ca155SHerbert Xu 					  MACVLAN_MODE_BRIDGE);
319412ca155SHerbert Xu 		else if (src->mode == MACVLAN_MODE_VEPA)
320412ca155SHerbert Xu 			/* flood to everyone except source */
321412ca155SHerbert Xu 			macvlan_broadcast(skb, port, src->dev,
322412ca155SHerbert Xu 					  MACVLAN_MODE_VEPA |
323412ca155SHerbert Xu 					  MACVLAN_MODE_BRIDGE);
324412ca155SHerbert Xu 		else
325412ca155SHerbert Xu 			/*
326412ca155SHerbert Xu 			 * flood only to VEPA ports, bridge ports
327412ca155SHerbert Xu 			 * already saw the frame on the way out.
328412ca155SHerbert Xu 			 */
329412ca155SHerbert Xu 			macvlan_broadcast(skb, port, src->dev,
330412ca155SHerbert Xu 					  MACVLAN_MODE_VEPA);
331412ca155SHerbert Xu 
332412ca155SHerbert Xu 		rcu_read_unlock();
333412ca155SHerbert Xu 
334260916dfSHerbert Xu 		if (src)
335260916dfSHerbert Xu 			dev_put(src->dev);
336bf97403aSYang Wei 		consume_skb(skb);
337ce9a4186SMahesh Bandewar 
338ce9a4186SMahesh Bandewar 		cond_resched();
339412ca155SHerbert Xu 	}
340412ca155SHerbert Xu }
341412ca155SHerbert Xu 
342412ca155SHerbert Xu static void macvlan_broadcast_enqueue(struct macvlan_port *port,
343260916dfSHerbert Xu 				      const struct macvlan_dev *src,
344412ca155SHerbert Xu 				      struct sk_buff *skb)
345412ca155SHerbert Xu {
346e676f197SHerbert Xu 	struct sk_buff *nskb;
347412ca155SHerbert Xu 	int err = -ENOMEM;
348412ca155SHerbert Xu 
349e676f197SHerbert Xu 	nskb = skb_clone(skb, GFP_ATOMIC);
350e676f197SHerbert Xu 	if (!nskb)
351412ca155SHerbert Xu 		goto err;
352412ca155SHerbert Xu 
353260916dfSHerbert Xu 	MACVLAN_SKB_CB(nskb)->src = src;
354260916dfSHerbert Xu 
355412ca155SHerbert Xu 	spin_lock(&port->bc_queue.lock);
356d4bff72cSThomas Karlsson 	if (skb_queue_len(&port->bc_queue) < port->bc_queue_len_used) {
357260916dfSHerbert Xu 		if (src)
358260916dfSHerbert Xu 			dev_hold(src->dev);
359e676f197SHerbert Xu 		__skb_queue_tail(&port->bc_queue, nskb);
360412ca155SHerbert Xu 		err = 0;
361412ca155SHerbert Xu 	}
362412ca155SHerbert Xu 	spin_unlock(&port->bc_queue.lock);
363412ca155SHerbert Xu 
3641d7ea556SMenglong Dong 	schedule_work(&port->bc_work);
3651d7ea556SMenglong Dong 
366412ca155SHerbert Xu 	if (err)
367e676f197SHerbert Xu 		goto free_nskb;
368412ca155SHerbert Xu 
369412ca155SHerbert Xu 	return;
370412ca155SHerbert Xu 
371e676f197SHerbert Xu free_nskb:
372e676f197SHerbert Xu 	kfree_skb(nskb);
373412ca155SHerbert Xu err:
374412ca155SHerbert Xu 	atomic_long_inc(&skb->dev->rx_dropped);
375412ca155SHerbert Xu }
376412ca155SHerbert Xu 
37779cf79abSMichael Braun static void macvlan_flush_sources(struct macvlan_port *port,
37879cf79abSMichael Braun 				  struct macvlan_dev *vlan)
37979cf79abSMichael Braun {
380bb23ffa1SChristophe JAILLET 	struct macvlan_source_entry *entry;
381bb23ffa1SChristophe JAILLET 	struct hlist_node *next;
38279cf79abSMichael Braun 	int i;
38379cf79abSMichael Braun 
384bb23ffa1SChristophe JAILLET 	hash_for_each_safe(port->vlan_source_hash, i, next, entry, hlist)
38579cf79abSMichael Braun 		if (entry->vlan == vlan)
38679cf79abSMichael Braun 			macvlan_hash_del_source(entry);
387bb23ffa1SChristophe JAILLET 
38879cf79abSMichael Braun 	vlan->macaddr_count = 0;
38979cf79abSMichael Braun }
39079cf79abSMichael Braun 
39179cf79abSMichael Braun static void macvlan_forward_source_one(struct sk_buff *skb,
39279cf79abSMichael Braun 				       struct macvlan_dev *vlan)
39379cf79abSMichael Braun {
39479cf79abSMichael Braun 	struct sk_buff *nskb;
39579cf79abSMichael Braun 	struct net_device *dev;
39679cf79abSMichael Braun 	int len;
39779cf79abSMichael Braun 	int ret;
39879cf79abSMichael Braun 
39979cf79abSMichael Braun 	dev = vlan->dev;
40079cf79abSMichael Braun 	if (unlikely(!(dev->flags & IFF_UP)))
40179cf79abSMichael Braun 		return;
40279cf79abSMichael Braun 
40379cf79abSMichael Braun 	nskb = skb_clone(skb, GFP_ATOMIC);
40479cf79abSMichael Braun 	if (!nskb)
40579cf79abSMichael Braun 		return;
40679cf79abSMichael Braun 
40779cf79abSMichael Braun 	len = nskb->len + ETH_HLEN;
40879cf79abSMichael Braun 	nskb->dev = dev;
409c8c41ea1SAlexander Duyck 
410c8c41ea1SAlexander Duyck 	if (ether_addr_equal_64bits(eth_hdr(skb)->h_dest, dev->dev_addr))
41179cf79abSMichael Braun 		nskb->pkt_type = PACKET_HOST;
41279cf79abSMichael Braun 
41379cf79abSMichael Braun 	ret = netif_rx(nskb);
4144c979935Sjbaron@akamai.com 	macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, false);
41579cf79abSMichael Braun }
41679cf79abSMichael Braun 
417427f0c8cSJethro Beekman static bool macvlan_forward_source(struct sk_buff *skb,
41879cf79abSMichael Braun 				   struct macvlan_port *port,
41979cf79abSMichael Braun 				   const unsigned char *addr)
42079cf79abSMichael Braun {
42179cf79abSMichael Braun 	struct macvlan_source_entry *entry;
42279cf79abSMichael Braun 	u32 idx = macvlan_eth_hash(addr);
42379cf79abSMichael Braun 	struct hlist_head *h = &port->vlan_source_hash[idx];
424427f0c8cSJethro Beekman 	bool consume = false;
42579cf79abSMichael Braun 
42679cf79abSMichael Braun 	hlist_for_each_entry_rcu(entry, h, hlist) {
427427f0c8cSJethro Beekman 		if (ether_addr_equal_64bits(entry->addr, addr)) {
428427f0c8cSJethro Beekman 			if (entry->vlan->flags & MACVLAN_FLAG_NODST)
429427f0c8cSJethro Beekman 				consume = true;
43079cf79abSMichael Braun 			macvlan_forward_source_one(skb, entry->vlan);
43179cf79abSMichael Braun 		}
43279cf79abSMichael Braun 	}
43379cf79abSMichael Braun 
434427f0c8cSJethro Beekman 	return consume;
435427f0c8cSJethro Beekman }
436427f0c8cSJethro Beekman 
437b863ceb7SPatrick McHardy /* called under rcu_read_lock() from netif_receive_skb */
4388a4eb573SJiri Pirko static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb)
439b863ceb7SPatrick McHardy {
440ab95bfe0SJiri Pirko 	struct macvlan_port *port;
4418a4eb573SJiri Pirko 	struct sk_buff *skb = *pskb;
442b863ceb7SPatrick McHardy 	const struct ethhdr *eth = eth_hdr(skb);
443b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan;
444618e1b74SArnd Bergmann 	const struct macvlan_dev *src;
445b863ceb7SPatrick McHardy 	struct net_device *dev;
446ba01877fSSridhar Samudrala 	unsigned int len = 0;
447d1dd9119Sjbaron@akamai.com 	int ret;
448d1dd9119Sjbaron@akamai.com 	rx_handler_result_t handle_res;
449b863ceb7SPatrick McHardy 
45081f3dc93SAlexander Sverdlin 	/* Packets from dev_loopback_xmit() do not have L2 header, bail out */
45181f3dc93SAlexander Sverdlin 	if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
45281f3dc93SAlexander Sverdlin 		return RX_HANDLER_PASS;
45381f3dc93SAlexander Sverdlin 
454a35e2c1bSJiri Pirko 	port = macvlan_port_get_rcu(skb->dev);
455b863ceb7SPatrick McHardy 	if (is_multicast_ether_addr(eth->h_dest)) {
4569c127a01SHerbert Xu 		unsigned int hash;
4579c127a01SHerbert Xu 
45819bcf9f2SEric W. Biederman 		skb = ip_check_defrag(dev_net(skb->dev), skb, IP_DEFRAG_MACVLAN);
459bc416d97SEric Dumazet 		if (!skb)
460bc416d97SEric Dumazet 			return RX_HANDLER_CONSUMED;
461e639b8d8SSabrina Dubroca 		*pskb = skb;
4624ec7ac12SEric Dumazet 		eth = eth_hdr(skb);
463427f0c8cSJethro Beekman 		if (macvlan_forward_source(skb, port, eth->h_source))
464427f0c8cSJethro Beekman 			return RX_HANDLER_CONSUMED;
465618e1b74SArnd Bergmann 		src = macvlan_hash_lookup(port, eth->h_source);
466412ca155SHerbert Xu 		if (src && src->mode != MACVLAN_MODE_VEPA &&
467412ca155SHerbert Xu 		    src->mode != MACVLAN_MODE_BRIDGE) {
468729e72a1Sstephen hemminger 			/* forward to original port. */
469729e72a1Sstephen hemminger 			vlan = src;
470412ca155SHerbert Xu 			ret = macvlan_broadcast_one(skb, vlan, eth, 0) ?:
471412ca155SHerbert Xu 			      netif_rx(skb);
472d1dd9119Sjbaron@akamai.com 			handle_res = RX_HANDLER_CONSUMED;
473729e72a1Sstephen hemminger 			goto out;
474729e72a1Sstephen hemminger 		}
475729e72a1Sstephen hemminger 
4769c127a01SHerbert Xu 		hash = mc_hash(NULL, eth->h_dest);
4779c127a01SHerbert Xu 		if (test_bit(hash, port->mc_filter))
478260916dfSHerbert Xu 			macvlan_broadcast_enqueue(port, src, skb);
479412ca155SHerbert Xu 
4808a4eb573SJiri Pirko 		return RX_HANDLER_PASS;
481b863ceb7SPatrick McHardy 	}
482b863ceb7SPatrick McHardy 
483427f0c8cSJethro Beekman 	if (macvlan_forward_source(skb, port, eth->h_source))
484427f0c8cSJethro Beekman 		return RX_HANDLER_CONSUMED;
48543c2d578SVlad Yasevich 	if (macvlan_passthru(port))
486233c7df0SJiri Pirko 		vlan = list_first_or_null_rcu(&port->vlans,
487233c7df0SJiri Pirko 					      struct macvlan_dev, list);
488eb06acdcSSridhar Samudrala 	else
489b863ceb7SPatrick McHardy 		vlan = macvlan_hash_lookup(port, eth->h_dest);
490dd6b9c2cSAlexander Duyck 	if (!vlan || vlan->mode == MACVLAN_MODE_SOURCE)
4918a4eb573SJiri Pirko 		return RX_HANDLER_PASS;
492b863ceb7SPatrick McHardy 
493b863ceb7SPatrick McHardy 	dev = vlan->dev;
494b863ceb7SPatrick McHardy 	if (unlikely(!(dev->flags & IFF_UP))) {
495b863ceb7SPatrick McHardy 		kfree_skb(skb);
4968a4eb573SJiri Pirko 		return RX_HANDLER_CONSUMED;
497b863ceb7SPatrick McHardy 	}
498a1e514c5SArnd Bergmann 	len = skb->len + ETH_HLEN;
499b863ceb7SPatrick McHardy 	skb = skb_share_check(skb, GFP_ATOMIC);
500d1dd9119Sjbaron@akamai.com 	if (!skb) {
501d1dd9119Sjbaron@akamai.com 		ret = NET_RX_DROP;
502d1dd9119Sjbaron@akamai.com 		handle_res = RX_HANDLER_CONSUMED;
503ba01877fSSridhar Samudrala 		goto out;
504d1dd9119Sjbaron@akamai.com 	}
505b863ceb7SPatrick McHardy 
506e639b8d8SSabrina Dubroca 	*pskb = skb;
507b863ceb7SPatrick McHardy 	skb->dev = dev;
508b863ceb7SPatrick McHardy 	skb->pkt_type = PACKET_HOST;
509b863ceb7SPatrick McHardy 
510d1dd9119Sjbaron@akamai.com 	ret = NET_RX_SUCCESS;
511d1dd9119Sjbaron@akamai.com 	handle_res = RX_HANDLER_ANOTHER;
512ba01877fSSridhar Samudrala out:
5134c979935Sjbaron@akamai.com 	macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, false);
514d1dd9119Sjbaron@akamai.com 	return handle_res;
515b863ceb7SPatrick McHardy }
516b863ceb7SPatrick McHardy 
517618e1b74SArnd Bergmann static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
518618e1b74SArnd Bergmann {
519618e1b74SArnd Bergmann 	const struct macvlan_dev *vlan = netdev_priv(dev);
520618e1b74SArnd Bergmann 	const struct macvlan_port *port = vlan->port;
521618e1b74SArnd Bergmann 	const struct macvlan_dev *dest;
522618e1b74SArnd Bergmann 
523618e1b74SArnd Bergmann 	if (vlan->mode == MACVLAN_MODE_BRIDGE) {
5241712b2ffSEric Dumazet 		const struct ethhdr *eth = skb_eth_hdr(skb);
525618e1b74SArnd Bergmann 
526618e1b74SArnd Bergmann 		/* send to other bridge ports directly */
527618e1b74SArnd Bergmann 		if (is_multicast_ether_addr(eth->h_dest)) {
5281712b2ffSEric Dumazet 			skb_reset_mac_header(skb);
529618e1b74SArnd Bergmann 			macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE);
530618e1b74SArnd Bergmann 			goto xmit_world;
531618e1b74SArnd Bergmann 		}
532618e1b74SArnd Bergmann 
533618e1b74SArnd Bergmann 		dest = macvlan_hash_lookup(port, eth->h_dest);
534618e1b74SArnd Bergmann 		if (dest && dest->mode == MACVLAN_MODE_BRIDGE) {
535a37dd333SDavid Ward 			/* send to lowerdev first for its network taps */
536cb2d0f3eSDavid Ward 			dev_forward_skb(vlan->lowerdev, skb);
537618e1b74SArnd Bergmann 
538618e1b74SArnd Bergmann 			return NET_XMIT_SUCCESS;
539618e1b74SArnd Bergmann 		}
540618e1b74SArnd Bergmann 	}
541618e1b74SArnd Bergmann xmit_world:
54259b9997bSDavid S. Miller 	skb->dev = vlan->lowerdev;
543eadec877SAlexander Duyck 	return dev_queue_xmit_accel(skb,
544eadec877SAlexander Duyck 				    netdev_get_sb_channel(dev) ? dev : NULL);
545618e1b74SArnd Bergmann }
546618e1b74SArnd Bergmann 
547688cea83Sdingtianhong static inline netdev_tx_t macvlan_netpoll_send_skb(struct macvlan_dev *vlan, struct sk_buff *skb)
548688cea83Sdingtianhong {
549688cea83Sdingtianhong #ifdef CONFIG_NET_POLL_CONTROLLER
550f78ed220SEric Dumazet 	return netpoll_send_skb(vlan->netpoll, skb);
551688cea83Sdingtianhong #else
552688cea83Sdingtianhong 	BUG();
553688cea83Sdingtianhong 	return NETDEV_TX_OK;
554f78ed220SEric Dumazet #endif
555688cea83Sdingtianhong }
556688cea83Sdingtianhong 
5570db901bdSstephen hemminger static netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
558424efe9cSStephen Hemminger 				      struct net_device *dev)
559b863ceb7SPatrick McHardy {
56081d4e91cSAlexander Duyck 	struct macvlan_dev *vlan = netdev_priv(dev);
561b863ceb7SPatrick McHardy 	unsigned int len = skb->len;
562b863ceb7SPatrick McHardy 	int ret;
563688cea83Sdingtianhong 
564688cea83Sdingtianhong 	if (unlikely(netpoll_tx_running(dev)))
565688cea83Sdingtianhong 		return macvlan_netpoll_send_skb(vlan, skb);
566b863ceb7SPatrick McHardy 
567618e1b74SArnd Bergmann 	ret = macvlan_queue_xmit(skb, dev);
568a6cc0cfaSJohn Fastabend 
5692d6c9ffcSEric Dumazet 	if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
570cdf3e274SLi RongQing 		struct vlan_pcpu_stats *pcpu_stats;
5712c114553SEric Dumazet 
5728ffab51bSEric Dumazet 		pcpu_stats = this_cpu_ptr(vlan->pcpu_stats);
5738ffab51bSEric Dumazet 		u64_stats_update_begin(&pcpu_stats->syncp);
5748ffab51bSEric Dumazet 		pcpu_stats->tx_packets++;
5758ffab51bSEric Dumazet 		pcpu_stats->tx_bytes += len;
5768ffab51bSEric Dumazet 		u64_stats_update_end(&pcpu_stats->syncp);
5778ffab51bSEric Dumazet 	} else {
5788ffab51bSEric Dumazet 		this_cpu_inc(vlan->pcpu_stats->tx_dropped);
5798ffab51bSEric Dumazet 	}
580cbbef5e1SPatrick McHardy 	return ret;
581b863ceb7SPatrick McHardy }
582b863ceb7SPatrick McHardy 
583b863ceb7SPatrick McHardy static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
5843b04dddeSStephen Hemminger 			       unsigned short type, const void *daddr,
5853b04dddeSStephen Hemminger 			       const void *saddr, unsigned len)
586b863ceb7SPatrick McHardy {
587b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan = netdev_priv(dev);
588b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
589b863ceb7SPatrick McHardy 
5900c4e8581SStephen Hemminger 	return dev_hard_header(skb, lowerdev, type, daddr,
591b863ceb7SPatrick McHardy 			       saddr ? : dev->dev_addr, len);
592b863ceb7SPatrick McHardy }
593b863ceb7SPatrick McHardy 
5943b04dddeSStephen Hemminger static const struct header_ops macvlan_hard_header_ops = {
5953b04dddeSStephen Hemminger 	.create  	= macvlan_hard_header,
5963b04dddeSStephen Hemminger 	.parse		= eth_header_parse,
5973b04dddeSStephen Hemminger 	.cache		= eth_header_cache,
5983b04dddeSStephen Hemminger 	.cache_update	= eth_header_cache_update,
5993b04dddeSStephen Hemminger };
6003b04dddeSStephen Hemminger 
601b863ceb7SPatrick McHardy static int macvlan_open(struct net_device *dev)
602b863ceb7SPatrick McHardy {
603b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
604b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
605b863ceb7SPatrick McHardy 	int err;
606b863ceb7SPatrick McHardy 
60743c2d578SVlad Yasevich 	if (macvlan_passthru(vlan->port)) {
60878738141SMichael S. Tsirkin 		if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) {
60978738141SMichael S. Tsirkin 			err = dev_set_promiscuity(lowerdev, 1);
61078738141SMichael S. Tsirkin 			if (err < 0)
61178738141SMichael S. Tsirkin 				goto out;
61278738141SMichael S. Tsirkin 		}
613eb06acdcSSridhar Samudrala 		goto hash_add;
614eb06acdcSSridhar Samudrala 	}
615eb06acdcSSridhar Samudrala 
61659f997b0SMatteo Croce 	err = -EADDRINUSE;
6177d775f63SAlexander Duyck 	if (macvlan_addr_busy(vlan->port, dev->dev_addr))
6187d775f63SAlexander Duyck 		goto out;
6197d775f63SAlexander Duyck 
6207d775f63SAlexander Duyck 	/* Attempt to populate accel_priv which is used to offload the L2
6217d775f63SAlexander Duyck 	 * forwarding requests for unicast packets.
6227d775f63SAlexander Duyck 	 */
62381d4e91cSAlexander Duyck 	if (lowerdev->features & NETIF_F_HW_L2FW_DOFFLOAD)
6247d775f63SAlexander Duyck 		vlan->accel_priv =
625a6cc0cfaSJohn Fastabend 		      lowerdev->netdev_ops->ndo_dfwd_add_station(lowerdev, dev);
626a6cc0cfaSJohn Fastabend 
62781d4e91cSAlexander Duyck 	/* If earlier attempt to offload failed, or accel_priv is not
62881d4e91cSAlexander Duyck 	 * populated we must add the unicast address to the lower device.
629a6cc0cfaSJohn Fastabend 	 */
63081d4e91cSAlexander Duyck 	if (IS_ERR_OR_NULL(vlan->accel_priv)) {
6317d775f63SAlexander Duyck 		vlan->accel_priv = NULL;
632a748ee24SJiri Pirko 		err = dev_uc_add(lowerdev, dev->dev_addr);
633b863ceb7SPatrick McHardy 		if (err < 0)
634b89fb7daSWang Chen 			goto out;
63581d4e91cSAlexander Duyck 	}
63681d4e91cSAlexander Duyck 
637b89fb7daSWang Chen 	if (dev->flags & IFF_ALLMULTI) {
638b89fb7daSWang Chen 		err = dev_set_allmulti(lowerdev, 1);
639b89fb7daSWang Chen 		if (err < 0)
640b89fb7daSWang Chen 			goto del_unicast;
641b89fb7daSWang Chen 	}
642eb06acdcSSridhar Samudrala 
643efdbd2b3SVlad Yasevich 	if (dev->flags & IFF_PROMISC) {
644efdbd2b3SVlad Yasevich 		err = dev_set_promiscuity(lowerdev, 1);
645efdbd2b3SVlad Yasevich 		if (err < 0)
646efdbd2b3SVlad Yasevich 			goto clear_multi;
647efdbd2b3SVlad Yasevich 	}
648efdbd2b3SVlad Yasevich 
649eb06acdcSSridhar Samudrala hash_add:
650f9ac30f0SEric Biederman 	macvlan_hash_add(vlan);
651b863ceb7SPatrick McHardy 	return 0;
652b89fb7daSWang Chen 
653efdbd2b3SVlad Yasevich clear_multi:
654c3891fa2SGao Feng 	if (dev->flags & IFF_ALLMULTI)
655efdbd2b3SVlad Yasevich 		dev_set_allmulti(lowerdev, -1);
656b89fb7daSWang Chen del_unicast:
6577d775f63SAlexander Duyck 	if (vlan->accel_priv) {
658a6cc0cfaSJohn Fastabend 		lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev,
6597d775f63SAlexander Duyck 							   vlan->accel_priv);
6607d775f63SAlexander Duyck 		vlan->accel_priv = NULL;
66181d4e91cSAlexander Duyck 	} else {
66281d4e91cSAlexander Duyck 		dev_uc_del(lowerdev, dev->dev_addr);
663a6cc0cfaSJohn Fastabend 	}
66481d4e91cSAlexander Duyck out:
665b89fb7daSWang Chen 	return err;
666b863ceb7SPatrick McHardy }
667b863ceb7SPatrick McHardy 
668b863ceb7SPatrick McHardy static int macvlan_stop(struct net_device *dev)
669b863ceb7SPatrick McHardy {
670b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
671b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
672b863ceb7SPatrick McHardy 
6737d775f63SAlexander Duyck 	if (vlan->accel_priv) {
674a6cc0cfaSJohn Fastabend 		lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev,
6757d775f63SAlexander Duyck 							   vlan->accel_priv);
6767d775f63SAlexander Duyck 		vlan->accel_priv = NULL;
677a6cc0cfaSJohn Fastabend 	}
678a6cc0cfaSJohn Fastabend 
679df8ef8f3SJohn Fastabend 	dev_uc_unsync(lowerdev, dev);
680df8ef8f3SJohn Fastabend 	dev_mc_unsync(lowerdev, dev);
681df8ef8f3SJohn Fastabend 
68243c2d578SVlad Yasevich 	if (macvlan_passthru(vlan->port)) {
683df8ef8f3SJohn Fastabend 		if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC))
684eb06acdcSSridhar Samudrala 			dev_set_promiscuity(lowerdev, -1);
685eb06acdcSSridhar Samudrala 		goto hash_del;
686eb06acdcSSridhar Samudrala 	}
687eb06acdcSSridhar Samudrala 
688b863ceb7SPatrick McHardy 	if (dev->flags & IFF_ALLMULTI)
689b863ceb7SPatrick McHardy 		dev_set_allmulti(lowerdev, -1);
690b863ceb7SPatrick McHardy 
691efdbd2b3SVlad Yasevich 	if (dev->flags & IFF_PROMISC)
692efdbd2b3SVlad Yasevich 		dev_set_promiscuity(lowerdev, -1);
693efdbd2b3SVlad Yasevich 
694a748ee24SJiri Pirko 	dev_uc_del(lowerdev, dev->dev_addr);
695b863ceb7SPatrick McHardy 
696eb06acdcSSridhar Samudrala hash_del:
697449f4544SEric Dumazet 	macvlan_hash_del(vlan, !dev->dismantle);
698b863ceb7SPatrick McHardy 	return 0;
699b863ceb7SPatrick McHardy }
700b863ceb7SPatrick McHardy 
701*8bc7823eSJakub Kicinski static int macvlan_sync_address(struct net_device *dev,
702*8bc7823eSJakub Kicinski 				const unsigned char *addr)
703ad5d20a6SPatrick McHardy {
704ad5d20a6SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
705ad5d20a6SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
70618c8c54dSVlad Yasevich 	struct macvlan_port *port = vlan->port;
707ad5d20a6SPatrick McHardy 	int err;
708ad5d20a6SPatrick McHardy 
709f9ac30f0SEric Biederman 	if (!(dev->flags & IFF_UP)) {
710f9ac30f0SEric Biederman 		/* Just copy in the new address */
711e35b8d7dSJakub Kicinski 		eth_hw_addr_set(dev, addr);
712f9ac30f0SEric Biederman 	} else {
713f9ac30f0SEric Biederman 		/* Rehash and update the device filters */
714e289fd28Sdingtianhong 		if (macvlan_addr_busy(vlan->port, addr))
71559f997b0SMatteo Croce 			return -EADDRINUSE;
716ad5d20a6SPatrick McHardy 
71718c8c54dSVlad Yasevich 		if (!macvlan_passthru(port)) {
718e289fd28Sdingtianhong 			err = dev_uc_add(lowerdev, addr);
719ccffad25SJiri Pirko 			if (err)
720ad5d20a6SPatrick McHardy 				return err;
721f9ac30f0SEric Biederman 
722a748ee24SJiri Pirko 			dev_uc_del(lowerdev, dev->dev_addr);
723e289fd28Sdingtianhong 		}
724ad5d20a6SPatrick McHardy 
725e289fd28Sdingtianhong 		macvlan_hash_change_addr(vlan, addr);
726f9ac30f0SEric Biederman 	}
72718c8c54dSVlad Yasevich 	if (macvlan_passthru(port) && !macvlan_addr_change(port)) {
72818c8c54dSVlad Yasevich 		/* Since addr_change isn't set, we are here due to lower
72918c8c54dSVlad Yasevich 		 * device change.  Save the lower-dev address so we can
73018c8c54dSVlad Yasevich 		 * restore it later.
73118c8c54dSVlad Yasevich 		 */
73218c8c54dSVlad Yasevich 		ether_addr_copy(vlan->port->perm_addr,
73318c8c54dSVlad Yasevich 				lowerdev->dev_addr);
73418c8c54dSVlad Yasevich 	}
73518c8c54dSVlad Yasevich 	macvlan_clear_addr_change(port);
736ad5d20a6SPatrick McHardy 	return 0;
737ad5d20a6SPatrick McHardy }
738ad5d20a6SPatrick McHardy 
739e289fd28Sdingtianhong static int macvlan_set_mac_address(struct net_device *dev, void *p)
740e289fd28Sdingtianhong {
741e289fd28Sdingtianhong 	struct macvlan_dev *vlan = netdev_priv(dev);
742e289fd28Sdingtianhong 	struct sockaddr *addr = p;
743e289fd28Sdingtianhong 
744e289fd28Sdingtianhong 	if (!is_valid_ether_addr(addr->sa_data))
745e289fd28Sdingtianhong 		return -EADDRNOTAVAIL;
746e289fd28Sdingtianhong 
747e26f43faSVlad Yasevich 	/* If the addresses are the same, this is a no-op */
748e26f43faSVlad Yasevich 	if (ether_addr_equal(dev->dev_addr, addr->sa_data))
749e26f43faSVlad Yasevich 		return 0;
750e26f43faSVlad Yasevich 
751e289fd28Sdingtianhong 	if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
75218c8c54dSVlad Yasevich 		macvlan_set_addr_change(vlan->port);
7533a37a963SPetr Machata 		return dev_set_mac_address(vlan->lowerdev, addr, NULL);
754e289fd28Sdingtianhong 	}
755e289fd28Sdingtianhong 
75659f997b0SMatteo Croce 	if (macvlan_addr_busy(vlan->port, addr->sa_data))
75759f997b0SMatteo Croce 		return -EADDRINUSE;
75859f997b0SMatteo Croce 
759e289fd28Sdingtianhong 	return macvlan_sync_address(dev, addr->sa_data);
760e289fd28Sdingtianhong }
761e289fd28Sdingtianhong 
762b863ceb7SPatrick McHardy static void macvlan_change_rx_flags(struct net_device *dev, int change)
763b863ceb7SPatrick McHardy {
764b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
765b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
766b863ceb7SPatrick McHardy 
767bbeb0eadSPeter Christensen 	if (dev->flags & IFF_UP) {
768b863ceb7SPatrick McHardy 		if (change & IFF_ALLMULTI)
769b863ceb7SPatrick McHardy 			dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
770efdbd2b3SVlad Yasevich 		if (change & IFF_PROMISC)
771efdbd2b3SVlad Yasevich 			dev_set_promiscuity(lowerdev,
772efdbd2b3SVlad Yasevich 					    dev->flags & IFF_PROMISC ? 1 : -1);
773efdbd2b3SVlad Yasevich 
774b863ceb7SPatrick McHardy 	}
775bbeb0eadSPeter Christensen }
776b863ceb7SPatrick McHardy 
7779c127a01SHerbert Xu static void macvlan_compute_filter(unsigned long *mc_filter,
7789c127a01SHerbert Xu 				   struct net_device *dev,
7799c127a01SHerbert Xu 				   struct macvlan_dev *vlan)
780b863ceb7SPatrick McHardy {
781cd431e73SEric Dumazet 	if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
7829c127a01SHerbert Xu 		bitmap_fill(mc_filter, MACVLAN_MC_FILTER_SZ);
783cd431e73SEric Dumazet 	} else {
784cd431e73SEric Dumazet 		struct netdev_hw_addr *ha;
785cd431e73SEric Dumazet 		DECLARE_BITMAP(filter, MACVLAN_MC_FILTER_SZ);
786cd431e73SEric Dumazet 
787cd431e73SEric Dumazet 		bitmap_zero(filter, MACVLAN_MC_FILTER_SZ);
788cd431e73SEric Dumazet 		netdev_for_each_mc_addr(ha, dev) {
7893807ff58SEric Dumazet 			__set_bit(mc_hash(vlan, ha->addr), filter);
790cd431e73SEric Dumazet 		}
791d5270430SEric Dumazet 
7923807ff58SEric Dumazet 		__set_bit(mc_hash(vlan, dev->broadcast), filter);
793d5270430SEric Dumazet 
7949c127a01SHerbert Xu 		bitmap_copy(mc_filter, filter, MACVLAN_MC_FILTER_SZ);
795cd431e73SEric Dumazet 	}
7969c127a01SHerbert Xu }
7979c127a01SHerbert Xu 
7989c127a01SHerbert Xu static void macvlan_set_mac_lists(struct net_device *dev)
7999c127a01SHerbert Xu {
8009c127a01SHerbert Xu 	struct macvlan_dev *vlan = netdev_priv(dev);
8019c127a01SHerbert Xu 
8029c127a01SHerbert Xu 	macvlan_compute_filter(vlan->mc_filter, dev, vlan);
8039c127a01SHerbert Xu 
804df8ef8f3SJohn Fastabend 	dev_uc_sync(vlan->lowerdev, dev);
805b863ceb7SPatrick McHardy 	dev_mc_sync(vlan->lowerdev, dev);
8069c127a01SHerbert Xu 
8079c127a01SHerbert Xu 	/* This is slightly inaccurate as we're including the subscription
8089c127a01SHerbert Xu 	 * list of vlan->lowerdev too.
8099c127a01SHerbert Xu 	 *
8109c127a01SHerbert Xu 	 * Bug alert: This only works if everyone has the same broadcast
8119c127a01SHerbert Xu 	 * address as lowerdev.  As soon as someone changes theirs this
8129c127a01SHerbert Xu 	 * will break.
8139c127a01SHerbert Xu 	 *
8149c127a01SHerbert Xu 	 * However, this is already broken as when you change your broadcast
8159c127a01SHerbert Xu 	 * address we don't get called.
8169c127a01SHerbert Xu 	 *
8179c127a01SHerbert Xu 	 * The solution is to maintain a list of broadcast addresses like
8189c127a01SHerbert Xu 	 * we do for uc/mc, if you care.
8199c127a01SHerbert Xu 	 */
8209c127a01SHerbert Xu 	macvlan_compute_filter(vlan->port->mc_filter, vlan->lowerdev, NULL);
821b863ceb7SPatrick McHardy }
822b863ceb7SPatrick McHardy 
823b863ceb7SPatrick McHardy static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
824b863ceb7SPatrick McHardy {
825b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
826b863ceb7SPatrick McHardy 
82791572088SJarod Wilson 	if (vlan->lowerdev->mtu < new_mtu)
828b863ceb7SPatrick McHardy 		return -EINVAL;
829b863ceb7SPatrick McHardy 	dev->mtu = new_mtu;
830b863ceb7SPatrick McHardy 	return 0;
831b863ceb7SPatrick McHardy }
832b863ceb7SPatrick McHardy 
833a7605370SArnd Bergmann static int macvlan_eth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
834254c0a2bSHangbin Liu {
835254c0a2bSHangbin Liu 	struct net_device *real_dev = macvlan_dev_real_dev(dev);
836254c0a2bSHangbin Liu 	const struct net_device_ops *ops = real_dev->netdev_ops;
837254c0a2bSHangbin Liu 	struct ifreq ifrr;
838254c0a2bSHangbin Liu 	int err = -EOPNOTSUPP;
839254c0a2bSHangbin Liu 
84036f18439SGustavo A. R. Silva 	strscpy(ifrr.ifr_name, real_dev->name, IFNAMSIZ);
841254c0a2bSHangbin Liu 	ifrr.ifr_ifru = ifr->ifr_ifru;
842254c0a2bSHangbin Liu 
843254c0a2bSHangbin Liu 	switch (cmd) {
844254c0a2bSHangbin Liu 	case SIOCSHWTSTAMP:
8456c2ea9ebSHangbin Liu 		if (!net_eq(dev_net(dev), &init_net))
8466c2ea9ebSHangbin Liu 			break;
847df561f66SGustavo A. R. Silva 		fallthrough;
848254c0a2bSHangbin Liu 	case SIOCGHWTSTAMP:
849a7605370SArnd Bergmann 		if (netif_device_present(real_dev) && ops->ndo_eth_ioctl)
850a7605370SArnd Bergmann 			err = ops->ndo_eth_ioctl(real_dev, &ifrr, cmd);
851254c0a2bSHangbin Liu 		break;
852254c0a2bSHangbin Liu 	}
853254c0a2bSHangbin Liu 
854254c0a2bSHangbin Liu 	if (!err)
855254c0a2bSHangbin Liu 		ifr->ifr_ifru = ifrr.ifr_ifru;
856254c0a2bSHangbin Liu 
857254c0a2bSHangbin Liu 	return err;
858254c0a2bSHangbin Liu }
859254c0a2bSHangbin Liu 
860b863ceb7SPatrick McHardy /*
861b863ceb7SPatrick McHardy  * macvlan network devices have devices nesting below it and are a special
862b863ceb7SPatrick McHardy  * "super class" of normal network devices; split their locks off into a
863b863ceb7SPatrick McHardy  * separate class since they always nest.
864b863ceb7SPatrick McHardy  */
865845e0ebbSCong Wang static struct lock_class_key macvlan_netdev_addr_lock_key;
866845e0ebbSCong Wang 
86770957eaeSVlad Yasevich #define ALWAYS_ON_OFFLOADS \
86870957eaeSVlad Yasevich 	(NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE | \
869f21e5077SDimitris Michailidis 	 NETIF_F_GSO_ROBUST | NETIF_F_GSO_ENCAP_ALL)
8708b4703e9SVlad Yasevich 
87170957eaeSVlad Yasevich #define ALWAYS_ON_FEATURES (ALWAYS_ON_OFFLOADS | NETIF_F_LLTX)
87270957eaeSVlad Yasevich 
873b863ceb7SPatrick McHardy #define MACVLAN_FEATURES \
874a188222bSTom Herbert 	(NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
875fb652fdfSDavid S. Miller 	 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_LRO | \
8768d13e670SJohn Fastabend 	 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
87728d2b136SPatrick McHardy 	 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
878b863ceb7SPatrick McHardy 
879b863ceb7SPatrick McHardy #define MACVLAN_STATE_MASK \
880b863ceb7SPatrick McHardy 	((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
881b863ceb7SPatrick McHardy 
882845e0ebbSCong Wang static void macvlan_set_lockdep_class(struct net_device *dev)
883845e0ebbSCong Wang {
884845e0ebbSCong Wang 	netdev_lockdep_set_classes(dev);
885be74294fSCong Wang 	lockdep_set_class(&dev->addr_list_lock,
886be74294fSCong Wang 			  &macvlan_netdev_addr_lock_key);
887845e0ebbSCong Wang }
888845e0ebbSCong Wang 
889b863ceb7SPatrick McHardy static int macvlan_init(struct net_device *dev)
890b863ceb7SPatrick McHardy {
891b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
892b863ceb7SPatrick McHardy 	const struct net_device *lowerdev = vlan->lowerdev;
89330837960SFrancesco Ruggeri 	struct macvlan_port *port = vlan->port;
894b863ceb7SPatrick McHardy 
895b863ceb7SPatrick McHardy 	dev->state		= (dev->state & ~MACVLAN_STATE_MASK) |
896b863ceb7SPatrick McHardy 				  (lowerdev->state & MACVLAN_STATE_MASK);
897b863ceb7SPatrick McHardy 	dev->features 		= lowerdev->features & MACVLAN_FEATURES;
8988b4703e9SVlad Yasevich 	dev->features		|= ALWAYS_ON_FEATURES;
89962dbe830SMichal Kubeček 	dev->hw_features	|= NETIF_F_LRO;
900081e83a7SVlad Yasevich 	dev->vlan_features	= lowerdev->vlan_features & MACVLAN_FEATURES;
90170957eaeSVlad Yasevich 	dev->vlan_features	|= ALWAYS_ON_OFFLOADS;
902f21e5077SDimitris Michailidis 	dev->hw_enc_features    |= dev->features;
9038c2acc53SPatrick McHardy 	dev->gso_max_size	= lowerdev->gso_max_size;
904f6773c5eSEric Dumazet 	dev->gso_max_segs	= lowerdev->gso_max_segs;
905ef5c8996Ssg.tweak@gmail.com 	dev->hard_header_len	= lowerdev->hard_header_len;
906845e0ebbSCong Wang 	macvlan_set_lockdep_class(dev);
9071a33e10eSCong Wang 
9081c213bd2SWANG Cong 	vlan->pcpu_stats = netdev_alloc_pcpu_stats(struct vlan_pcpu_stats);
9098ffab51bSEric Dumazet 	if (!vlan->pcpu_stats)
910fccaf710SEric Dumazet 		return -ENOMEM;
911fccaf710SEric Dumazet 
91230837960SFrancesco Ruggeri 	port->count += 1;
91330837960SFrancesco Ruggeri 
914b863ceb7SPatrick McHardy 	return 0;
915b863ceb7SPatrick McHardy }
916b863ceb7SPatrick McHardy 
917fccaf710SEric Dumazet static void macvlan_uninit(struct net_device *dev)
918fccaf710SEric Dumazet {
919fccaf710SEric Dumazet 	struct macvlan_dev *vlan = netdev_priv(dev);
920d5cd9244SEric W. Biederman 	struct macvlan_port *port = vlan->port;
921fccaf710SEric Dumazet 
9228ffab51bSEric Dumazet 	free_percpu(vlan->pcpu_stats);
923d5cd9244SEric W. Biederman 
92479cf79abSMichael Braun 	macvlan_flush_sources(port, vlan);
9255e3c516bSDavid S. Miller 	port->count -= 1;
9265e3c516bSDavid S. Miller 	if (!port->count)
927d5cd9244SEric W. Biederman 		macvlan_port_destroy(port->dev);
928fccaf710SEric Dumazet }
929fccaf710SEric Dumazet 
930bc1f4470Sstephen hemminger static void macvlan_dev_get_stats64(struct net_device *dev,
93128172739SEric Dumazet 				    struct rtnl_link_stats64 *stats)
932fccaf710SEric Dumazet {
933fccaf710SEric Dumazet 	struct macvlan_dev *vlan = netdev_priv(dev);
934fccaf710SEric Dumazet 
9358ffab51bSEric Dumazet 	if (vlan->pcpu_stats) {
936cdf3e274SLi RongQing 		struct vlan_pcpu_stats *p;
9378ffab51bSEric Dumazet 		u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
9388ffab51bSEric Dumazet 		u32 rx_errors = 0, tx_dropped = 0;
939bc66154eSEric Dumazet 		unsigned int start;
940fccaf710SEric Dumazet 		int i;
941fccaf710SEric Dumazet 
942fccaf710SEric Dumazet 		for_each_possible_cpu(i) {
9438ffab51bSEric Dumazet 			p = per_cpu_ptr(vlan->pcpu_stats, i);
944bc66154eSEric Dumazet 			do {
94557a7744eSEric W. Biederman 				start = u64_stats_fetch_begin_irq(&p->syncp);
946bc66154eSEric Dumazet 				rx_packets	= p->rx_packets;
947bc66154eSEric Dumazet 				rx_bytes	= p->rx_bytes;
948bc66154eSEric Dumazet 				rx_multicast	= p->rx_multicast;
9498ffab51bSEric Dumazet 				tx_packets	= p->tx_packets;
9508ffab51bSEric Dumazet 				tx_bytes	= p->tx_bytes;
95157a7744eSEric W. Biederman 			} while (u64_stats_fetch_retry_irq(&p->syncp, start));
9528ffab51bSEric Dumazet 
9538ffab51bSEric Dumazet 			stats->rx_packets	+= rx_packets;
9548ffab51bSEric Dumazet 			stats->rx_bytes		+= rx_bytes;
9558ffab51bSEric Dumazet 			stats->multicast	+= rx_multicast;
9568ffab51bSEric Dumazet 			stats->tx_packets	+= tx_packets;
9578ffab51bSEric Dumazet 			stats->tx_bytes		+= tx_bytes;
9588ffab51bSEric Dumazet 			/* rx_errors & tx_dropped are u32, updated
9598ffab51bSEric Dumazet 			 * without syncp protection.
9608ffab51bSEric Dumazet 			 */
9618ffab51bSEric Dumazet 			rx_errors	+= p->rx_errors;
9628ffab51bSEric Dumazet 			tx_dropped	+= p->tx_dropped;
963fccaf710SEric Dumazet 		}
9648ffab51bSEric Dumazet 		stats->rx_errors	= rx_errors;
9658ffab51bSEric Dumazet 		stats->rx_dropped	= rx_errors;
9668ffab51bSEric Dumazet 		stats->tx_dropped	= tx_dropped;
967fccaf710SEric Dumazet 	}
968fccaf710SEric Dumazet }
969fccaf710SEric Dumazet 
9708e586137SJiri Pirko static int macvlan_vlan_rx_add_vid(struct net_device *dev,
97180d5c368SPatrick McHardy 				   __be16 proto, u16 vid)
9728d13e670SJohn Fastabend {
9738d13e670SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
9748d13e670SJohn Fastabend 	struct net_device *lowerdev = vlan->lowerdev;
9758d13e670SJohn Fastabend 
97680d5c368SPatrick McHardy 	return vlan_vid_add(lowerdev, proto, vid);
9778d13e670SJohn Fastabend }
9788d13e670SJohn Fastabend 
9798e586137SJiri Pirko static int macvlan_vlan_rx_kill_vid(struct net_device *dev,
98080d5c368SPatrick McHardy 				    __be16 proto, u16 vid)
9818d13e670SJohn Fastabend {
9828d13e670SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
9838d13e670SJohn Fastabend 	struct net_device *lowerdev = vlan->lowerdev;
9848d13e670SJohn Fastabend 
98580d5c368SPatrick McHardy 	vlan_vid_del(lowerdev, proto, vid);
9868e586137SJiri Pirko 	return 0;
9878d13e670SJohn Fastabend }
9888d13e670SJohn Fastabend 
989edc7d573Sstephen hemminger static int macvlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
990df8ef8f3SJohn Fastabend 			   struct net_device *dev,
991f6f6424bSJiri Pirko 			   const unsigned char *addr, u16 vid,
99287b0984eSPetr Machata 			   u16 flags,
99387b0984eSPetr Machata 			   struct netlink_ext_ack *extack)
994df8ef8f3SJohn Fastabend {
995df8ef8f3SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
996df8ef8f3SJohn Fastabend 	int err = -EINVAL;
997df8ef8f3SJohn Fastabend 
9988a50f11cSVlad Yasevich 	/* Support unicast filter only on passthru devices.
9998a50f11cSVlad Yasevich 	 * Multicast filter should be allowed on all devices.
10008a50f11cSVlad Yasevich 	 */
100143c2d578SVlad Yasevich 	if (!macvlan_passthru(vlan->port) && is_unicast_ether_addr(addr))
1002df8ef8f3SJohn Fastabend 		return -EOPNOTSUPP;
1003df8ef8f3SJohn Fastabend 
1004ab2cfbb2SThomas Richter 	if (flags & NLM_F_REPLACE)
1005ab2cfbb2SThomas Richter 		return -EOPNOTSUPP;
1006ab2cfbb2SThomas Richter 
1007df8ef8f3SJohn Fastabend 	if (is_unicast_ether_addr(addr))
1008df8ef8f3SJohn Fastabend 		err = dev_uc_add_excl(dev, addr);
1009df8ef8f3SJohn Fastabend 	else if (is_multicast_ether_addr(addr))
1010df8ef8f3SJohn Fastabend 		err = dev_mc_add_excl(dev, addr);
1011df8ef8f3SJohn Fastabend 
1012df8ef8f3SJohn Fastabend 	return err;
1013df8ef8f3SJohn Fastabend }
1014df8ef8f3SJohn Fastabend 
10151690be63SVlad Yasevich static int macvlan_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
1016df8ef8f3SJohn Fastabend 			   struct net_device *dev,
1017f6f6424bSJiri Pirko 			   const unsigned char *addr, u16 vid)
1018df8ef8f3SJohn Fastabend {
1019df8ef8f3SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
1020df8ef8f3SJohn Fastabend 	int err = -EINVAL;
1021df8ef8f3SJohn Fastabend 
10228a50f11cSVlad Yasevich 	/* Support unicast filter only on passthru devices.
10238a50f11cSVlad Yasevich 	 * Multicast filter should be allowed on all devices.
10248a50f11cSVlad Yasevich 	 */
102543c2d578SVlad Yasevich 	if (!macvlan_passthru(vlan->port) && is_unicast_ether_addr(addr))
1026df8ef8f3SJohn Fastabend 		return -EOPNOTSUPP;
1027df8ef8f3SJohn Fastabend 
1028df8ef8f3SJohn Fastabend 	if (is_unicast_ether_addr(addr))
1029df8ef8f3SJohn Fastabend 		err = dev_uc_del(dev, addr);
1030df8ef8f3SJohn Fastabend 	else if (is_multicast_ether_addr(addr))
1031df8ef8f3SJohn Fastabend 		err = dev_mc_del(dev, addr);
1032df8ef8f3SJohn Fastabend 
1033df8ef8f3SJohn Fastabend 	return err;
1034df8ef8f3SJohn Fastabend }
1035df8ef8f3SJohn Fastabend 
1036b863ceb7SPatrick McHardy static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
1037b863ceb7SPatrick McHardy 					struct ethtool_drvinfo *drvinfo)
1038b863ceb7SPatrick McHardy {
10397826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "macvlan", sizeof(drvinfo->driver));
10407826d43fSJiri Pirko 	strlcpy(drvinfo->version, "0.1", sizeof(drvinfo->version));
1041b863ceb7SPatrick McHardy }
1042b863ceb7SPatrick McHardy 
104385f95819SDavid Decotigny static int macvlan_ethtool_get_link_ksettings(struct net_device *dev,
104485f95819SDavid Decotigny 					      struct ethtool_link_ksettings *cmd)
10459edb8bb6SStephen Hemminger {
10469edb8bb6SStephen Hemminger 	const struct macvlan_dev *vlan = netdev_priv(dev);
10474bc71cb9SJiri Pirko 
104885f95819SDavid Decotigny 	return __ethtool_get_link_ksettings(vlan->lowerdev, cmd);
10499edb8bb6SStephen Hemminger }
10509edb8bb6SStephen Hemminger 
1051254c0a2bSHangbin Liu static int macvlan_ethtool_get_ts_info(struct net_device *dev,
1052254c0a2bSHangbin Liu 				       struct ethtool_ts_info *info)
1053254c0a2bSHangbin Liu {
1054254c0a2bSHangbin Liu 	struct net_device *real_dev = macvlan_dev_real_dev(dev);
1055254c0a2bSHangbin Liu 	const struct ethtool_ops *ops = real_dev->ethtool_ops;
1056254c0a2bSHangbin Liu 	struct phy_device *phydev = real_dev->phydev;
1057254c0a2bSHangbin Liu 
1058d25de984SRichard Cochran 	if (phy_has_tsinfo(phydev)) {
1059d25de984SRichard Cochran 		return phy_ts_info(phydev, info);
1060254c0a2bSHangbin Liu 	} else if (ops->get_ts_info) {
1061254c0a2bSHangbin Liu 		return ops->get_ts_info(real_dev, info);
1062254c0a2bSHangbin Liu 	} else {
1063254c0a2bSHangbin Liu 		info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE |
1064254c0a2bSHangbin Liu 			SOF_TIMESTAMPING_SOFTWARE;
1065254c0a2bSHangbin Liu 		info->phc_index = -1;
1066254c0a2bSHangbin Liu 	}
1067254c0a2bSHangbin Liu 
1068254c0a2bSHangbin Liu 	return 0;
1069254c0a2bSHangbin Liu }
1070254c0a2bSHangbin Liu 
10712be5c767SVlad Yasevich static netdev_features_t macvlan_fix_features(struct net_device *dev,
10722be5c767SVlad Yasevich 					      netdev_features_t features)
10732be5c767SVlad Yasevich {
10742be5c767SVlad Yasevich 	struct macvlan_dev *vlan = netdev_priv(dev);
107562dbe830SMichal Kubeček 	netdev_features_t lowerdev_features = vlan->lowerdev->features;
1076797f87f8SFlorian Westphal 	netdev_features_t mask;
10772be5c767SVlad Yasevich 
1078797f87f8SFlorian Westphal 	features |= NETIF_F_ALL_FOR_ALL;
1079797f87f8SFlorian Westphal 	features &= (vlan->set_features | ~MACVLAN_FEATURES);
1080797f87f8SFlorian Westphal 	mask = features;
1081797f87f8SFlorian Westphal 
108262dbe830SMichal Kubeček 	lowerdev_features &= (features | ~NETIF_F_LRO);
108362dbe830SMichal Kubeček 	features = netdev_increment_features(lowerdev_features, features, mask);
10848b4703e9SVlad Yasevich 	features |= ALWAYS_ON_FEATURES;
108513fbcc8dSShannon Nelson 	features &= (ALWAYS_ON_FEATURES | MACVLAN_FEATURES);
1086797f87f8SFlorian Westphal 
1087797f87f8SFlorian Westphal 	return features;
10882be5c767SVlad Yasevich }
10892be5c767SVlad Yasevich 
1090688cea83Sdingtianhong #ifdef CONFIG_NET_POLL_CONTROLLER
1091688cea83Sdingtianhong static void macvlan_dev_poll_controller(struct net_device *dev)
1092688cea83Sdingtianhong {
1093688cea83Sdingtianhong 	return;
1094688cea83Sdingtianhong }
1095688cea83Sdingtianhong 
1096688cea83Sdingtianhong static int macvlan_dev_netpoll_setup(struct net_device *dev, struct netpoll_info *npinfo)
1097688cea83Sdingtianhong {
1098688cea83Sdingtianhong 	struct macvlan_dev *vlan = netdev_priv(dev);
1099688cea83Sdingtianhong 	struct net_device *real_dev = vlan->lowerdev;
1100688cea83Sdingtianhong 	struct netpoll *netpoll;
1101419a38ceSMenglong Dong 	int err;
1102688cea83Sdingtianhong 
1103688cea83Sdingtianhong 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
1104688cea83Sdingtianhong 	err = -ENOMEM;
1105688cea83Sdingtianhong 	if (!netpoll)
1106688cea83Sdingtianhong 		goto out;
1107688cea83Sdingtianhong 
1108688cea83Sdingtianhong 	err = __netpoll_setup(netpoll, real_dev);
1109688cea83Sdingtianhong 	if (err) {
1110688cea83Sdingtianhong 		kfree(netpoll);
1111688cea83Sdingtianhong 		goto out;
1112688cea83Sdingtianhong 	}
1113688cea83Sdingtianhong 
1114688cea83Sdingtianhong 	vlan->netpoll = netpoll;
1115688cea83Sdingtianhong 
1116688cea83Sdingtianhong out:
1117688cea83Sdingtianhong 	return err;
1118688cea83Sdingtianhong }
1119688cea83Sdingtianhong 
1120688cea83Sdingtianhong static void macvlan_dev_netpoll_cleanup(struct net_device *dev)
1121688cea83Sdingtianhong {
1122688cea83Sdingtianhong 	struct macvlan_dev *vlan = netdev_priv(dev);
1123688cea83Sdingtianhong 	struct netpoll *netpoll = vlan->netpoll;
1124688cea83Sdingtianhong 
1125688cea83Sdingtianhong 	if (!netpoll)
1126688cea83Sdingtianhong 		return;
1127688cea83Sdingtianhong 
1128688cea83Sdingtianhong 	vlan->netpoll = NULL;
1129688cea83Sdingtianhong 
1130c9fbd71fSDebabrata Banerjee 	__netpoll_free(netpoll);
1131688cea83Sdingtianhong }
1132688cea83Sdingtianhong #endif	/* CONFIG_NET_POLL_CONTROLLER */
1133688cea83Sdingtianhong 
1134ef5fa6bcSNicolas Dichtel static int macvlan_dev_get_iflink(const struct net_device *dev)
1135ef5fa6bcSNicolas Dichtel {
1136ef5fa6bcSNicolas Dichtel 	struct macvlan_dev *vlan = netdev_priv(dev);
1137ef5fa6bcSNicolas Dichtel 
1138ef5fa6bcSNicolas Dichtel 	return vlan->lowerdev->ifindex;
1139ef5fa6bcSNicolas Dichtel }
1140ef5fa6bcSNicolas Dichtel 
1141b863ceb7SPatrick McHardy static const struct ethtool_ops macvlan_ethtool_ops = {
1142b863ceb7SPatrick McHardy 	.get_link		= ethtool_op_get_link,
114385f95819SDavid Decotigny 	.get_link_ksettings	= macvlan_ethtool_get_link_ksettings,
1144b863ceb7SPatrick McHardy 	.get_drvinfo		= macvlan_ethtool_get_drvinfo,
1145254c0a2bSHangbin Liu 	.get_ts_info		= macvlan_ethtool_get_ts_info,
1146b863ceb7SPatrick McHardy };
1147b863ceb7SPatrick McHardy 
114854a30c97SStephen Hemminger static const struct net_device_ops macvlan_netdev_ops = {
114954a30c97SStephen Hemminger 	.ndo_init		= macvlan_init,
1150fccaf710SEric Dumazet 	.ndo_uninit		= macvlan_uninit,
115154a30c97SStephen Hemminger 	.ndo_open		= macvlan_open,
115254a30c97SStephen Hemminger 	.ndo_stop		= macvlan_stop,
115300829823SStephen Hemminger 	.ndo_start_xmit		= macvlan_start_xmit,
115454a30c97SStephen Hemminger 	.ndo_change_mtu		= macvlan_change_mtu,
1155a7605370SArnd Bergmann 	.ndo_eth_ioctl		= macvlan_eth_ioctl,
11562be5c767SVlad Yasevich 	.ndo_fix_features	= macvlan_fix_features,
115754a30c97SStephen Hemminger 	.ndo_change_rx_flags	= macvlan_change_rx_flags,
115854a30c97SStephen Hemminger 	.ndo_set_mac_address	= macvlan_set_mac_address,
1159df8ef8f3SJohn Fastabend 	.ndo_set_rx_mode	= macvlan_set_mac_lists,
1160bc66154eSEric Dumazet 	.ndo_get_stats64	= macvlan_dev_get_stats64,
116154a30c97SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
11628d13e670SJohn Fastabend 	.ndo_vlan_rx_add_vid	= macvlan_vlan_rx_add_vid,
11638d13e670SJohn Fastabend 	.ndo_vlan_rx_kill_vid	= macvlan_vlan_rx_kill_vid,
1164df8ef8f3SJohn Fastabend 	.ndo_fdb_add		= macvlan_fdb_add,
1165df8ef8f3SJohn Fastabend 	.ndo_fdb_del		= macvlan_fdb_del,
1166df8ef8f3SJohn Fastabend 	.ndo_fdb_dump		= ndo_dflt_fdb_dump,
1167688cea83Sdingtianhong #ifdef CONFIG_NET_POLL_CONTROLLER
1168688cea83Sdingtianhong 	.ndo_poll_controller	= macvlan_dev_poll_controller,
1169688cea83Sdingtianhong 	.ndo_netpoll_setup	= macvlan_dev_netpoll_setup,
1170688cea83Sdingtianhong 	.ndo_netpoll_cleanup	= macvlan_dev_netpoll_cleanup,
1171688cea83Sdingtianhong #endif
1172ef5fa6bcSNicolas Dichtel 	.ndo_get_iflink		= macvlan_dev_get_iflink,
1173f56e67b5SToshiaki Makita 	.ndo_features_check	= passthru_features_check,
11742e8b4ba6SAndy Roulin 	.ndo_change_proto_down  = dev_change_proto_down_generic,
117554a30c97SStephen Hemminger };
117654a30c97SStephen Hemminger 
11778a35747aSHerbert Xu void macvlan_common_setup(struct net_device *dev)
1178b863ceb7SPatrick McHardy {
1179b863ceb7SPatrick McHardy 	ether_setup(dev);
1180b863ceb7SPatrick McHardy 
118191572088SJarod Wilson 	dev->min_mtu		= 0;
118291572088SJarod Wilson 	dev->max_mtu		= ETH_MAX_MTU;
118302875878SEric Dumazet 	dev->priv_flags	       &= ~IFF_TX_SKB_SHARING;
118402875878SEric Dumazet 	netif_keep_dst(dev);
118587ab7f6fSVlad Yasevich 	dev->priv_flags	       |= IFF_UNICAST_FLT;
118654a30c97SStephen Hemminger 	dev->netdev_ops		= &macvlan_netdev_ops;
1187cf124db5SDavid S. Miller 	dev->needs_free_netdev	= true;
118871740129SLutz Jaenicke 	dev->header_ops		= &macvlan_hard_header_ops;
1189b863ceb7SPatrick McHardy 	dev->ethtool_ops	= &macvlan_ethtool_ops;
11908a35747aSHerbert Xu }
11918a35747aSHerbert Xu EXPORT_SYMBOL_GPL(macvlan_common_setup);
11928a35747aSHerbert Xu 
11938a35747aSHerbert Xu static void macvlan_setup(struct net_device *dev)
11948a35747aSHerbert Xu {
11958a35747aSHerbert Xu 	macvlan_common_setup(dev);
11967009212bSZhang Shengju 	dev->priv_flags |= IFF_NO_QUEUE;
1197b863ceb7SPatrick McHardy }
1198b863ceb7SPatrick McHardy 
1199b863ceb7SPatrick McHardy static int macvlan_port_create(struct net_device *dev)
1200b863ceb7SPatrick McHardy {
1201b863ceb7SPatrick McHardy 	struct macvlan_port *port;
1202b863ceb7SPatrick McHardy 	unsigned int i;
1203ab95bfe0SJiri Pirko 	int err;
1204b863ceb7SPatrick McHardy 
1205b863ceb7SPatrick McHardy 	if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
1206b863ceb7SPatrick McHardy 		return -EINVAL;
1207b863ceb7SPatrick McHardy 
1208322dc6e0SMahesh Bandewar 	if (netdev_is_rx_handler_busy(dev))
1209d6b00fecSMahesh Bandewar 		return -EBUSY;
1210d6b00fecSMahesh Bandewar 
1211b863ceb7SPatrick McHardy 	port = kzalloc(sizeof(*port), GFP_KERNEL);
1212b863ceb7SPatrick McHardy 	if (port == NULL)
1213b863ceb7SPatrick McHardy 		return -ENOMEM;
1214b863ceb7SPatrick McHardy 
1215b863ceb7SPatrick McHardy 	port->dev = dev;
121618c8c54dSVlad Yasevich 	ether_addr_copy(port->perm_addr, dev->dev_addr);
1217b863ceb7SPatrick McHardy 	INIT_LIST_HEAD(&port->vlans);
1218b863ceb7SPatrick McHardy 	for (i = 0; i < MACVLAN_HASH_SIZE; i++)
1219b863ceb7SPatrick McHardy 		INIT_HLIST_HEAD(&port->vlan_hash[i]);
122079cf79abSMichael Braun 	for (i = 0; i < MACVLAN_HASH_SIZE; i++)
122179cf79abSMichael Braun 		INIT_HLIST_HEAD(&port->vlan_source_hash[i]);
1222ab95bfe0SJiri Pirko 
1223d4bff72cSThomas Karlsson 	port->bc_queue_len_used = 0;
1224412ca155SHerbert Xu 	skb_queue_head_init(&port->bc_queue);
1225412ca155SHerbert Xu 	INIT_WORK(&port->bc_work, macvlan_process_broadcast);
1226412ca155SHerbert Xu 
1227a35e2c1bSJiri Pirko 	err = netdev_rx_handler_register(dev, macvlan_handle_frame, port);
1228a35e2c1bSJiri Pirko 	if (err)
1229ab95bfe0SJiri Pirko 		kfree(port);
1230d9351561SEric Dumazet 	else
1231a35e2c1bSJiri Pirko 		dev->priv_flags |= IFF_MACVLAN_PORT;
1232ab95bfe0SJiri Pirko 	return err;
1233b863ceb7SPatrick McHardy }
1234b863ceb7SPatrick McHardy 
1235b863ceb7SPatrick McHardy static void macvlan_port_destroy(struct net_device *dev)
1236b863ceb7SPatrick McHardy {
1237e052f7e6SEric Dumazet 	struct macvlan_port *port = macvlan_port_get_rtnl(dev);
1238f6478218SHerbert Xu 	struct sk_buff *skb;
1239b863ceb7SPatrick McHardy 
1240a35e2c1bSJiri Pirko 	dev->priv_flags &= ~IFF_MACVLAN_PORT;
1241ab95bfe0SJiri Pirko 	netdev_rx_handler_unregister(dev);
1242fe0ca732SEric Dumazet 
1243fe0ca732SEric Dumazet 	/* After this point, no packet can schedule bc_work anymore,
1244fe0ca732SEric Dumazet 	 * but we need to cancel it and purge left skbs if any.
1245fe0ca732SEric Dumazet 	 */
1246fe0ca732SEric Dumazet 	cancel_work_sync(&port->bc_work);
1247f6478218SHerbert Xu 
1248f6478218SHerbert Xu 	while ((skb = __skb_dequeue(&port->bc_queue))) {
1249f6478218SHerbert Xu 		const struct macvlan_dev *src = MACVLAN_SKB_CB(skb)->src;
1250f6478218SHerbert Xu 
1251f6478218SHerbert Xu 		if (src)
1252f6478218SHerbert Xu 			dev_put(src->dev);
1253f6478218SHerbert Xu 
1254f6478218SHerbert Xu 		kfree_skb(skb);
1255f6478218SHerbert Xu 	}
1256fe0ca732SEric Dumazet 
125718c8c54dSVlad Yasevich 	/* If the lower device address has been changed by passthru
125818c8c54dSVlad Yasevich 	 * macvlan, put it back.
125918c8c54dSVlad Yasevich 	 */
126018c8c54dSVlad Yasevich 	if (macvlan_passthru(port) &&
126118c8c54dSVlad Yasevich 	    !ether_addr_equal(port->dev->dev_addr, port->perm_addr)) {
126218c8c54dSVlad Yasevich 		struct sockaddr sa;
126318c8c54dSVlad Yasevich 
126418c8c54dSVlad Yasevich 		sa.sa_family = port->dev->type;
126518c8c54dSVlad Yasevich 		memcpy(&sa.sa_data, port->perm_addr, port->dev->addr_len);
12663a37a963SPetr Machata 		dev_set_mac_address(port->dev, &sa, NULL);
126718c8c54dSVlad Yasevich 	}
126818c8c54dSVlad Yasevich 
1269a1f5315cSGao Feng 	kfree(port);
1270b863ceb7SPatrick McHardy }
1271b863ceb7SPatrick McHardy 
1272a8b8a889SMatthias Schiffer static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[],
1273a8b8a889SMatthias Schiffer 			    struct netlink_ext_ack *extack)
1274b863ceb7SPatrick McHardy {
12758b61fba5SAlvin Šipraga 	struct nlattr *nla, *head;
12768b61fba5SAlvin Šipraga 	int rem, len;
12778b61fba5SAlvin Šipraga 
1278b863ceb7SPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
1279b863ceb7SPatrick McHardy 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1280b863ceb7SPatrick McHardy 			return -EINVAL;
1281b863ceb7SPatrick McHardy 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1282b863ceb7SPatrick McHardy 			return -EADDRNOTAVAIL;
1283b863ceb7SPatrick McHardy 	}
128427c0b1a8SArnd Bergmann 
128507850a4fSZhang Shengju 	if (!data)
128607850a4fSZhang Shengju 		return 0;
128707850a4fSZhang Shengju 
128807850a4fSZhang Shengju 	if (data[IFLA_MACVLAN_FLAGS] &&
1289427f0c8cSJethro Beekman 	    nla_get_u16(data[IFLA_MACVLAN_FLAGS]) & ~(MACVLAN_FLAG_NOPROMISC |
1290427f0c8cSJethro Beekman 						      MACVLAN_FLAG_NODST))
129115127478SMichael S. Tsirkin 		return -EINVAL;
129215127478SMichael S. Tsirkin 
129307850a4fSZhang Shengju 	if (data[IFLA_MACVLAN_MODE]) {
129427c0b1a8SArnd Bergmann 		switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) {
129527c0b1a8SArnd Bergmann 		case MACVLAN_MODE_PRIVATE:
129627c0b1a8SArnd Bergmann 		case MACVLAN_MODE_VEPA:
129727c0b1a8SArnd Bergmann 		case MACVLAN_MODE_BRIDGE:
1298eb06acdcSSridhar Samudrala 		case MACVLAN_MODE_PASSTHRU:
129979cf79abSMichael Braun 		case MACVLAN_MODE_SOURCE:
130027c0b1a8SArnd Bergmann 			break;
130127c0b1a8SArnd Bergmann 		default:
130227c0b1a8SArnd Bergmann 			return -EINVAL;
130327c0b1a8SArnd Bergmann 		}
130427c0b1a8SArnd Bergmann 	}
130579cf79abSMichael Braun 
130607850a4fSZhang Shengju 	if (data[IFLA_MACVLAN_MACADDR_MODE]) {
130779cf79abSMichael Braun 		switch (nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE])) {
130879cf79abSMichael Braun 		case MACVLAN_MACADDR_ADD:
130979cf79abSMichael Braun 		case MACVLAN_MACADDR_DEL:
131079cf79abSMichael Braun 		case MACVLAN_MACADDR_FLUSH:
131179cf79abSMichael Braun 		case MACVLAN_MACADDR_SET:
131279cf79abSMichael Braun 			break;
131379cf79abSMichael Braun 		default:
131479cf79abSMichael Braun 			return -EINVAL;
131579cf79abSMichael Braun 		}
131679cf79abSMichael Braun 	}
131779cf79abSMichael Braun 
131807850a4fSZhang Shengju 	if (data[IFLA_MACVLAN_MACADDR]) {
131979cf79abSMichael Braun 		if (nla_len(data[IFLA_MACVLAN_MACADDR]) != ETH_ALEN)
132079cf79abSMichael Braun 			return -EINVAL;
132179cf79abSMichael Braun 
132279cf79abSMichael Braun 		if (!is_valid_ether_addr(nla_data(data[IFLA_MACVLAN_MACADDR])))
132379cf79abSMichael Braun 			return -EADDRNOTAVAIL;
132479cf79abSMichael Braun 	}
132579cf79abSMichael Braun 
13268b61fba5SAlvin Šipraga 	if (data[IFLA_MACVLAN_MACADDR_DATA]) {
13278b61fba5SAlvin Šipraga 		head = nla_data(data[IFLA_MACVLAN_MACADDR_DATA]);
13288b61fba5SAlvin Šipraga 		len = nla_len(data[IFLA_MACVLAN_MACADDR_DATA]);
13298b61fba5SAlvin Šipraga 
13308b61fba5SAlvin Šipraga 		nla_for_each_attr(nla, head, len, rem) {
13318b61fba5SAlvin Šipraga 			if (nla_type(nla) != IFLA_MACVLAN_MACADDR ||
13328b61fba5SAlvin Šipraga 			    nla_len(nla) != ETH_ALEN)
13338b61fba5SAlvin Šipraga 				return -EINVAL;
13348b61fba5SAlvin Šipraga 
13358b61fba5SAlvin Šipraga 			if (!is_valid_ether_addr(nla_data(nla)))
13368b61fba5SAlvin Šipraga 				return -EADDRNOTAVAIL;
13378b61fba5SAlvin Šipraga 		}
13388b61fba5SAlvin Šipraga 	}
13398b61fba5SAlvin Šipraga 
134007850a4fSZhang Shengju 	if (data[IFLA_MACVLAN_MACADDR_COUNT])
134179cf79abSMichael Braun 		return -EINVAL;
134279cf79abSMichael Braun 
134379cf79abSMichael Braun 	return 0;
134479cf79abSMichael Braun }
134579cf79abSMichael Braun 
134650a77e5fSLee Jones /*
134779cf79abSMichael Braun  * reconfigure list of remote source mac address
134879cf79abSMichael Braun  * (only for macvlan devices in source mode)
134979cf79abSMichael Braun  * Note regarding alignment: all netlink data is aligned to 4 Byte, which
135079cf79abSMichael Braun  * suffices for both ether_addr_copy and ether_addr_equal_64bits usage.
135179cf79abSMichael Braun  */
135279cf79abSMichael Braun static int macvlan_changelink_sources(struct macvlan_dev *vlan, u32 mode,
135379cf79abSMichael Braun 				      struct nlattr *data[])
135479cf79abSMichael Braun {
135579cf79abSMichael Braun 	char *addr = NULL;
135679cf79abSMichael Braun 	int ret, rem, len;
135779cf79abSMichael Braun 	struct nlattr *nla, *head;
135879cf79abSMichael Braun 	struct macvlan_source_entry *entry;
135979cf79abSMichael Braun 
136079cf79abSMichael Braun 	if (data[IFLA_MACVLAN_MACADDR])
136179cf79abSMichael Braun 		addr = nla_data(data[IFLA_MACVLAN_MACADDR]);
136279cf79abSMichael Braun 
136379cf79abSMichael Braun 	if (mode == MACVLAN_MACADDR_ADD) {
136479cf79abSMichael Braun 		if (!addr)
136579cf79abSMichael Braun 			return -EINVAL;
136679cf79abSMichael Braun 
136779cf79abSMichael Braun 		return macvlan_hash_add_source(vlan, addr);
136879cf79abSMichael Braun 
136979cf79abSMichael Braun 	} else if (mode == MACVLAN_MACADDR_DEL) {
137079cf79abSMichael Braun 		if (!addr)
137179cf79abSMichael Braun 			return -EINVAL;
137279cf79abSMichael Braun 
137379cf79abSMichael Braun 		entry = macvlan_hash_lookup_source(vlan, addr);
137479cf79abSMichael Braun 		if (entry) {
137579cf79abSMichael Braun 			macvlan_hash_del_source(entry);
137679cf79abSMichael Braun 			vlan->macaddr_count--;
137779cf79abSMichael Braun 		}
137879cf79abSMichael Braun 	} else if (mode == MACVLAN_MACADDR_FLUSH) {
137979cf79abSMichael Braun 		macvlan_flush_sources(vlan->port, vlan);
138079cf79abSMichael Braun 	} else if (mode == MACVLAN_MACADDR_SET) {
138179cf79abSMichael Braun 		macvlan_flush_sources(vlan->port, vlan);
138279cf79abSMichael Braun 
138379cf79abSMichael Braun 		if (addr) {
138479cf79abSMichael Braun 			ret = macvlan_hash_add_source(vlan, addr);
138579cf79abSMichael Braun 			if (ret)
138679cf79abSMichael Braun 				return ret;
138779cf79abSMichael Braun 		}
138879cf79abSMichael Braun 
138989430ef3SYunjian Wang 		if (!data[IFLA_MACVLAN_MACADDR_DATA])
139079cf79abSMichael Braun 			return 0;
139179cf79abSMichael Braun 
139279cf79abSMichael Braun 		head = nla_data(data[IFLA_MACVLAN_MACADDR_DATA]);
139379cf79abSMichael Braun 		len = nla_len(data[IFLA_MACVLAN_MACADDR_DATA]);
139479cf79abSMichael Braun 
139579cf79abSMichael Braun 		nla_for_each_attr(nla, head, len, rem) {
139679cf79abSMichael Braun 			addr = nla_data(nla);
139779cf79abSMichael Braun 			ret = macvlan_hash_add_source(vlan, addr);
139879cf79abSMichael Braun 			if (ret)
139979cf79abSMichael Braun 				return ret;
140079cf79abSMichael Braun 		}
140179cf79abSMichael Braun 	} else {
140279cf79abSMichael Braun 		return -EINVAL;
140379cf79abSMichael Braun 	}
140479cf79abSMichael Braun 
1405b863ceb7SPatrick McHardy 	return 0;
1406b863ceb7SPatrick McHardy }
1407b863ceb7SPatrick McHardy 
1408fc0663d6SArnd Bergmann int macvlan_common_newlink(struct net *src_net, struct net_device *dev,
140942ab19eeSDavid Ahern 			   struct nlattr *tb[], struct nlattr *data[],
141042ab19eeSDavid Ahern 			   struct netlink_ext_ack *extack)
1411b863ceb7SPatrick McHardy {
1412b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
1413b863ceb7SPatrick McHardy 	struct macvlan_port *port;
1414b863ceb7SPatrick McHardy 	struct net_device *lowerdev;
1415b863ceb7SPatrick McHardy 	int err;
141679cf79abSMichael Braun 	int macmode;
1417aa5fd0fbSGao Feng 	bool create = false;
1418b863ceb7SPatrick McHardy 
1419b863ceb7SPatrick McHardy 	if (!tb[IFLA_LINK])
1420b863ceb7SPatrick McHardy 		return -EINVAL;
1421b863ceb7SPatrick McHardy 
142281adee47SEric W. Biederman 	lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
1423b863ceb7SPatrick McHardy 	if (lowerdev == NULL)
1424b863ceb7SPatrick McHardy 		return -ENODEV;
1425b863ceb7SPatrick McHardy 
1426d70f2cf5SKevin Wallace 	/* When creating macvlans or macvtaps on top of other macvlans - use
1427b0832a29SEric Biederman 	 * the real device as the lowerdev.
1428a6ca5f1dSPatrick McHardy 	 */
1429d70f2cf5SKevin Wallace 	if (netif_is_macvlan(lowerdev))
1430d70f2cf5SKevin Wallace 		lowerdev = macvlan_dev_real_dev(lowerdev);
1431a6ca5f1dSPatrick McHardy 
1432b863ceb7SPatrick McHardy 	if (!tb[IFLA_MTU])
1433b863ceb7SPatrick McHardy 		dev->mtu = lowerdev->mtu;
1434b863ceb7SPatrick McHardy 	else if (dev->mtu > lowerdev->mtu)
1435b863ceb7SPatrick McHardy 		return -EINVAL;
1436b863ceb7SPatrick McHardy 
143791572088SJarod Wilson 	/* MTU range: 68 - lowerdev->max_mtu */
143891572088SJarod Wilson 	dev->min_mtu = ETH_MIN_MTU;
143991572088SJarod Wilson 	dev->max_mtu = lowerdev->max_mtu;
144091572088SJarod Wilson 
1441b863ceb7SPatrick McHardy 	if (!tb[IFLA_ADDRESS])
14427ce5d222SDanny Kukawka 		eth_hw_addr_random(dev);
1443b863ceb7SPatrick McHardy 
1444913564fbSJulian Wiedmann 	if (!netif_is_macvlan_port(lowerdev)) {
1445b863ceb7SPatrick McHardy 		err = macvlan_port_create(lowerdev);
1446b863ceb7SPatrick McHardy 		if (err < 0)
1447b863ceb7SPatrick McHardy 			return err;
1448aa5fd0fbSGao Feng 		create = true;
1449b863ceb7SPatrick McHardy 	}
1450e052f7e6SEric Dumazet 	port = macvlan_port_get_rtnl(lowerdev);
1451b863ceb7SPatrick McHardy 
1452eb06acdcSSridhar Samudrala 	/* Only 1 macvlan device can be created in passthru mode */
145343c2d578SVlad Yasevich 	if (macvlan_passthru(port)) {
1454aa5fd0fbSGao Feng 		/* The macvlan port must be not created this time,
1455aa5fd0fbSGao Feng 		 * still goto destroy_macvlan_port for readability.
1456aa5fd0fbSGao Feng 		 */
1457aa5fd0fbSGao Feng 		err = -EINVAL;
1458aa5fd0fbSGao Feng 		goto destroy_macvlan_port;
1459aa5fd0fbSGao Feng 	}
1460eb06acdcSSridhar Samudrala 
1461b863ceb7SPatrick McHardy 	vlan->lowerdev = lowerdev;
1462b863ceb7SPatrick McHardy 	vlan->dev      = dev;
1463b863ceb7SPatrick McHardy 	vlan->port     = port;
14642be5c767SVlad Yasevich 	vlan->set_features = MACVLAN_FEATURES;
1465b863ceb7SPatrick McHardy 
146627c0b1a8SArnd Bergmann 	vlan->mode     = MACVLAN_MODE_VEPA;
146727c0b1a8SArnd Bergmann 	if (data && data[IFLA_MACVLAN_MODE])
146827c0b1a8SArnd Bergmann 		vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
146927c0b1a8SArnd Bergmann 
1470df8ef8f3SJohn Fastabend 	if (data && data[IFLA_MACVLAN_FLAGS])
1471df8ef8f3SJohn Fastabend 		vlan->flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
1472df8ef8f3SJohn Fastabend 
1473eb06acdcSSridhar Samudrala 	if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
1474aa5fd0fbSGao Feng 		if (port->count) {
1475aa5fd0fbSGao Feng 			err = -EINVAL;
1476aa5fd0fbSGao Feng 			goto destroy_macvlan_port;
1477aa5fd0fbSGao Feng 		}
147843c2d578SVlad Yasevich 		macvlan_set_passthru(port);
14798b98604eSBjørn Mork 		eth_hw_addr_inherit(dev, lowerdev);
1480eb06acdcSSridhar Samudrala 	}
1481eb06acdcSSridhar Samudrala 
148279cf79abSMichael Braun 	if (data && data[IFLA_MACVLAN_MACADDR_MODE]) {
1483aa5fd0fbSGao Feng 		if (vlan->mode != MACVLAN_MODE_SOURCE) {
1484aa5fd0fbSGao Feng 			err = -EINVAL;
1485aa5fd0fbSGao Feng 			goto destroy_macvlan_port;
1486aa5fd0fbSGao Feng 		}
148779cf79abSMichael Braun 		macmode = nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE]);
148879cf79abSMichael Braun 		err = macvlan_changelink_sources(vlan, macmode, data);
148979cf79abSMichael Braun 		if (err)
1490aa5fd0fbSGao Feng 			goto destroy_macvlan_port;
149179cf79abSMichael Braun 	}
149279cf79abSMichael Braun 
1493d4bff72cSThomas Karlsson 	vlan->bc_queue_len_req = MACVLAN_DEFAULT_BC_QUEUE_LEN;
1494d4bff72cSThomas Karlsson 	if (data && data[IFLA_MACVLAN_BC_QUEUE_LEN])
1495d4bff72cSThomas Karlsson 		vlan->bc_queue_len_req = nla_get_u32(data[IFLA_MACVLAN_BC_QUEUE_LEN]);
1496d4bff72cSThomas Karlsson 
149747d4ab91SJohn Fastabend 	err = register_netdevice(dev);
149847d4ab91SJohn Fastabend 	if (err < 0)
1499aa5fd0fbSGao Feng 		goto destroy_macvlan_port;
150047d4ab91SJohn Fastabend 
1501a6cc0cfaSJohn Fastabend 	dev->priv_flags |= IFF_MACVLAN;
150242ab19eeSDavid Ahern 	err = netdev_upper_dev_link(lowerdev, dev, extack);
15037cd43db7SJiri Pirko 	if (err)
1504da37705cSCong Wang 		goto unregister_netdev;
1505b863ceb7SPatrick McHardy 
1506233c7df0SJiri Pirko 	list_add_tail_rcu(&vlan->list, &port->vlans);
1507d4bff72cSThomas Karlsson 	update_port_bc_queue_len(vlan->port);
1508fc4a7489SPatrick Mullaney 	netif_stacked_transfer_operstate(lowerdev, dev);
1509de7d244dSNikolay Aleksandrov 	linkwatch_fire_event(dev);
1510f16d3d57SJiri Pirko 
1511b863ceb7SPatrick McHardy 	return 0;
1512f16d3d57SJiri Pirko 
1513da37705cSCong Wang unregister_netdev:
1514d02fd6e7SGao Feng 	/* macvlan_uninit would free the macvlan port */
1515da37705cSCong Wang 	unregister_netdevice(dev);
1516d02fd6e7SGao Feng 	return err;
1517aa5fd0fbSGao Feng destroy_macvlan_port:
1518d02fd6e7SGao Feng 	/* the macvlan port may be freed by macvlan_uninit when fail to register.
1519d02fd6e7SGao Feng 	 * so we destroy the macvlan port only when it's valid.
1520d02fd6e7SGao Feng 	 */
15214e14bf42SAlexey Kodanev 	if (create && macvlan_port_get_rtnl(lowerdev))
1522aa5fd0fbSGao Feng 		macvlan_port_destroy(port->dev);
1523f16d3d57SJiri Pirko 	return err;
1524b863ceb7SPatrick McHardy }
1525fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_common_newlink);
1526b863ceb7SPatrick McHardy 
1527fc0663d6SArnd Bergmann static int macvlan_newlink(struct net *src_net, struct net_device *dev,
15287a3f4a18SMatthias Schiffer 			   struct nlattr *tb[], struct nlattr *data[],
15297a3f4a18SMatthias Schiffer 			   struct netlink_ext_ack *extack)
1530fc0663d6SArnd Bergmann {
153142ab19eeSDavid Ahern 	return macvlan_common_newlink(src_net, dev, tb, data, extack);
1532fc0663d6SArnd Bergmann }
1533fc0663d6SArnd Bergmann 
1534fc0663d6SArnd Bergmann void macvlan_dellink(struct net_device *dev, struct list_head *head)
1535b863ceb7SPatrick McHardy {
1536b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
1537b863ceb7SPatrick McHardy 
153879cf79abSMichael Braun 	if (vlan->mode == MACVLAN_MODE_SOURCE)
153979cf79abSMichael Braun 		macvlan_flush_sources(vlan->port, vlan);
1540233c7df0SJiri Pirko 	list_del_rcu(&vlan->list);
1541d4bff72cSThomas Karlsson 	update_port_bc_queue_len(vlan->port);
154223289a37SEric Dumazet 	unregister_netdevice_queue(dev, head);
15437cd43db7SJiri Pirko 	netdev_upper_dev_unlink(vlan->lowerdev, dev);
1544b863ceb7SPatrick McHardy }
1545fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_dellink);
1546b863ceb7SPatrick McHardy 
154727c0b1a8SArnd Bergmann static int macvlan_changelink(struct net_device *dev,
1548ad744b22SMatthias Schiffer 			      struct nlattr *tb[], struct nlattr *data[],
1549ad744b22SMatthias Schiffer 			      struct netlink_ext_ack *extack)
155027c0b1a8SArnd Bergmann {
155127c0b1a8SArnd Bergmann 	struct macvlan_dev *vlan = netdev_priv(dev);
1552266e8347SMichael S. Tsirkin 	enum macvlan_mode mode;
1553266e8347SMichael S. Tsirkin 	bool set_mode = false;
155479cf79abSMichael Braun 	enum macvlan_macaddr_mode macmode;
155579cf79abSMichael Braun 	int ret;
1556266e8347SMichael S. Tsirkin 
1557266e8347SMichael S. Tsirkin 	/* Validate mode, but don't set yet: setting flags may fail. */
1558266e8347SMichael S. Tsirkin 	if (data && data[IFLA_MACVLAN_MODE]) {
1559266e8347SMichael S. Tsirkin 		set_mode = true;
1560266e8347SMichael S. Tsirkin 		mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
1561266e8347SMichael S. Tsirkin 		/* Passthrough mode can't be set or cleared dynamically */
1562266e8347SMichael S. Tsirkin 		if ((mode == MACVLAN_MODE_PASSTHRU) !=
1563266e8347SMichael S. Tsirkin 		    (vlan->mode == MACVLAN_MODE_PASSTHRU))
1564266e8347SMichael S. Tsirkin 			return -EINVAL;
156579cf79abSMichael Braun 		if (vlan->mode == MACVLAN_MODE_SOURCE &&
156679cf79abSMichael Braun 		    vlan->mode != mode)
156779cf79abSMichael Braun 			macvlan_flush_sources(vlan->port, vlan);
1568266e8347SMichael S. Tsirkin 	}
156999ffc3e7SMichael S. Tsirkin 
1570df8ef8f3SJohn Fastabend 	if (data && data[IFLA_MACVLAN_FLAGS]) {
1571df8ef8f3SJohn Fastabend 		__u16 flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
1572df8ef8f3SJohn Fastabend 		bool promisc = (flags ^ vlan->flags) & MACVLAN_FLAG_NOPROMISC;
157343c2d578SVlad Yasevich 		if (macvlan_passthru(vlan->port) && promisc) {
157499ffc3e7SMichael S. Tsirkin 			int err;
1575df8ef8f3SJohn Fastabend 
157699ffc3e7SMichael S. Tsirkin 			if (flags & MACVLAN_FLAG_NOPROMISC)
157799ffc3e7SMichael S. Tsirkin 				err = dev_set_promiscuity(vlan->lowerdev, -1);
157899ffc3e7SMichael S. Tsirkin 			else
157999ffc3e7SMichael S. Tsirkin 				err = dev_set_promiscuity(vlan->lowerdev, 1);
158099ffc3e7SMichael S. Tsirkin 			if (err < 0)
158199ffc3e7SMichael S. Tsirkin 				return err;
158299ffc3e7SMichael S. Tsirkin 		}
1583df8ef8f3SJohn Fastabend 		vlan->flags = flags;
1584df8ef8f3SJohn Fastabend 	}
1585d4bff72cSThomas Karlsson 
1586d4bff72cSThomas Karlsson 	if (data && data[IFLA_MACVLAN_BC_QUEUE_LEN]) {
1587d4bff72cSThomas Karlsson 		vlan->bc_queue_len_req = nla_get_u32(data[IFLA_MACVLAN_BC_QUEUE_LEN]);
1588d4bff72cSThomas Karlsson 		update_port_bc_queue_len(vlan->port);
1589d4bff72cSThomas Karlsson 	}
1590d4bff72cSThomas Karlsson 
1591266e8347SMichael S. Tsirkin 	if (set_mode)
1592266e8347SMichael S. Tsirkin 		vlan->mode = mode;
159379cf79abSMichael Braun 	if (data && data[IFLA_MACVLAN_MACADDR_MODE]) {
159479cf79abSMichael Braun 		if (vlan->mode != MACVLAN_MODE_SOURCE)
159579cf79abSMichael Braun 			return -EINVAL;
159679cf79abSMichael Braun 		macmode = nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE]);
159779cf79abSMichael Braun 		ret = macvlan_changelink_sources(vlan, macmode, data);
159879cf79abSMichael Braun 		if (ret)
159979cf79abSMichael Braun 			return ret;
160079cf79abSMichael Braun 	}
160127c0b1a8SArnd Bergmann 	return 0;
160227c0b1a8SArnd Bergmann }
160327c0b1a8SArnd Bergmann 
160479cf79abSMichael Braun static size_t macvlan_get_size_mac(const struct macvlan_dev *vlan)
160579cf79abSMichael Braun {
160679cf79abSMichael Braun 	if (vlan->macaddr_count == 0)
160779cf79abSMichael Braun 		return 0;
160879cf79abSMichael Braun 	return nla_total_size(0) /* IFLA_MACVLAN_MACADDR_DATA */
160979cf79abSMichael Braun 		+ vlan->macaddr_count * nla_total_size(sizeof(u8) * ETH_ALEN);
161079cf79abSMichael Braun }
161179cf79abSMichael Braun 
161227c0b1a8SArnd Bergmann static size_t macvlan_get_size(const struct net_device *dev)
161327c0b1a8SArnd Bergmann {
161479cf79abSMichael Braun 	struct macvlan_dev *vlan = netdev_priv(dev);
161579cf79abSMichael Braun 
161601fe944fSEric Dumazet 	return (0
161701fe944fSEric Dumazet 		+ nla_total_size(4) /* IFLA_MACVLAN_MODE */
161801fe944fSEric Dumazet 		+ nla_total_size(2) /* IFLA_MACVLAN_FLAGS */
161979cf79abSMichael Braun 		+ nla_total_size(4) /* IFLA_MACVLAN_MACADDR_COUNT */
162079cf79abSMichael Braun 		+ macvlan_get_size_mac(vlan) /* IFLA_MACVLAN_MACADDR */
1621d4bff72cSThomas Karlsson 		+ nla_total_size(4) /* IFLA_MACVLAN_BC_QUEUE_LEN */
1622d4bff72cSThomas Karlsson 		+ nla_total_size(4) /* IFLA_MACVLAN_BC_QUEUE_LEN_USED */
162301fe944fSEric Dumazet 		);
162427c0b1a8SArnd Bergmann }
162527c0b1a8SArnd Bergmann 
162679cf79abSMichael Braun static int macvlan_fill_info_macaddr(struct sk_buff *skb,
162779cf79abSMichael Braun 				     const struct macvlan_dev *vlan,
162879cf79abSMichael Braun 				     const int i)
162979cf79abSMichael Braun {
163079cf79abSMichael Braun 	struct hlist_head *h = &vlan->port->vlan_source_hash[i];
163179cf79abSMichael Braun 	struct macvlan_source_entry *entry;
163279cf79abSMichael Braun 
163379cf79abSMichael Braun 	hlist_for_each_entry_rcu(entry, h, hlist) {
163479cf79abSMichael Braun 		if (entry->vlan != vlan)
163579cf79abSMichael Braun 			continue;
163679cf79abSMichael Braun 		if (nla_put(skb, IFLA_MACVLAN_MACADDR, ETH_ALEN, entry->addr))
163779cf79abSMichael Braun 			return 1;
163879cf79abSMichael Braun 	}
163979cf79abSMichael Braun 	return 0;
164079cf79abSMichael Braun }
164179cf79abSMichael Braun 
164227c0b1a8SArnd Bergmann static int macvlan_fill_info(struct sk_buff *skb,
164327c0b1a8SArnd Bergmann 				const struct net_device *dev)
164427c0b1a8SArnd Bergmann {
164527c0b1a8SArnd Bergmann 	struct macvlan_dev *vlan = netdev_priv(dev);
1646d4bff72cSThomas Karlsson 	struct macvlan_port *port = vlan->port;
164779cf79abSMichael Braun 	int i;
164879cf79abSMichael Braun 	struct nlattr *nest;
164927c0b1a8SArnd Bergmann 
1650ead9a76cSDavid S. Miller 	if (nla_put_u32(skb, IFLA_MACVLAN_MODE, vlan->mode))
1651ead9a76cSDavid S. Miller 		goto nla_put_failure;
1652df8ef8f3SJohn Fastabend 	if (nla_put_u16(skb, IFLA_MACVLAN_FLAGS, vlan->flags))
1653df8ef8f3SJohn Fastabend 		goto nla_put_failure;
165479cf79abSMichael Braun 	if (nla_put_u32(skb, IFLA_MACVLAN_MACADDR_COUNT, vlan->macaddr_count))
165579cf79abSMichael Braun 		goto nla_put_failure;
165679cf79abSMichael Braun 	if (vlan->macaddr_count > 0) {
1657ae0be8deSMichal Kubecek 		nest = nla_nest_start_noflag(skb, IFLA_MACVLAN_MACADDR_DATA);
165879cf79abSMichael Braun 		if (nest == NULL)
165979cf79abSMichael Braun 			goto nla_put_failure;
166079cf79abSMichael Braun 
166179cf79abSMichael Braun 		for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
166279cf79abSMichael Braun 			if (macvlan_fill_info_macaddr(skb, vlan, i))
166379cf79abSMichael Braun 				goto nla_put_failure;
166479cf79abSMichael Braun 		}
166579cf79abSMichael Braun 		nla_nest_end(skb, nest);
166679cf79abSMichael Braun 	}
1667d4bff72cSThomas Karlsson 	if (nla_put_u32(skb, IFLA_MACVLAN_BC_QUEUE_LEN, vlan->bc_queue_len_req))
1668d4bff72cSThomas Karlsson 		goto nla_put_failure;
1669d4bff72cSThomas Karlsson 	if (nla_put_u32(skb, IFLA_MACVLAN_BC_QUEUE_LEN_USED, port->bc_queue_len_used))
1670d4bff72cSThomas Karlsson 		goto nla_put_failure;
167127c0b1a8SArnd Bergmann 	return 0;
167227c0b1a8SArnd Bergmann 
167327c0b1a8SArnd Bergmann nla_put_failure:
167427c0b1a8SArnd Bergmann 	return -EMSGSIZE;
167527c0b1a8SArnd Bergmann }
167627c0b1a8SArnd Bergmann 
167727c0b1a8SArnd Bergmann static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = {
167827c0b1a8SArnd Bergmann 	[IFLA_MACVLAN_MODE]  = { .type = NLA_U32 },
1679df8ef8f3SJohn Fastabend 	[IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 },
168079cf79abSMichael Braun 	[IFLA_MACVLAN_MACADDR_MODE] = { .type = NLA_U32 },
168179cf79abSMichael Braun 	[IFLA_MACVLAN_MACADDR] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
168279cf79abSMichael Braun 	[IFLA_MACVLAN_MACADDR_DATA] = { .type = NLA_NESTED },
168379cf79abSMichael Braun 	[IFLA_MACVLAN_MACADDR_COUNT] = { .type = NLA_U32 },
1684d4bff72cSThomas Karlsson 	[IFLA_MACVLAN_BC_QUEUE_LEN] = { .type = NLA_U32 },
1685d4bff72cSThomas Karlsson 	[IFLA_MACVLAN_BC_QUEUE_LEN_USED] = { .type = NLA_REJECT },
168627c0b1a8SArnd Bergmann };
168727c0b1a8SArnd Bergmann 
1688fc0663d6SArnd Bergmann int macvlan_link_register(struct rtnl_link_ops *ops)
1689fc0663d6SArnd Bergmann {
1690fc0663d6SArnd Bergmann 	/* common fields */
1691fc0663d6SArnd Bergmann 	ops->validate		= macvlan_validate;
1692fc0663d6SArnd Bergmann 	ops->maxtype		= IFLA_MACVLAN_MAX;
1693fc0663d6SArnd Bergmann 	ops->policy		= macvlan_policy;
1694fc0663d6SArnd Bergmann 	ops->changelink		= macvlan_changelink;
1695fc0663d6SArnd Bergmann 	ops->get_size		= macvlan_get_size;
1696fc0663d6SArnd Bergmann 	ops->fill_info		= macvlan_fill_info;
1697fc0663d6SArnd Bergmann 
1698fc0663d6SArnd Bergmann 	return rtnl_link_register(ops);
1699fc0663d6SArnd Bergmann };
1700fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_link_register);
1701fc0663d6SArnd Bergmann 
1702eaca400fSNicolas Dichtel static struct net *macvlan_get_link_net(const struct net_device *dev)
1703eaca400fSNicolas Dichtel {
1704eaca400fSNicolas Dichtel 	return dev_net(macvlan_dev_real_dev(dev));
1705eaca400fSNicolas Dichtel }
1706eaca400fSNicolas Dichtel 
1707fc0663d6SArnd Bergmann static struct rtnl_link_ops macvlan_link_ops = {
1708b863ceb7SPatrick McHardy 	.kind		= "macvlan",
17098a35747aSHerbert Xu 	.setup		= macvlan_setup,
1710b863ceb7SPatrick McHardy 	.newlink	= macvlan_newlink,
1711b863ceb7SPatrick McHardy 	.dellink	= macvlan_dellink,
1712eaca400fSNicolas Dichtel 	.get_link_net	= macvlan_get_link_net,
17136fe3faf8SSainath Grandhi 	.priv_size      = sizeof(struct macvlan_dev),
1714b863ceb7SPatrick McHardy };
1715b863ceb7SPatrick McHardy 
1716d4bff72cSThomas Karlsson static void update_port_bc_queue_len(struct macvlan_port *port)
1717d4bff72cSThomas Karlsson {
1718d4bff72cSThomas Karlsson 	u32 max_bc_queue_len_req = 0;
1719d4bff72cSThomas Karlsson 	struct macvlan_dev *vlan;
1720d4bff72cSThomas Karlsson 
1721d4bff72cSThomas Karlsson 	list_for_each_entry(vlan, &port->vlans, list) {
1722d4bff72cSThomas Karlsson 		if (vlan->bc_queue_len_req > max_bc_queue_len_req)
1723d4bff72cSThomas Karlsson 			max_bc_queue_len_req = vlan->bc_queue_len_req;
1724d4bff72cSThomas Karlsson 	}
1725d4bff72cSThomas Karlsson 	port->bc_queue_len_used = max_bc_queue_len_req;
1726d4bff72cSThomas Karlsson }
1727d4bff72cSThomas Karlsson 
1728b863ceb7SPatrick McHardy static int macvlan_device_event(struct notifier_block *unused,
1729b863ceb7SPatrick McHardy 				unsigned long event, void *ptr)
1730b863ceb7SPatrick McHardy {
1731351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1732b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan, *next;
1733b863ceb7SPatrick McHardy 	struct macvlan_port *port;
1734226bd341SEric Dumazet 	LIST_HEAD(list_kill);
1735b863ceb7SPatrick McHardy 
1736913564fbSJulian Wiedmann 	if (!netif_is_macvlan_port(dev))
1737b863ceb7SPatrick McHardy 		return NOTIFY_DONE;
1738b863ceb7SPatrick McHardy 
1739e052f7e6SEric Dumazet 	port = macvlan_port_get_rtnl(dev);
1740a35e2c1bSJiri Pirko 
1741b863ceb7SPatrick McHardy 	switch (event) {
1742de7d244dSNikolay Aleksandrov 	case NETDEV_UP:
174380fd2d6cSTravis Brown 	case NETDEV_DOWN:
1744b863ceb7SPatrick McHardy 	case NETDEV_CHANGE:
1745b863ceb7SPatrick McHardy 		list_for_each_entry(vlan, &port->vlans, list)
1746fc4a7489SPatrick Mullaney 			netif_stacked_transfer_operstate(vlan->lowerdev,
1747fc4a7489SPatrick Mullaney 							 vlan->dev);
1748b863ceb7SPatrick McHardy 		break;
1749b863ceb7SPatrick McHardy 	case NETDEV_FEAT_CHANGE:
1750b863ceb7SPatrick McHardy 		list_for_each_entry(vlan, &port->vlans, list) {
17518c2acc53SPatrick McHardy 			vlan->dev->gso_max_size = dev->gso_max_size;
1752f6773c5eSEric Dumazet 			vlan->dev->gso_max_segs = dev->gso_max_segs;
1753797f87f8SFlorian Westphal 			netdev_update_features(vlan->dev);
1754b863ceb7SPatrick McHardy 		}
1755b863ceb7SPatrick McHardy 		break;
17563763e7efSdingtianhong 	case NETDEV_CHANGEMTU:
17573763e7efSdingtianhong 		list_for_each_entry(vlan, &port->vlans, list) {
17583763e7efSdingtianhong 			if (vlan->dev->mtu <= dev->mtu)
17593763e7efSdingtianhong 				continue;
17603763e7efSdingtianhong 			dev_set_mtu(vlan->dev, dev->mtu);
17613763e7efSdingtianhong 		}
17623763e7efSdingtianhong 		break;
1763e289fd28Sdingtianhong 	case NETDEV_CHANGEADDR:
176443c2d578SVlad Yasevich 		if (!macvlan_passthru(port))
1765e289fd28Sdingtianhong 			return NOTIFY_DONE;
1766e289fd28Sdingtianhong 
1767e289fd28Sdingtianhong 		vlan = list_first_entry_or_null(&port->vlans,
1768e289fd28Sdingtianhong 						struct macvlan_dev,
1769e289fd28Sdingtianhong 						list);
1770e289fd28Sdingtianhong 
17714dee15b4STaehee Yoo 		if (vlan && macvlan_sync_address(vlan->dev, dev->dev_addr))
1772e289fd28Sdingtianhong 			return NOTIFY_BAD;
1773e289fd28Sdingtianhong 
1774e289fd28Sdingtianhong 		break;
1775b863ceb7SPatrick McHardy 	case NETDEV_UNREGISTER:
17763b27e105SDavid Lamparter 		/* twiddle thumbs on netns device moves */
17773b27e105SDavid Lamparter 		if (dev->reg_state != NETREG_UNREGISTERING)
17783b27e105SDavid Lamparter 			break;
17793b27e105SDavid Lamparter 
1780b863ceb7SPatrick McHardy 		list_for_each_entry_safe(vlan, next, &port->vlans, list)
1781226bd341SEric Dumazet 			vlan->dev->rtnl_link_ops->dellink(vlan->dev, &list_kill);
1782226bd341SEric Dumazet 		unregister_netdevice_many(&list_kill);
1783b863ceb7SPatrick McHardy 		break;
17841c01fe14SJiri Pirko 	case NETDEV_PRE_TYPE_CHANGE:
178526d3f69cSZheng Yongjun 		/* Forbid underlying device to change its type. */
17861c01fe14SJiri Pirko 		return NOTIFY_BAD;
17874c991255SVlad Yasevich 
17884c991255SVlad Yasevich 	case NETDEV_NOTIFY_PEERS:
17894c991255SVlad Yasevich 	case NETDEV_BONDING_FAILOVER:
17904c991255SVlad Yasevich 	case NETDEV_RESEND_IGMP:
17914c991255SVlad Yasevich 		/* Propagate to all vlans */
17924c991255SVlad Yasevich 		list_for_each_entry(vlan, &port->vlans, list)
17934c991255SVlad Yasevich 			call_netdevice_notifiers(event, vlan->dev);
1794b863ceb7SPatrick McHardy 	}
1795b863ceb7SPatrick McHardy 	return NOTIFY_DONE;
1796b863ceb7SPatrick McHardy }
1797b863ceb7SPatrick McHardy 
1798b863ceb7SPatrick McHardy static struct notifier_block macvlan_notifier_block __read_mostly = {
1799b863ceb7SPatrick McHardy 	.notifier_call	= macvlan_device_event,
1800b863ceb7SPatrick McHardy };
1801b863ceb7SPatrick McHardy 
1802b863ceb7SPatrick McHardy static int __init macvlan_init_module(void)
1803b863ceb7SPatrick McHardy {
1804b863ceb7SPatrick McHardy 	int err;
1805b863ceb7SPatrick McHardy 
1806b863ceb7SPatrick McHardy 	register_netdevice_notifier(&macvlan_notifier_block);
1807b863ceb7SPatrick McHardy 
1808fc0663d6SArnd Bergmann 	err = macvlan_link_register(&macvlan_link_ops);
1809b863ceb7SPatrick McHardy 	if (err < 0)
1810b863ceb7SPatrick McHardy 		goto err1;
1811b863ceb7SPatrick McHardy 	return 0;
1812b863ceb7SPatrick McHardy err1:
1813b863ceb7SPatrick McHardy 	unregister_netdevice_notifier(&macvlan_notifier_block);
1814b863ceb7SPatrick McHardy 	return err;
1815b863ceb7SPatrick McHardy }
1816b863ceb7SPatrick McHardy 
1817b863ceb7SPatrick McHardy static void __exit macvlan_cleanup_module(void)
1818b863ceb7SPatrick McHardy {
1819b863ceb7SPatrick McHardy 	rtnl_link_unregister(&macvlan_link_ops);
1820b863ceb7SPatrick McHardy 	unregister_netdevice_notifier(&macvlan_notifier_block);
1821b863ceb7SPatrick McHardy }
1822b863ceb7SPatrick McHardy 
1823b863ceb7SPatrick McHardy module_init(macvlan_init_module);
1824b863ceb7SPatrick McHardy module_exit(macvlan_cleanup_module);
1825b863ceb7SPatrick McHardy 
1826b863ceb7SPatrick McHardy MODULE_LICENSE("GPL");
1827b863ceb7SPatrick McHardy MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
1828b863ceb7SPatrick McHardy MODULE_DESCRIPTION("Driver for MAC address based VLANs");
1829b863ceb7SPatrick McHardy MODULE_ALIAS_RTNL_LINK("macvlan");
1830