xref: /openbmc/linux/drivers/net/macvlan.c (revision c9fbd71f73094311b31ee703a918e9e0df502cef)
1b863ceb7SPatrick McHardy /*
2b863ceb7SPatrick McHardy  * Copyright (c) 2007 Patrick McHardy <kaber@trash.net>
3b863ceb7SPatrick McHardy  *
4b863ceb7SPatrick McHardy  * This program is free software; you can redistribute it and/or
5b863ceb7SPatrick McHardy  * modify it under the terms of the GNU General Public License as
6b863ceb7SPatrick McHardy  * published by the Free Software Foundation; either version 2 of
7b863ceb7SPatrick McHardy  * the License, or (at your option) any later version.
8b863ceb7SPatrick McHardy  *
9b863ceb7SPatrick McHardy  * The code this is based on carried the following copyright notice:
10b863ceb7SPatrick McHardy  * ---
11b863ceb7SPatrick McHardy  * (C) Copyright 2001-2006
12b863ceb7SPatrick McHardy  * Alex Zeffertt, Cambridge Broadband Ltd, ajz@cambridgebroadband.com
13b863ceb7SPatrick McHardy  * Re-worked by Ben Greear <greearb@candelatech.com>
14b863ceb7SPatrick McHardy  * ---
15b863ceb7SPatrick McHardy  */
16b863ceb7SPatrick McHardy #include <linux/kernel.h>
17b863ceb7SPatrick McHardy #include <linux/types.h>
18b863ceb7SPatrick McHardy #include <linux/module.h>
19b863ceb7SPatrick McHardy #include <linux/init.h>
20b863ceb7SPatrick McHardy #include <linux/errno.h>
21b863ceb7SPatrick McHardy #include <linux/slab.h>
22b863ceb7SPatrick McHardy #include <linux/string.h>
2382524746SFranck Bui-Huu #include <linux/rculist.h>
24b863ceb7SPatrick McHardy #include <linux/notifier.h>
25b863ceb7SPatrick McHardy #include <linux/netdevice.h>
26b863ceb7SPatrick McHardy #include <linux/etherdevice.h>
27b863ceb7SPatrick McHardy #include <linux/ethtool.h>
28b863ceb7SPatrick McHardy #include <linux/if_arp.h>
2987002b03SJiri Pirko #include <linux/if_vlan.h>
30b863ceb7SPatrick McHardy #include <linux/if_link.h>
31b863ceb7SPatrick McHardy #include <linux/if_macvlan.h>
32cd431e73SEric Dumazet #include <linux/hash.h>
33412ca155SHerbert Xu #include <linux/workqueue.h>
34b863ceb7SPatrick McHardy #include <net/rtnetlink.h>
35618e1b74SArnd Bergmann #include <net/xfrm.h>
36688cea83Sdingtianhong #include <linux/netpoll.h>
37b863ceb7SPatrick McHardy 
3879cf79abSMichael Braun #define MACVLAN_HASH_BITS	8
3979cf79abSMichael Braun #define MACVLAN_HASH_SIZE	(1<<MACVLAN_HASH_BITS)
4007d92d5cSNicolas Dichtel #define MACVLAN_BC_QUEUE_LEN	1000
41b863ceb7SPatrick McHardy 
4243c2d578SVlad Yasevich #define MACVLAN_F_PASSTHRU	1
4318c8c54dSVlad Yasevich #define MACVLAN_F_ADDRCHANGE	2
4443c2d578SVlad Yasevich 
45b863ceb7SPatrick McHardy struct macvlan_port {
46b863ceb7SPatrick McHardy 	struct net_device	*dev;
47b863ceb7SPatrick McHardy 	struct hlist_head	vlan_hash[MACVLAN_HASH_SIZE];
48b863ceb7SPatrick McHardy 	struct list_head	vlans;
49412ca155SHerbert Xu 	struct sk_buff_head	bc_queue;
50412ca155SHerbert Xu 	struct work_struct	bc_work;
5143c2d578SVlad Yasevich 	u32			flags;
525e3c516bSDavid S. Miller 	int			count;
5379cf79abSMichael Braun 	struct hlist_head	vlan_source_hash[MACVLAN_HASH_SIZE];
549c127a01SHerbert Xu 	DECLARE_BITMAP(mc_filter, MACVLAN_MC_FILTER_SZ);
5518c8c54dSVlad Yasevich 	unsigned char           perm_addr[ETH_ALEN];
5679cf79abSMichael Braun };
5779cf79abSMichael Braun 
5879cf79abSMichael Braun struct macvlan_source_entry {
5979cf79abSMichael Braun 	struct hlist_node	hlist;
6079cf79abSMichael Braun 	struct macvlan_dev	*vlan;
6179cf79abSMichael Braun 	unsigned char		addr[6+2] __aligned(sizeof(u16));
6279cf79abSMichael Braun 	struct rcu_head		rcu;
63b863ceb7SPatrick McHardy };
64b863ceb7SPatrick McHardy 
65412ca155SHerbert Xu struct macvlan_skb_cb {
66412ca155SHerbert Xu 	const struct macvlan_dev *src;
67412ca155SHerbert Xu };
68412ca155SHerbert Xu 
69412ca155SHerbert Xu #define MACVLAN_SKB_CB(__skb) ((struct macvlan_skb_cb *)&((__skb)->cb[0]))
70412ca155SHerbert Xu 
71d5cd9244SEric W. Biederman static void macvlan_port_destroy(struct net_device *dev);
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 
122a35e2c1bSJiri Pirko #define macvlan_port_exists(dev) (dev->priv_flags & IFF_MACVLAN_PORT)
123a35e2c1bSJiri Pirko 
124b863ceb7SPatrick McHardy static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
125b863ceb7SPatrick McHardy 					       const unsigned char *addr)
126b863ceb7SPatrick McHardy {
127b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan;
12879cf79abSMichael Braun 	u32 idx = macvlan_eth_hash(addr);
129b863ceb7SPatrick McHardy 
13079cf79abSMichael Braun 	hlist_for_each_entry_rcu(vlan, &port->vlan_hash[idx], hlist) {
131a6700db1SJoe Perches 		if (ether_addr_equal_64bits(vlan->dev->dev_addr, addr))
132b863ceb7SPatrick McHardy 			return vlan;
133b863ceb7SPatrick McHardy 	}
134b863ceb7SPatrick McHardy 	return NULL;
135b863ceb7SPatrick McHardy }
136b863ceb7SPatrick McHardy 
13779cf79abSMichael Braun static struct macvlan_source_entry *macvlan_hash_lookup_source(
13879cf79abSMichael Braun 	const struct macvlan_dev *vlan,
13979cf79abSMichael Braun 	const unsigned char *addr)
14079cf79abSMichael Braun {
14179cf79abSMichael Braun 	struct macvlan_source_entry *entry;
14279cf79abSMichael Braun 	u32 idx = macvlan_eth_hash(addr);
14379cf79abSMichael Braun 	struct hlist_head *h = &vlan->port->vlan_source_hash[idx];
14479cf79abSMichael Braun 
14579cf79abSMichael Braun 	hlist_for_each_entry_rcu(entry, h, hlist) {
14679cf79abSMichael Braun 		if (ether_addr_equal_64bits(entry->addr, addr) &&
14779cf79abSMichael Braun 		    entry->vlan == vlan)
14879cf79abSMichael Braun 			return entry;
14979cf79abSMichael Braun 	}
15079cf79abSMichael Braun 	return NULL;
15179cf79abSMichael Braun }
15279cf79abSMichael Braun 
15379cf79abSMichael Braun static int macvlan_hash_add_source(struct macvlan_dev *vlan,
15479cf79abSMichael Braun 				   const unsigned char *addr)
15579cf79abSMichael Braun {
15679cf79abSMichael Braun 	struct macvlan_port *port = vlan->port;
15779cf79abSMichael Braun 	struct macvlan_source_entry *entry;
15879cf79abSMichael Braun 	struct hlist_head *h;
15979cf79abSMichael Braun 
16079cf79abSMichael Braun 	entry = macvlan_hash_lookup_source(vlan, addr);
16179cf79abSMichael Braun 	if (entry)
16279cf79abSMichael Braun 		return 0;
16379cf79abSMichael Braun 
16479cf79abSMichael Braun 	entry = kmalloc(sizeof(*entry), GFP_KERNEL);
16579cf79abSMichael Braun 	if (!entry)
16679cf79abSMichael Braun 		return -ENOMEM;
16779cf79abSMichael Braun 
16879cf79abSMichael Braun 	ether_addr_copy(entry->addr, addr);
16979cf79abSMichael Braun 	entry->vlan = vlan;
17079cf79abSMichael Braun 	h = &port->vlan_source_hash[macvlan_eth_hash(addr)];
17179cf79abSMichael Braun 	hlist_add_head_rcu(&entry->hlist, h);
17279cf79abSMichael Braun 	vlan->macaddr_count++;
17379cf79abSMichael Braun 
17479cf79abSMichael Braun 	return 0;
17579cf79abSMichael Braun }
17679cf79abSMichael Braun 
177f9ac30f0SEric Biederman static void macvlan_hash_add(struct macvlan_dev *vlan)
178f9ac30f0SEric Biederman {
179f9ac30f0SEric Biederman 	struct macvlan_port *port = vlan->port;
180f9ac30f0SEric Biederman 	const unsigned char *addr = vlan->dev->dev_addr;
18179cf79abSMichael Braun 	u32 idx = macvlan_eth_hash(addr);
182f9ac30f0SEric Biederman 
18379cf79abSMichael Braun 	hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[idx]);
18479cf79abSMichael Braun }
18579cf79abSMichael Braun 
18679cf79abSMichael Braun static void macvlan_hash_del_source(struct macvlan_source_entry *entry)
18779cf79abSMichael Braun {
18879cf79abSMichael Braun 	hlist_del_rcu(&entry->hlist);
18979cf79abSMichael Braun 	kfree_rcu(entry, rcu);
190f9ac30f0SEric Biederman }
191f9ac30f0SEric Biederman 
192449f4544SEric Dumazet static void macvlan_hash_del(struct macvlan_dev *vlan, bool sync)
193f9ac30f0SEric Biederman {
194f9ac30f0SEric Biederman 	hlist_del_rcu(&vlan->hlist);
195449f4544SEric Dumazet 	if (sync)
196f9ac30f0SEric Biederman 		synchronize_rcu();
197f9ac30f0SEric Biederman }
198f9ac30f0SEric Biederman 
199f9ac30f0SEric Biederman static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
200f9ac30f0SEric Biederman 					const unsigned char *addr)
201f9ac30f0SEric Biederman {
202449f4544SEric Dumazet 	macvlan_hash_del(vlan, true);
203f9ac30f0SEric Biederman 	/* Now that we are unhashed it is safe to change the device
204f9ac30f0SEric Biederman 	 * address without confusing packet delivery.
205f9ac30f0SEric Biederman 	 */
206f9ac30f0SEric Biederman 	memcpy(vlan->dev->dev_addr, addr, ETH_ALEN);
207f9ac30f0SEric Biederman 	macvlan_hash_add(vlan);
208f9ac30f0SEric Biederman }
209f9ac30f0SEric Biederman 
210d94d0254SGao Feng static bool macvlan_addr_busy(const struct macvlan_port *port,
211f9ac30f0SEric Biederman 			      const unsigned char *addr)
212f9ac30f0SEric Biederman {
21318c8c54dSVlad Yasevich 	/* Test to see if the specified address is
214f9ac30f0SEric Biederman 	 * currently in use by the underlying device or
215f9ac30f0SEric Biederman 	 * another macvlan.
216f9ac30f0SEric Biederman 	 */
21718c8c54dSVlad Yasevich 	if (!macvlan_passthru(port) && !macvlan_addr_change(port) &&
218e696cda7SVlad Yasevich 	    ether_addr_equal_64bits(port->dev->dev_addr, addr))
219d94d0254SGao Feng 		return true;
220f9ac30f0SEric Biederman 
221f9ac30f0SEric Biederman 	if (macvlan_hash_lookup(port, addr))
222d94d0254SGao Feng 		return true;
223f9ac30f0SEric Biederman 
224d94d0254SGao Feng 	return false;
225f9ac30f0SEric Biederman }
226f9ac30f0SEric Biederman 
227a1e514c5SArnd Bergmann 
228fc0663d6SArnd Bergmann static int macvlan_broadcast_one(struct sk_buff *skb,
229fc0663d6SArnd Bergmann 				 const struct macvlan_dev *vlan,
230618e1b74SArnd Bergmann 				 const struct ethhdr *eth, bool local)
231a1e514c5SArnd Bergmann {
232fc0663d6SArnd Bergmann 	struct net_device *dev = vlan->dev;
233a1e514c5SArnd Bergmann 
234618e1b74SArnd Bergmann 	if (local)
235412ca155SHerbert Xu 		return __dev_forward_skb(dev, skb);
236618e1b74SArnd Bergmann 
237a1e514c5SArnd Bergmann 	skb->dev = dev;
238a6700db1SJoe Perches 	if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast))
239a1e514c5SArnd Bergmann 		skb->pkt_type = PACKET_BROADCAST;
240a1e514c5SArnd Bergmann 	else
241a1e514c5SArnd Bergmann 		skb->pkt_type = PACKET_MULTICAST;
242a1e514c5SArnd Bergmann 
243412ca155SHerbert Xu 	return 0;
244a1e514c5SArnd Bergmann }
245a1e514c5SArnd Bergmann 
2463807ff58SEric Dumazet static u32 macvlan_hash_mix(const struct macvlan_dev *vlan)
2473807ff58SEric Dumazet {
2483807ff58SEric Dumazet 	return (u32)(((unsigned long)vlan) >> L1_CACHE_SHIFT);
2493807ff58SEric Dumazet }
2503807ff58SEric Dumazet 
2513807ff58SEric Dumazet 
2523807ff58SEric Dumazet static unsigned int mc_hash(const struct macvlan_dev *vlan,
2533807ff58SEric Dumazet 			    const unsigned char *addr)
254cd431e73SEric Dumazet {
255cd431e73SEric Dumazet 	u32 val = __get_unaligned_cpu32(addr + 2);
256cd431e73SEric Dumazet 
2573807ff58SEric Dumazet 	val ^= macvlan_hash_mix(vlan);
258cd431e73SEric Dumazet 	return hash_32(val, MACVLAN_MC_FILTER_BITS);
259cd431e73SEric Dumazet }
260cd431e73SEric Dumazet 
261b863ceb7SPatrick McHardy static void macvlan_broadcast(struct sk_buff *skb,
262618e1b74SArnd Bergmann 			      const struct macvlan_port *port,
263618e1b74SArnd Bergmann 			      struct net_device *src,
264618e1b74SArnd Bergmann 			      enum macvlan_mode mode)
265b863ceb7SPatrick McHardy {
266b863ceb7SPatrick McHardy 	const struct ethhdr *eth = eth_hdr(skb);
267b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan;
268b863ceb7SPatrick McHardy 	struct sk_buff *nskb;
269b863ceb7SPatrick McHardy 	unsigned int i;
270a1e514c5SArnd Bergmann 	int err;
2713807ff58SEric Dumazet 	unsigned int hash;
272b863ceb7SPatrick McHardy 
273efbbced3SPatrick McHardy 	if (skb->protocol == htons(ETH_P_PAUSE))
274efbbced3SPatrick McHardy 		return;
275efbbced3SPatrick McHardy 
276b863ceb7SPatrick McHardy 	for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
277b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(vlan, &port->vlan_hash[i], hlist) {
278618e1b74SArnd Bergmann 			if (vlan->dev == src || !(vlan->mode & mode))
279618e1b74SArnd Bergmann 				continue;
280618e1b74SArnd Bergmann 
2813807ff58SEric Dumazet 			hash = mc_hash(vlan, eth->h_dest);
282cd431e73SEric Dumazet 			if (!test_bit(hash, vlan->mc_filter))
283cd431e73SEric Dumazet 				continue;
284de9e8f3fSHerbert Xu 
285de9e8f3fSHerbert Xu 			err = NET_RX_DROP;
286b863ceb7SPatrick McHardy 			nskb = skb_clone(skb, GFP_ATOMIC);
287de9e8f3fSHerbert Xu 			if (likely(nskb))
288de9e8f3fSHerbert Xu 				err = macvlan_broadcast_one(
289de9e8f3fSHerbert Xu 					nskb, vlan, eth,
290412ca155SHerbert Xu 					mode == MACVLAN_MODE_BRIDGE) ?:
291412ca155SHerbert Xu 				      netif_rx_ni(nskb);
292a1e514c5SArnd Bergmann 			macvlan_count_rx(vlan, skb->len + ETH_HLEN,
2934c979935Sjbaron@akamai.com 					 err == NET_RX_SUCCESS, true);
294b863ceb7SPatrick McHardy 		}
295b863ceb7SPatrick McHardy 	}
296b863ceb7SPatrick McHardy }
297b863ceb7SPatrick McHardy 
298412ca155SHerbert Xu static void macvlan_process_broadcast(struct work_struct *w)
299412ca155SHerbert Xu {
300412ca155SHerbert Xu 	struct macvlan_port *port = container_of(w, struct macvlan_port,
301412ca155SHerbert Xu 						 bc_work);
302412ca155SHerbert Xu 	struct sk_buff *skb;
303412ca155SHerbert Xu 	struct sk_buff_head list;
304412ca155SHerbert Xu 
305fe0ca732SEric Dumazet 	__skb_queue_head_init(&list);
306412ca155SHerbert Xu 
307412ca155SHerbert Xu 	spin_lock_bh(&port->bc_queue.lock);
308412ca155SHerbert Xu 	skb_queue_splice_tail_init(&port->bc_queue, &list);
309412ca155SHerbert Xu 	spin_unlock_bh(&port->bc_queue.lock);
310412ca155SHerbert Xu 
311412ca155SHerbert Xu 	while ((skb = __skb_dequeue(&list))) {
312412ca155SHerbert Xu 		const struct macvlan_dev *src = MACVLAN_SKB_CB(skb)->src;
313412ca155SHerbert Xu 
314412ca155SHerbert Xu 		rcu_read_lock();
315412ca155SHerbert Xu 
316412ca155SHerbert Xu 		if (!src)
317412ca155SHerbert Xu 			/* frame comes from an external address */
318412ca155SHerbert Xu 			macvlan_broadcast(skb, port, NULL,
319412ca155SHerbert Xu 					  MACVLAN_MODE_PRIVATE |
320412ca155SHerbert Xu 					  MACVLAN_MODE_VEPA    |
321412ca155SHerbert Xu 					  MACVLAN_MODE_PASSTHRU|
322412ca155SHerbert Xu 					  MACVLAN_MODE_BRIDGE);
323412ca155SHerbert Xu 		else if (src->mode == MACVLAN_MODE_VEPA)
324412ca155SHerbert Xu 			/* flood to everyone except source */
325412ca155SHerbert Xu 			macvlan_broadcast(skb, port, src->dev,
326412ca155SHerbert Xu 					  MACVLAN_MODE_VEPA |
327412ca155SHerbert Xu 					  MACVLAN_MODE_BRIDGE);
328412ca155SHerbert Xu 		else
329412ca155SHerbert Xu 			/*
330412ca155SHerbert Xu 			 * flood only to VEPA ports, bridge ports
331412ca155SHerbert Xu 			 * already saw the frame on the way out.
332412ca155SHerbert Xu 			 */
333412ca155SHerbert Xu 			macvlan_broadcast(skb, port, src->dev,
334412ca155SHerbert Xu 					  MACVLAN_MODE_VEPA);
335412ca155SHerbert Xu 
336412ca155SHerbert Xu 		rcu_read_unlock();
337412ca155SHerbert Xu 
338260916dfSHerbert Xu 		if (src)
339260916dfSHerbert Xu 			dev_put(src->dev);
340412ca155SHerbert Xu 		kfree_skb(skb);
341412ca155SHerbert Xu 	}
342412ca155SHerbert Xu }
343412ca155SHerbert Xu 
344412ca155SHerbert Xu static void macvlan_broadcast_enqueue(struct macvlan_port *port,
345260916dfSHerbert Xu 				      const struct macvlan_dev *src,
346412ca155SHerbert Xu 				      struct sk_buff *skb)
347412ca155SHerbert Xu {
348e676f197SHerbert Xu 	struct sk_buff *nskb;
349412ca155SHerbert Xu 	int err = -ENOMEM;
350412ca155SHerbert Xu 
351e676f197SHerbert Xu 	nskb = skb_clone(skb, GFP_ATOMIC);
352e676f197SHerbert Xu 	if (!nskb)
353412ca155SHerbert Xu 		goto err;
354412ca155SHerbert Xu 
355260916dfSHerbert Xu 	MACVLAN_SKB_CB(nskb)->src = src;
356260916dfSHerbert Xu 
357412ca155SHerbert Xu 	spin_lock(&port->bc_queue.lock);
35807d92d5cSNicolas Dichtel 	if (skb_queue_len(&port->bc_queue) < MACVLAN_BC_QUEUE_LEN) {
359260916dfSHerbert Xu 		if (src)
360260916dfSHerbert Xu 			dev_hold(src->dev);
361e676f197SHerbert Xu 		__skb_queue_tail(&port->bc_queue, nskb);
362412ca155SHerbert Xu 		err = 0;
363412ca155SHerbert Xu 	}
364412ca155SHerbert Xu 	spin_unlock(&port->bc_queue.lock);
365412ca155SHerbert Xu 
366412ca155SHerbert Xu 	if (err)
367e676f197SHerbert Xu 		goto free_nskb;
368412ca155SHerbert Xu 
369412ca155SHerbert Xu 	schedule_work(&port->bc_work);
370412ca155SHerbert Xu 	return;
371412ca155SHerbert Xu 
372e676f197SHerbert Xu free_nskb:
373e676f197SHerbert Xu 	kfree_skb(nskb);
374412ca155SHerbert Xu err:
375412ca155SHerbert Xu 	atomic_long_inc(&skb->dev->rx_dropped);
376412ca155SHerbert Xu }
377412ca155SHerbert Xu 
37879cf79abSMichael Braun static void macvlan_flush_sources(struct macvlan_port *port,
37979cf79abSMichael Braun 				  struct macvlan_dev *vlan)
38079cf79abSMichael Braun {
38179cf79abSMichael Braun 	int i;
38279cf79abSMichael Braun 
38379cf79abSMichael Braun 	for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
38479cf79abSMichael Braun 		struct hlist_node *h, *n;
38579cf79abSMichael Braun 
38679cf79abSMichael Braun 		hlist_for_each_safe(h, n, &port->vlan_source_hash[i]) {
38779cf79abSMichael Braun 			struct macvlan_source_entry *entry;
38879cf79abSMichael Braun 
38979cf79abSMichael Braun 			entry = hlist_entry(h, struct macvlan_source_entry,
39079cf79abSMichael Braun 					    hlist);
39179cf79abSMichael Braun 			if (entry->vlan == vlan)
39279cf79abSMichael Braun 				macvlan_hash_del_source(entry);
39379cf79abSMichael Braun 		}
39479cf79abSMichael Braun 	}
39579cf79abSMichael Braun 	vlan->macaddr_count = 0;
39679cf79abSMichael Braun }
39779cf79abSMichael Braun 
39879cf79abSMichael Braun static void macvlan_forward_source_one(struct sk_buff *skb,
39979cf79abSMichael Braun 				       struct macvlan_dev *vlan)
40079cf79abSMichael Braun {
40179cf79abSMichael Braun 	struct sk_buff *nskb;
40279cf79abSMichael Braun 	struct net_device *dev;
40379cf79abSMichael Braun 	int len;
40479cf79abSMichael Braun 	int ret;
40579cf79abSMichael Braun 
40679cf79abSMichael Braun 	dev = vlan->dev;
40779cf79abSMichael Braun 	if (unlikely(!(dev->flags & IFF_UP)))
40879cf79abSMichael Braun 		return;
40979cf79abSMichael Braun 
41079cf79abSMichael Braun 	nskb = skb_clone(skb, GFP_ATOMIC);
41179cf79abSMichael Braun 	if (!nskb)
41279cf79abSMichael Braun 		return;
41379cf79abSMichael Braun 
41479cf79abSMichael Braun 	len = nskb->len + ETH_HLEN;
41579cf79abSMichael Braun 	nskb->dev = dev;
416c8c41ea1SAlexander Duyck 
417c8c41ea1SAlexander Duyck 	if (ether_addr_equal_64bits(eth_hdr(skb)->h_dest, dev->dev_addr))
41879cf79abSMichael Braun 		nskb->pkt_type = PACKET_HOST;
41979cf79abSMichael Braun 
42079cf79abSMichael Braun 	ret = netif_rx(nskb);
4214c979935Sjbaron@akamai.com 	macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, false);
42279cf79abSMichael Braun }
42379cf79abSMichael Braun 
42479cf79abSMichael Braun static void macvlan_forward_source(struct sk_buff *skb,
42579cf79abSMichael Braun 				   struct macvlan_port *port,
42679cf79abSMichael Braun 				   const unsigned char *addr)
42779cf79abSMichael Braun {
42879cf79abSMichael Braun 	struct macvlan_source_entry *entry;
42979cf79abSMichael Braun 	u32 idx = macvlan_eth_hash(addr);
43079cf79abSMichael Braun 	struct hlist_head *h = &port->vlan_source_hash[idx];
43179cf79abSMichael Braun 
43279cf79abSMichael Braun 	hlist_for_each_entry_rcu(entry, h, hlist) {
43379cf79abSMichael Braun 		if (ether_addr_equal_64bits(entry->addr, addr))
43479cf79abSMichael Braun 			macvlan_forward_source_one(skb, entry->vlan);
43579cf79abSMichael Braun 	}
43679cf79abSMichael Braun }
43779cf79abSMichael Braun 
438b863ceb7SPatrick McHardy /* called under rcu_read_lock() from netif_receive_skb */
4398a4eb573SJiri Pirko static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb)
440b863ceb7SPatrick McHardy {
441ab95bfe0SJiri Pirko 	struct macvlan_port *port;
4428a4eb573SJiri Pirko 	struct sk_buff *skb = *pskb;
443b863ceb7SPatrick McHardy 	const struct ethhdr *eth = eth_hdr(skb);
444b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan;
445618e1b74SArnd Bergmann 	const struct macvlan_dev *src;
446b863ceb7SPatrick McHardy 	struct net_device *dev;
447ba01877fSSridhar Samudrala 	unsigned int len = 0;
448d1dd9119Sjbaron@akamai.com 	int ret;
449d1dd9119Sjbaron@akamai.com 	rx_handler_result_t handle_res;
450b863ceb7SPatrick McHardy 
451a35e2c1bSJiri Pirko 	port = macvlan_port_get_rcu(skb->dev);
452b863ceb7SPatrick McHardy 	if (is_multicast_ether_addr(eth->h_dest)) {
4539c127a01SHerbert Xu 		unsigned int hash;
4549c127a01SHerbert Xu 
45519bcf9f2SEric W. Biederman 		skb = ip_check_defrag(dev_net(skb->dev), skb, IP_DEFRAG_MACVLAN);
456bc416d97SEric Dumazet 		if (!skb)
457bc416d97SEric Dumazet 			return RX_HANDLER_CONSUMED;
458e639b8d8SSabrina Dubroca 		*pskb = skb;
4594ec7ac12SEric Dumazet 		eth = eth_hdr(skb);
46079cf79abSMichael Braun 		macvlan_forward_source(skb, port, eth->h_source);
461618e1b74SArnd Bergmann 		src = macvlan_hash_lookup(port, eth->h_source);
462412ca155SHerbert Xu 		if (src && src->mode != MACVLAN_MODE_VEPA &&
463412ca155SHerbert Xu 		    src->mode != MACVLAN_MODE_BRIDGE) {
464729e72a1Sstephen hemminger 			/* forward to original port. */
465729e72a1Sstephen hemminger 			vlan = src;
466412ca155SHerbert Xu 			ret = macvlan_broadcast_one(skb, vlan, eth, 0) ?:
467412ca155SHerbert Xu 			      netif_rx(skb);
468d1dd9119Sjbaron@akamai.com 			handle_res = RX_HANDLER_CONSUMED;
469729e72a1Sstephen hemminger 			goto out;
470729e72a1Sstephen hemminger 		}
471729e72a1Sstephen hemminger 
4729c127a01SHerbert Xu 		hash = mc_hash(NULL, eth->h_dest);
4739c127a01SHerbert Xu 		if (test_bit(hash, port->mc_filter))
474260916dfSHerbert Xu 			macvlan_broadcast_enqueue(port, src, skb);
475412ca155SHerbert Xu 
4768a4eb573SJiri Pirko 		return RX_HANDLER_PASS;
477b863ceb7SPatrick McHardy 	}
478b863ceb7SPatrick McHardy 
47979cf79abSMichael Braun 	macvlan_forward_source(skb, port, eth->h_source);
48043c2d578SVlad Yasevich 	if (macvlan_passthru(port))
481233c7df0SJiri Pirko 		vlan = list_first_or_null_rcu(&port->vlans,
482233c7df0SJiri Pirko 					      struct macvlan_dev, list);
483eb06acdcSSridhar Samudrala 	else
484b863ceb7SPatrick McHardy 		vlan = macvlan_hash_lookup(port, eth->h_dest);
485dd6b9c2cSAlexander Duyck 	if (!vlan || vlan->mode == MACVLAN_MODE_SOURCE)
4868a4eb573SJiri Pirko 		return RX_HANDLER_PASS;
487b863ceb7SPatrick McHardy 
488b863ceb7SPatrick McHardy 	dev = vlan->dev;
489b863ceb7SPatrick McHardy 	if (unlikely(!(dev->flags & IFF_UP))) {
490b863ceb7SPatrick McHardy 		kfree_skb(skb);
4918a4eb573SJiri Pirko 		return RX_HANDLER_CONSUMED;
492b863ceb7SPatrick McHardy 	}
493a1e514c5SArnd Bergmann 	len = skb->len + ETH_HLEN;
494b863ceb7SPatrick McHardy 	skb = skb_share_check(skb, GFP_ATOMIC);
495d1dd9119Sjbaron@akamai.com 	if (!skb) {
496d1dd9119Sjbaron@akamai.com 		ret = NET_RX_DROP;
497d1dd9119Sjbaron@akamai.com 		handle_res = RX_HANDLER_CONSUMED;
498ba01877fSSridhar Samudrala 		goto out;
499d1dd9119Sjbaron@akamai.com 	}
500b863ceb7SPatrick McHardy 
501e639b8d8SSabrina Dubroca 	*pskb = skb;
502b863ceb7SPatrick McHardy 	skb->dev = dev;
503b863ceb7SPatrick McHardy 	skb->pkt_type = PACKET_HOST;
504b863ceb7SPatrick McHardy 
505d1dd9119Sjbaron@akamai.com 	ret = NET_RX_SUCCESS;
506d1dd9119Sjbaron@akamai.com 	handle_res = RX_HANDLER_ANOTHER;
507ba01877fSSridhar Samudrala out:
5084c979935Sjbaron@akamai.com 	macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, false);
509d1dd9119Sjbaron@akamai.com 	return handle_res;
510b863ceb7SPatrick McHardy }
511b863ceb7SPatrick McHardy 
512618e1b74SArnd Bergmann static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
513618e1b74SArnd Bergmann {
514618e1b74SArnd Bergmann 	const struct macvlan_dev *vlan = netdev_priv(dev);
515618e1b74SArnd Bergmann 	const struct macvlan_port *port = vlan->port;
516618e1b74SArnd Bergmann 	const struct macvlan_dev *dest;
517618e1b74SArnd Bergmann 
518618e1b74SArnd Bergmann 	if (vlan->mode == MACVLAN_MODE_BRIDGE) {
519618e1b74SArnd Bergmann 		const struct ethhdr *eth = (void *)skb->data;
520618e1b74SArnd Bergmann 
521618e1b74SArnd Bergmann 		/* send to other bridge ports directly */
522618e1b74SArnd Bergmann 		if (is_multicast_ether_addr(eth->h_dest)) {
523618e1b74SArnd Bergmann 			macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE);
524618e1b74SArnd Bergmann 			goto xmit_world;
525618e1b74SArnd Bergmann 		}
526618e1b74SArnd Bergmann 
527618e1b74SArnd Bergmann 		dest = macvlan_hash_lookup(port, eth->h_dest);
528618e1b74SArnd Bergmann 		if (dest && dest->mode == MACVLAN_MODE_BRIDGE) {
529a37dd333SDavid Ward 			/* send to lowerdev first for its network taps */
530cb2d0f3eSDavid Ward 			dev_forward_skb(vlan->lowerdev, skb);
531618e1b74SArnd Bergmann 
532618e1b74SArnd Bergmann 			return NET_XMIT_SUCCESS;
533618e1b74SArnd Bergmann 		}
534618e1b74SArnd Bergmann 	}
535618e1b74SArnd Bergmann xmit_world:
53659b9997bSDavid S. Miller 	skb->dev = vlan->lowerdev;
537eadec877SAlexander Duyck 	return dev_queue_xmit_accel(skb,
538eadec877SAlexander Duyck 				    netdev_get_sb_channel(dev) ? dev : NULL);
539618e1b74SArnd Bergmann }
540618e1b74SArnd Bergmann 
541688cea83Sdingtianhong static inline netdev_tx_t macvlan_netpoll_send_skb(struct macvlan_dev *vlan, struct sk_buff *skb)
542688cea83Sdingtianhong {
543688cea83Sdingtianhong #ifdef CONFIG_NET_POLL_CONTROLLER
544688cea83Sdingtianhong 	if (vlan->netpoll)
545688cea83Sdingtianhong 		netpoll_send_skb(vlan->netpoll, skb);
546688cea83Sdingtianhong #else
547688cea83Sdingtianhong 	BUG();
548688cea83Sdingtianhong #endif
549688cea83Sdingtianhong 	return NETDEV_TX_OK;
550688cea83Sdingtianhong }
551688cea83Sdingtianhong 
5520db901bdSstephen hemminger static netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
553424efe9cSStephen Hemminger 				      struct net_device *dev)
554b863ceb7SPatrick McHardy {
55581d4e91cSAlexander Duyck 	struct macvlan_dev *vlan = netdev_priv(dev);
556b863ceb7SPatrick McHardy 	unsigned int len = skb->len;
557b863ceb7SPatrick McHardy 	int ret;
558688cea83Sdingtianhong 
559688cea83Sdingtianhong 	if (unlikely(netpoll_tx_running(dev)))
560688cea83Sdingtianhong 		return macvlan_netpoll_send_skb(vlan, skb);
561b863ceb7SPatrick McHardy 
562618e1b74SArnd Bergmann 	ret = macvlan_queue_xmit(skb, dev);
563a6cc0cfaSJohn Fastabend 
5642d6c9ffcSEric Dumazet 	if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
565cdf3e274SLi RongQing 		struct vlan_pcpu_stats *pcpu_stats;
5662c114553SEric Dumazet 
5678ffab51bSEric Dumazet 		pcpu_stats = this_cpu_ptr(vlan->pcpu_stats);
5688ffab51bSEric Dumazet 		u64_stats_update_begin(&pcpu_stats->syncp);
5698ffab51bSEric Dumazet 		pcpu_stats->tx_packets++;
5708ffab51bSEric Dumazet 		pcpu_stats->tx_bytes += len;
5718ffab51bSEric Dumazet 		u64_stats_update_end(&pcpu_stats->syncp);
5728ffab51bSEric Dumazet 	} else {
5738ffab51bSEric Dumazet 		this_cpu_inc(vlan->pcpu_stats->tx_dropped);
5748ffab51bSEric Dumazet 	}
575cbbef5e1SPatrick McHardy 	return ret;
576b863ceb7SPatrick McHardy }
577b863ceb7SPatrick McHardy 
578b863ceb7SPatrick McHardy static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
5793b04dddeSStephen Hemminger 			       unsigned short type, const void *daddr,
5803b04dddeSStephen Hemminger 			       const void *saddr, unsigned len)
581b863ceb7SPatrick McHardy {
582b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan = netdev_priv(dev);
583b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
584b863ceb7SPatrick McHardy 
5850c4e8581SStephen Hemminger 	return dev_hard_header(skb, lowerdev, type, daddr,
586b863ceb7SPatrick McHardy 			       saddr ? : dev->dev_addr, len);
587b863ceb7SPatrick McHardy }
588b863ceb7SPatrick McHardy 
5893b04dddeSStephen Hemminger static const struct header_ops macvlan_hard_header_ops = {
5903b04dddeSStephen Hemminger 	.create  	= macvlan_hard_header,
5913b04dddeSStephen Hemminger 	.parse		= eth_header_parse,
5923b04dddeSStephen Hemminger 	.cache		= eth_header_cache,
5933b04dddeSStephen Hemminger 	.cache_update	= eth_header_cache_update,
5943b04dddeSStephen Hemminger };
5953b04dddeSStephen Hemminger 
596b863ceb7SPatrick McHardy static int macvlan_open(struct net_device *dev)
597b863ceb7SPatrick McHardy {
598b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
599b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
600b863ceb7SPatrick McHardy 	int err;
601b863ceb7SPatrick McHardy 
60243c2d578SVlad Yasevich 	if (macvlan_passthru(vlan->port)) {
60378738141SMichael S. Tsirkin 		if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) {
60478738141SMichael S. Tsirkin 			err = dev_set_promiscuity(lowerdev, 1);
60578738141SMichael S. Tsirkin 			if (err < 0)
60678738141SMichael S. Tsirkin 				goto out;
60778738141SMichael S. Tsirkin 		}
608eb06acdcSSridhar Samudrala 		goto hash_add;
609eb06acdcSSridhar Samudrala 	}
610eb06acdcSSridhar Samudrala 
6117d775f63SAlexander Duyck 	err = -EBUSY;
6127d775f63SAlexander Duyck 	if (macvlan_addr_busy(vlan->port, dev->dev_addr))
6137d775f63SAlexander Duyck 		goto out;
6147d775f63SAlexander Duyck 
6157d775f63SAlexander Duyck 	/* Attempt to populate accel_priv which is used to offload the L2
6167d775f63SAlexander Duyck 	 * forwarding requests for unicast packets.
6177d775f63SAlexander Duyck 	 */
61881d4e91cSAlexander Duyck 	if (lowerdev->features & NETIF_F_HW_L2FW_DOFFLOAD)
6197d775f63SAlexander Duyck 		vlan->accel_priv =
620a6cc0cfaSJohn Fastabend 		      lowerdev->netdev_ops->ndo_dfwd_add_station(lowerdev, dev);
621a6cc0cfaSJohn Fastabend 
62281d4e91cSAlexander Duyck 	/* If earlier attempt to offload failed, or accel_priv is not
62381d4e91cSAlexander Duyck 	 * populated we must add the unicast address to the lower device.
624a6cc0cfaSJohn Fastabend 	 */
62581d4e91cSAlexander Duyck 	if (IS_ERR_OR_NULL(vlan->accel_priv)) {
6267d775f63SAlexander Duyck 		vlan->accel_priv = NULL;
627a748ee24SJiri Pirko 		err = dev_uc_add(lowerdev, dev->dev_addr);
628b863ceb7SPatrick McHardy 		if (err < 0)
629b89fb7daSWang Chen 			goto out;
63081d4e91cSAlexander Duyck 	}
63181d4e91cSAlexander Duyck 
632b89fb7daSWang Chen 	if (dev->flags & IFF_ALLMULTI) {
633b89fb7daSWang Chen 		err = dev_set_allmulti(lowerdev, 1);
634b89fb7daSWang Chen 		if (err < 0)
635b89fb7daSWang Chen 			goto del_unicast;
636b89fb7daSWang Chen 	}
637eb06acdcSSridhar Samudrala 
638efdbd2b3SVlad Yasevich 	if (dev->flags & IFF_PROMISC) {
639efdbd2b3SVlad Yasevich 		err = dev_set_promiscuity(lowerdev, 1);
640efdbd2b3SVlad Yasevich 		if (err < 0)
641efdbd2b3SVlad Yasevich 			goto clear_multi;
642efdbd2b3SVlad Yasevich 	}
643efdbd2b3SVlad Yasevich 
644eb06acdcSSridhar Samudrala hash_add:
645f9ac30f0SEric Biederman 	macvlan_hash_add(vlan);
646b863ceb7SPatrick McHardy 	return 0;
647b89fb7daSWang Chen 
648efdbd2b3SVlad Yasevich clear_multi:
649c3891fa2SGao Feng 	if (dev->flags & IFF_ALLMULTI)
650efdbd2b3SVlad Yasevich 		dev_set_allmulti(lowerdev, -1);
651b89fb7daSWang Chen del_unicast:
6527d775f63SAlexander Duyck 	if (vlan->accel_priv) {
653a6cc0cfaSJohn Fastabend 		lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev,
6547d775f63SAlexander Duyck 							   vlan->accel_priv);
6557d775f63SAlexander Duyck 		vlan->accel_priv = NULL;
65681d4e91cSAlexander Duyck 	} else {
65781d4e91cSAlexander Duyck 		dev_uc_del(lowerdev, dev->dev_addr);
658a6cc0cfaSJohn Fastabend 	}
65981d4e91cSAlexander Duyck out:
660b89fb7daSWang Chen 	return err;
661b863ceb7SPatrick McHardy }
662b863ceb7SPatrick McHardy 
663b863ceb7SPatrick McHardy static int macvlan_stop(struct net_device *dev)
664b863ceb7SPatrick McHardy {
665b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
666b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
667b863ceb7SPatrick McHardy 
6687d775f63SAlexander Duyck 	if (vlan->accel_priv) {
669a6cc0cfaSJohn Fastabend 		lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev,
6707d775f63SAlexander Duyck 							   vlan->accel_priv);
6717d775f63SAlexander Duyck 		vlan->accel_priv = NULL;
672a6cc0cfaSJohn Fastabend 	}
673a6cc0cfaSJohn Fastabend 
674df8ef8f3SJohn Fastabend 	dev_uc_unsync(lowerdev, dev);
675df8ef8f3SJohn Fastabend 	dev_mc_unsync(lowerdev, dev);
676df8ef8f3SJohn Fastabend 
67743c2d578SVlad Yasevich 	if (macvlan_passthru(vlan->port)) {
678df8ef8f3SJohn Fastabend 		if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC))
679eb06acdcSSridhar Samudrala 			dev_set_promiscuity(lowerdev, -1);
680eb06acdcSSridhar Samudrala 		goto hash_del;
681eb06acdcSSridhar Samudrala 	}
682eb06acdcSSridhar Samudrala 
683b863ceb7SPatrick McHardy 	if (dev->flags & IFF_ALLMULTI)
684b863ceb7SPatrick McHardy 		dev_set_allmulti(lowerdev, -1);
685b863ceb7SPatrick McHardy 
686efdbd2b3SVlad Yasevich 	if (dev->flags & IFF_PROMISC)
687efdbd2b3SVlad Yasevich 		dev_set_promiscuity(lowerdev, -1);
688efdbd2b3SVlad Yasevich 
689a748ee24SJiri Pirko 	dev_uc_del(lowerdev, dev->dev_addr);
690b863ceb7SPatrick McHardy 
691eb06acdcSSridhar Samudrala hash_del:
692449f4544SEric Dumazet 	macvlan_hash_del(vlan, !dev->dismantle);
693b863ceb7SPatrick McHardy 	return 0;
694b863ceb7SPatrick McHardy }
695b863ceb7SPatrick McHardy 
696e289fd28Sdingtianhong static int macvlan_sync_address(struct net_device *dev, unsigned char *addr)
697ad5d20a6SPatrick McHardy {
698ad5d20a6SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
699ad5d20a6SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
70018c8c54dSVlad Yasevich 	struct macvlan_port *port = vlan->port;
701ad5d20a6SPatrick McHardy 	int err;
702ad5d20a6SPatrick McHardy 
703f9ac30f0SEric Biederman 	if (!(dev->flags & IFF_UP)) {
704f9ac30f0SEric Biederman 		/* Just copy in the new address */
705e289fd28Sdingtianhong 		ether_addr_copy(dev->dev_addr, addr);
706f9ac30f0SEric Biederman 	} else {
707f9ac30f0SEric Biederman 		/* Rehash and update the device filters */
708e289fd28Sdingtianhong 		if (macvlan_addr_busy(vlan->port, addr))
709f9ac30f0SEric Biederman 			return -EBUSY;
710ad5d20a6SPatrick McHardy 
71118c8c54dSVlad Yasevich 		if (!macvlan_passthru(port)) {
712e289fd28Sdingtianhong 			err = dev_uc_add(lowerdev, addr);
713ccffad25SJiri Pirko 			if (err)
714ad5d20a6SPatrick McHardy 				return err;
715f9ac30f0SEric Biederman 
716a748ee24SJiri Pirko 			dev_uc_del(lowerdev, dev->dev_addr);
717e289fd28Sdingtianhong 		}
718ad5d20a6SPatrick McHardy 
719e289fd28Sdingtianhong 		macvlan_hash_change_addr(vlan, addr);
720f9ac30f0SEric Biederman 	}
72118c8c54dSVlad Yasevich 	if (macvlan_passthru(port) && !macvlan_addr_change(port)) {
72218c8c54dSVlad Yasevich 		/* Since addr_change isn't set, we are here due to lower
72318c8c54dSVlad Yasevich 		 * device change.  Save the lower-dev address so we can
72418c8c54dSVlad Yasevich 		 * restore it later.
72518c8c54dSVlad Yasevich 		 */
72618c8c54dSVlad Yasevich 		ether_addr_copy(vlan->port->perm_addr,
72718c8c54dSVlad Yasevich 				lowerdev->dev_addr);
72818c8c54dSVlad Yasevich 	}
72918c8c54dSVlad Yasevich 	macvlan_clear_addr_change(port);
730ad5d20a6SPatrick McHardy 	return 0;
731ad5d20a6SPatrick McHardy }
732ad5d20a6SPatrick McHardy 
733e289fd28Sdingtianhong static int macvlan_set_mac_address(struct net_device *dev, void *p)
734e289fd28Sdingtianhong {
735e289fd28Sdingtianhong 	struct macvlan_dev *vlan = netdev_priv(dev);
736e289fd28Sdingtianhong 	struct sockaddr *addr = p;
737e289fd28Sdingtianhong 
738e289fd28Sdingtianhong 	if (!is_valid_ether_addr(addr->sa_data))
739e289fd28Sdingtianhong 		return -EADDRNOTAVAIL;
740e289fd28Sdingtianhong 
741e26f43faSVlad Yasevich 	/* If the addresses are the same, this is a no-op */
742e26f43faSVlad Yasevich 	if (ether_addr_equal(dev->dev_addr, addr->sa_data))
743e26f43faSVlad Yasevich 		return 0;
744e26f43faSVlad Yasevich 
745e289fd28Sdingtianhong 	if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
74618c8c54dSVlad Yasevich 		macvlan_set_addr_change(vlan->port);
747a88e2676SZhang Shengju 		return dev_set_mac_address(vlan->lowerdev, addr);
748e289fd28Sdingtianhong 	}
749e289fd28Sdingtianhong 
750e289fd28Sdingtianhong 	return macvlan_sync_address(dev, addr->sa_data);
751e289fd28Sdingtianhong }
752e289fd28Sdingtianhong 
753b863ceb7SPatrick McHardy static void macvlan_change_rx_flags(struct net_device *dev, int change)
754b863ceb7SPatrick McHardy {
755b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
756b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
757b863ceb7SPatrick McHardy 
758bbeb0eadSPeter Christensen 	if (dev->flags & IFF_UP) {
759b863ceb7SPatrick McHardy 		if (change & IFF_ALLMULTI)
760b863ceb7SPatrick McHardy 			dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
761efdbd2b3SVlad Yasevich 		if (change & IFF_PROMISC)
762efdbd2b3SVlad Yasevich 			dev_set_promiscuity(lowerdev,
763efdbd2b3SVlad Yasevich 					    dev->flags & IFF_PROMISC ? 1 : -1);
764efdbd2b3SVlad Yasevich 
765b863ceb7SPatrick McHardy 	}
766bbeb0eadSPeter Christensen }
767b863ceb7SPatrick McHardy 
7689c127a01SHerbert Xu static void macvlan_compute_filter(unsigned long *mc_filter,
7699c127a01SHerbert Xu 				   struct net_device *dev,
7709c127a01SHerbert Xu 				   struct macvlan_dev *vlan)
771b863ceb7SPatrick McHardy {
772cd431e73SEric Dumazet 	if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
7739c127a01SHerbert Xu 		bitmap_fill(mc_filter, MACVLAN_MC_FILTER_SZ);
774cd431e73SEric Dumazet 	} else {
775cd431e73SEric Dumazet 		struct netdev_hw_addr *ha;
776cd431e73SEric Dumazet 		DECLARE_BITMAP(filter, MACVLAN_MC_FILTER_SZ);
777cd431e73SEric Dumazet 
778cd431e73SEric Dumazet 		bitmap_zero(filter, MACVLAN_MC_FILTER_SZ);
779cd431e73SEric Dumazet 		netdev_for_each_mc_addr(ha, dev) {
7803807ff58SEric Dumazet 			__set_bit(mc_hash(vlan, ha->addr), filter);
781cd431e73SEric Dumazet 		}
782d5270430SEric Dumazet 
7833807ff58SEric Dumazet 		__set_bit(mc_hash(vlan, dev->broadcast), filter);
784d5270430SEric Dumazet 
7859c127a01SHerbert Xu 		bitmap_copy(mc_filter, filter, MACVLAN_MC_FILTER_SZ);
786cd431e73SEric Dumazet 	}
7879c127a01SHerbert Xu }
7889c127a01SHerbert Xu 
7899c127a01SHerbert Xu static void macvlan_set_mac_lists(struct net_device *dev)
7909c127a01SHerbert Xu {
7919c127a01SHerbert Xu 	struct macvlan_dev *vlan = netdev_priv(dev);
7929c127a01SHerbert Xu 
7939c127a01SHerbert Xu 	macvlan_compute_filter(vlan->mc_filter, dev, vlan);
7949c127a01SHerbert Xu 
795df8ef8f3SJohn Fastabend 	dev_uc_sync(vlan->lowerdev, dev);
796b863ceb7SPatrick McHardy 	dev_mc_sync(vlan->lowerdev, dev);
7979c127a01SHerbert Xu 
7989c127a01SHerbert Xu 	/* This is slightly inaccurate as we're including the subscription
7999c127a01SHerbert Xu 	 * list of vlan->lowerdev too.
8009c127a01SHerbert Xu 	 *
8019c127a01SHerbert Xu 	 * Bug alert: This only works if everyone has the same broadcast
8029c127a01SHerbert Xu 	 * address as lowerdev.  As soon as someone changes theirs this
8039c127a01SHerbert Xu 	 * will break.
8049c127a01SHerbert Xu 	 *
8059c127a01SHerbert Xu 	 * However, this is already broken as when you change your broadcast
8069c127a01SHerbert Xu 	 * address we don't get called.
8079c127a01SHerbert Xu 	 *
8089c127a01SHerbert Xu 	 * The solution is to maintain a list of broadcast addresses like
8099c127a01SHerbert Xu 	 * we do for uc/mc, if you care.
8109c127a01SHerbert Xu 	 */
8119c127a01SHerbert Xu 	macvlan_compute_filter(vlan->port->mc_filter, vlan->lowerdev, NULL);
812b863ceb7SPatrick McHardy }
813b863ceb7SPatrick McHardy 
814b863ceb7SPatrick McHardy static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
815b863ceb7SPatrick McHardy {
816b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
817b863ceb7SPatrick McHardy 
81891572088SJarod Wilson 	if (vlan->lowerdev->mtu < new_mtu)
819b863ceb7SPatrick McHardy 		return -EINVAL;
820b863ceb7SPatrick McHardy 	dev->mtu = new_mtu;
821b863ceb7SPatrick McHardy 	return 0;
822b863ceb7SPatrick McHardy }
823b863ceb7SPatrick McHardy 
824b863ceb7SPatrick McHardy /*
825b863ceb7SPatrick McHardy  * macvlan network devices have devices nesting below it and are a special
826b863ceb7SPatrick McHardy  * "super class" of normal network devices; split their locks off into a
827b863ceb7SPatrick McHardy  * separate class since they always nest.
828b863ceb7SPatrick McHardy  */
829cf508b12SDavid S. Miller static struct lock_class_key macvlan_netdev_addr_lock_key;
830b863ceb7SPatrick McHardy 
83170957eaeSVlad Yasevich #define ALWAYS_ON_OFFLOADS \
83270957eaeSVlad Yasevich 	(NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE | \
833f21e5077SDimitris Michailidis 	 NETIF_F_GSO_ROBUST | NETIF_F_GSO_ENCAP_ALL)
8348b4703e9SVlad Yasevich 
83570957eaeSVlad Yasevich #define ALWAYS_ON_FEATURES (ALWAYS_ON_OFFLOADS | NETIF_F_LLTX)
83670957eaeSVlad Yasevich 
837b863ceb7SPatrick McHardy #define MACVLAN_FEATURES \
838a188222bSTom Herbert 	(NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
839fb652fdfSDavid S. Miller 	 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_LRO | \
8408d13e670SJohn Fastabend 	 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
84128d2b136SPatrick McHardy 	 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
842b863ceb7SPatrick McHardy 
843b863ceb7SPatrick McHardy #define MACVLAN_STATE_MASK \
844b863ceb7SPatrick McHardy 	((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
845b863ceb7SPatrick McHardy 
846c674ac30SVlad Yasevich static int macvlan_get_nest_level(struct net_device *dev)
847c674ac30SVlad Yasevich {
848c674ac30SVlad Yasevich 	return ((struct macvlan_dev *)netdev_priv(dev))->nest_level;
849c674ac30SVlad Yasevich }
850c674ac30SVlad Yasevich 
851c773e847SDavid S. Miller static void macvlan_set_lockdep_class(struct net_device *dev)
852c773e847SDavid S. Miller {
85324ffd752SEric Dumazet 	netdev_lockdep_set_classes(dev);
854c674ac30SVlad Yasevich 	lockdep_set_class_and_subclass(&dev->addr_list_lock,
855c674ac30SVlad Yasevich 				       &macvlan_netdev_addr_lock_key,
856c674ac30SVlad Yasevich 				       macvlan_get_nest_level(dev));
857c773e847SDavid S. Miller }
858c773e847SDavid S. Miller 
859b863ceb7SPatrick McHardy static int macvlan_init(struct net_device *dev)
860b863ceb7SPatrick McHardy {
861b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
862b863ceb7SPatrick McHardy 	const struct net_device *lowerdev = vlan->lowerdev;
86330837960SFrancesco Ruggeri 	struct macvlan_port *port = vlan->port;
864b863ceb7SPatrick McHardy 
865b863ceb7SPatrick McHardy 	dev->state		= (dev->state & ~MACVLAN_STATE_MASK) |
866b863ceb7SPatrick McHardy 				  (lowerdev->state & MACVLAN_STATE_MASK);
867b863ceb7SPatrick McHardy 	dev->features 		= lowerdev->features & MACVLAN_FEATURES;
8688b4703e9SVlad Yasevich 	dev->features		|= ALWAYS_ON_FEATURES;
86962dbe830SMichal Kubeček 	dev->hw_features	|= NETIF_F_LRO;
870081e83a7SVlad Yasevich 	dev->vlan_features	= lowerdev->vlan_features & MACVLAN_FEATURES;
87170957eaeSVlad Yasevich 	dev->vlan_features	|= ALWAYS_ON_OFFLOADS;
872f21e5077SDimitris Michailidis 	dev->hw_enc_features    |= dev->features;
8738c2acc53SPatrick McHardy 	dev->gso_max_size	= lowerdev->gso_max_size;
874f6773c5eSEric Dumazet 	dev->gso_max_segs	= lowerdev->gso_max_segs;
875ef5c8996Ssg.tweak@gmail.com 	dev->hard_header_len	= lowerdev->hard_header_len;
876b863ceb7SPatrick McHardy 
877c773e847SDavid S. Miller 	macvlan_set_lockdep_class(dev);
878c773e847SDavid S. Miller 
8791c213bd2SWANG Cong 	vlan->pcpu_stats = netdev_alloc_pcpu_stats(struct vlan_pcpu_stats);
8808ffab51bSEric Dumazet 	if (!vlan->pcpu_stats)
881fccaf710SEric Dumazet 		return -ENOMEM;
882fccaf710SEric Dumazet 
88330837960SFrancesco Ruggeri 	port->count += 1;
88430837960SFrancesco Ruggeri 
885b863ceb7SPatrick McHardy 	return 0;
886b863ceb7SPatrick McHardy }
887b863ceb7SPatrick McHardy 
888fccaf710SEric Dumazet static void macvlan_uninit(struct net_device *dev)
889fccaf710SEric Dumazet {
890fccaf710SEric Dumazet 	struct macvlan_dev *vlan = netdev_priv(dev);
891d5cd9244SEric W. Biederman 	struct macvlan_port *port = vlan->port;
892fccaf710SEric Dumazet 
8938ffab51bSEric Dumazet 	free_percpu(vlan->pcpu_stats);
894d5cd9244SEric W. Biederman 
89579cf79abSMichael Braun 	macvlan_flush_sources(port, vlan);
8965e3c516bSDavid S. Miller 	port->count -= 1;
8975e3c516bSDavid S. Miller 	if (!port->count)
898d5cd9244SEric W. Biederman 		macvlan_port_destroy(port->dev);
899fccaf710SEric Dumazet }
900fccaf710SEric Dumazet 
901bc1f4470Sstephen hemminger static void macvlan_dev_get_stats64(struct net_device *dev,
90228172739SEric Dumazet 				    struct rtnl_link_stats64 *stats)
903fccaf710SEric Dumazet {
904fccaf710SEric Dumazet 	struct macvlan_dev *vlan = netdev_priv(dev);
905fccaf710SEric Dumazet 
9068ffab51bSEric Dumazet 	if (vlan->pcpu_stats) {
907cdf3e274SLi RongQing 		struct vlan_pcpu_stats *p;
9088ffab51bSEric Dumazet 		u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
9098ffab51bSEric Dumazet 		u32 rx_errors = 0, tx_dropped = 0;
910bc66154eSEric Dumazet 		unsigned int start;
911fccaf710SEric Dumazet 		int i;
912fccaf710SEric Dumazet 
913fccaf710SEric Dumazet 		for_each_possible_cpu(i) {
9148ffab51bSEric Dumazet 			p = per_cpu_ptr(vlan->pcpu_stats, i);
915bc66154eSEric Dumazet 			do {
91657a7744eSEric W. Biederman 				start = u64_stats_fetch_begin_irq(&p->syncp);
917bc66154eSEric Dumazet 				rx_packets	= p->rx_packets;
918bc66154eSEric Dumazet 				rx_bytes	= p->rx_bytes;
919bc66154eSEric Dumazet 				rx_multicast	= p->rx_multicast;
9208ffab51bSEric Dumazet 				tx_packets	= p->tx_packets;
9218ffab51bSEric Dumazet 				tx_bytes	= p->tx_bytes;
92257a7744eSEric W. Biederman 			} while (u64_stats_fetch_retry_irq(&p->syncp, start));
9238ffab51bSEric Dumazet 
9248ffab51bSEric Dumazet 			stats->rx_packets	+= rx_packets;
9258ffab51bSEric Dumazet 			stats->rx_bytes		+= rx_bytes;
9268ffab51bSEric Dumazet 			stats->multicast	+= rx_multicast;
9278ffab51bSEric Dumazet 			stats->tx_packets	+= tx_packets;
9288ffab51bSEric Dumazet 			stats->tx_bytes		+= tx_bytes;
9298ffab51bSEric Dumazet 			/* rx_errors & tx_dropped are u32, updated
9308ffab51bSEric Dumazet 			 * without syncp protection.
9318ffab51bSEric Dumazet 			 */
9328ffab51bSEric Dumazet 			rx_errors	+= p->rx_errors;
9338ffab51bSEric Dumazet 			tx_dropped	+= p->tx_dropped;
934fccaf710SEric Dumazet 		}
9358ffab51bSEric Dumazet 		stats->rx_errors	= rx_errors;
9368ffab51bSEric Dumazet 		stats->rx_dropped	= rx_errors;
9378ffab51bSEric Dumazet 		stats->tx_dropped	= tx_dropped;
938fccaf710SEric Dumazet 	}
939fccaf710SEric Dumazet }
940fccaf710SEric Dumazet 
9418e586137SJiri Pirko static int macvlan_vlan_rx_add_vid(struct net_device *dev,
94280d5c368SPatrick McHardy 				   __be16 proto, u16 vid)
9438d13e670SJohn Fastabend {
9448d13e670SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
9458d13e670SJohn Fastabend 	struct net_device *lowerdev = vlan->lowerdev;
9468d13e670SJohn Fastabend 
94780d5c368SPatrick McHardy 	return vlan_vid_add(lowerdev, proto, vid);
9488d13e670SJohn Fastabend }
9498d13e670SJohn Fastabend 
9508e586137SJiri Pirko static int macvlan_vlan_rx_kill_vid(struct net_device *dev,
95180d5c368SPatrick McHardy 				    __be16 proto, u16 vid)
9528d13e670SJohn Fastabend {
9538d13e670SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
9548d13e670SJohn Fastabend 	struct net_device *lowerdev = vlan->lowerdev;
9558d13e670SJohn Fastabend 
95680d5c368SPatrick McHardy 	vlan_vid_del(lowerdev, proto, vid);
9578e586137SJiri Pirko 	return 0;
9588d13e670SJohn Fastabend }
9598d13e670SJohn Fastabend 
960edc7d573Sstephen hemminger static int macvlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
961df8ef8f3SJohn Fastabend 			   struct net_device *dev,
962f6f6424bSJiri Pirko 			   const unsigned char *addr, u16 vid,
963df8ef8f3SJohn Fastabend 			   u16 flags)
964df8ef8f3SJohn Fastabend {
965df8ef8f3SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
966df8ef8f3SJohn Fastabend 	int err = -EINVAL;
967df8ef8f3SJohn Fastabend 
9688a50f11cSVlad Yasevich 	/* Support unicast filter only on passthru devices.
9698a50f11cSVlad Yasevich 	 * Multicast filter should be allowed on all devices.
9708a50f11cSVlad Yasevich 	 */
97143c2d578SVlad Yasevich 	if (!macvlan_passthru(vlan->port) && is_unicast_ether_addr(addr))
972df8ef8f3SJohn Fastabend 		return -EOPNOTSUPP;
973df8ef8f3SJohn Fastabend 
974ab2cfbb2SThomas Richter 	if (flags & NLM_F_REPLACE)
975ab2cfbb2SThomas Richter 		return -EOPNOTSUPP;
976ab2cfbb2SThomas Richter 
977df8ef8f3SJohn Fastabend 	if (is_unicast_ether_addr(addr))
978df8ef8f3SJohn Fastabend 		err = dev_uc_add_excl(dev, addr);
979df8ef8f3SJohn Fastabend 	else if (is_multicast_ether_addr(addr))
980df8ef8f3SJohn Fastabend 		err = dev_mc_add_excl(dev, addr);
981df8ef8f3SJohn Fastabend 
982df8ef8f3SJohn Fastabend 	return err;
983df8ef8f3SJohn Fastabend }
984df8ef8f3SJohn Fastabend 
9851690be63SVlad Yasevich static int macvlan_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
986df8ef8f3SJohn Fastabend 			   struct net_device *dev,
987f6f6424bSJiri Pirko 			   const unsigned char *addr, u16 vid)
988df8ef8f3SJohn Fastabend {
989df8ef8f3SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
990df8ef8f3SJohn Fastabend 	int err = -EINVAL;
991df8ef8f3SJohn Fastabend 
9928a50f11cSVlad Yasevich 	/* Support unicast filter only on passthru devices.
9938a50f11cSVlad Yasevich 	 * Multicast filter should be allowed on all devices.
9948a50f11cSVlad Yasevich 	 */
99543c2d578SVlad Yasevich 	if (!macvlan_passthru(vlan->port) && is_unicast_ether_addr(addr))
996df8ef8f3SJohn Fastabend 		return -EOPNOTSUPP;
997df8ef8f3SJohn Fastabend 
998df8ef8f3SJohn Fastabend 	if (is_unicast_ether_addr(addr))
999df8ef8f3SJohn Fastabend 		err = dev_uc_del(dev, addr);
1000df8ef8f3SJohn Fastabend 	else if (is_multicast_ether_addr(addr))
1001df8ef8f3SJohn Fastabend 		err = dev_mc_del(dev, addr);
1002df8ef8f3SJohn Fastabend 
1003df8ef8f3SJohn Fastabend 	return err;
1004df8ef8f3SJohn Fastabend }
1005df8ef8f3SJohn Fastabend 
1006b863ceb7SPatrick McHardy static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
1007b863ceb7SPatrick McHardy 					struct ethtool_drvinfo *drvinfo)
1008b863ceb7SPatrick McHardy {
10097826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "macvlan", sizeof(drvinfo->driver));
10107826d43fSJiri Pirko 	strlcpy(drvinfo->version, "0.1", sizeof(drvinfo->version));
1011b863ceb7SPatrick McHardy }
1012b863ceb7SPatrick McHardy 
101385f95819SDavid Decotigny static int macvlan_ethtool_get_link_ksettings(struct net_device *dev,
101485f95819SDavid Decotigny 					      struct ethtool_link_ksettings *cmd)
10159edb8bb6SStephen Hemminger {
10169edb8bb6SStephen Hemminger 	const struct macvlan_dev *vlan = netdev_priv(dev);
10174bc71cb9SJiri Pirko 
101885f95819SDavid Decotigny 	return __ethtool_get_link_ksettings(vlan->lowerdev, cmd);
10199edb8bb6SStephen Hemminger }
10209edb8bb6SStephen Hemminger 
10212be5c767SVlad Yasevich static netdev_features_t macvlan_fix_features(struct net_device *dev,
10222be5c767SVlad Yasevich 					      netdev_features_t features)
10232be5c767SVlad Yasevich {
10242be5c767SVlad Yasevich 	struct macvlan_dev *vlan = netdev_priv(dev);
102562dbe830SMichal Kubeček 	netdev_features_t lowerdev_features = vlan->lowerdev->features;
1026797f87f8SFlorian Westphal 	netdev_features_t mask;
10272be5c767SVlad Yasevich 
1028797f87f8SFlorian Westphal 	features |= NETIF_F_ALL_FOR_ALL;
1029797f87f8SFlorian Westphal 	features &= (vlan->set_features | ~MACVLAN_FEATURES);
1030797f87f8SFlorian Westphal 	mask = features;
1031797f87f8SFlorian Westphal 
103262dbe830SMichal Kubeček 	lowerdev_features &= (features | ~NETIF_F_LRO);
103362dbe830SMichal Kubeček 	features = netdev_increment_features(lowerdev_features, features, mask);
10348b4703e9SVlad Yasevich 	features |= ALWAYS_ON_FEATURES;
103513fbcc8dSShannon Nelson 	features &= (ALWAYS_ON_FEATURES | MACVLAN_FEATURES);
1036797f87f8SFlorian Westphal 
1037797f87f8SFlorian Westphal 	return features;
10382be5c767SVlad Yasevich }
10392be5c767SVlad Yasevich 
1040688cea83Sdingtianhong #ifdef CONFIG_NET_POLL_CONTROLLER
1041688cea83Sdingtianhong static void macvlan_dev_poll_controller(struct net_device *dev)
1042688cea83Sdingtianhong {
1043688cea83Sdingtianhong 	return;
1044688cea83Sdingtianhong }
1045688cea83Sdingtianhong 
1046688cea83Sdingtianhong static int macvlan_dev_netpoll_setup(struct net_device *dev, struct netpoll_info *npinfo)
1047688cea83Sdingtianhong {
1048688cea83Sdingtianhong 	struct macvlan_dev *vlan = netdev_priv(dev);
1049688cea83Sdingtianhong 	struct net_device *real_dev = vlan->lowerdev;
1050688cea83Sdingtianhong 	struct netpoll *netpoll;
1051688cea83Sdingtianhong 	int err = 0;
1052688cea83Sdingtianhong 
1053688cea83Sdingtianhong 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
1054688cea83Sdingtianhong 	err = -ENOMEM;
1055688cea83Sdingtianhong 	if (!netpoll)
1056688cea83Sdingtianhong 		goto out;
1057688cea83Sdingtianhong 
1058688cea83Sdingtianhong 	err = __netpoll_setup(netpoll, real_dev);
1059688cea83Sdingtianhong 	if (err) {
1060688cea83Sdingtianhong 		kfree(netpoll);
1061688cea83Sdingtianhong 		goto out;
1062688cea83Sdingtianhong 	}
1063688cea83Sdingtianhong 
1064688cea83Sdingtianhong 	vlan->netpoll = netpoll;
1065688cea83Sdingtianhong 
1066688cea83Sdingtianhong out:
1067688cea83Sdingtianhong 	return err;
1068688cea83Sdingtianhong }
1069688cea83Sdingtianhong 
1070688cea83Sdingtianhong static void macvlan_dev_netpoll_cleanup(struct net_device *dev)
1071688cea83Sdingtianhong {
1072688cea83Sdingtianhong 	struct macvlan_dev *vlan = netdev_priv(dev);
1073688cea83Sdingtianhong 	struct netpoll *netpoll = vlan->netpoll;
1074688cea83Sdingtianhong 
1075688cea83Sdingtianhong 	if (!netpoll)
1076688cea83Sdingtianhong 		return;
1077688cea83Sdingtianhong 
1078688cea83Sdingtianhong 	vlan->netpoll = NULL;
1079688cea83Sdingtianhong 
1080*c9fbd71fSDebabrata Banerjee 	__netpoll_free(netpoll);
1081688cea83Sdingtianhong }
1082688cea83Sdingtianhong #endif	/* CONFIG_NET_POLL_CONTROLLER */
1083688cea83Sdingtianhong 
1084ef5fa6bcSNicolas Dichtel static int macvlan_dev_get_iflink(const struct net_device *dev)
1085ef5fa6bcSNicolas Dichtel {
1086ef5fa6bcSNicolas Dichtel 	struct macvlan_dev *vlan = netdev_priv(dev);
1087ef5fa6bcSNicolas Dichtel 
1088ef5fa6bcSNicolas Dichtel 	return vlan->lowerdev->ifindex;
1089ef5fa6bcSNicolas Dichtel }
1090ef5fa6bcSNicolas Dichtel 
1091b863ceb7SPatrick McHardy static const struct ethtool_ops macvlan_ethtool_ops = {
1092b863ceb7SPatrick McHardy 	.get_link		= ethtool_op_get_link,
109385f95819SDavid Decotigny 	.get_link_ksettings	= macvlan_ethtool_get_link_ksettings,
1094b863ceb7SPatrick McHardy 	.get_drvinfo		= macvlan_ethtool_get_drvinfo,
1095b863ceb7SPatrick McHardy };
1096b863ceb7SPatrick McHardy 
109754a30c97SStephen Hemminger static const struct net_device_ops macvlan_netdev_ops = {
109854a30c97SStephen Hemminger 	.ndo_init		= macvlan_init,
1099fccaf710SEric Dumazet 	.ndo_uninit		= macvlan_uninit,
110054a30c97SStephen Hemminger 	.ndo_open		= macvlan_open,
110154a30c97SStephen Hemminger 	.ndo_stop		= macvlan_stop,
110200829823SStephen Hemminger 	.ndo_start_xmit		= macvlan_start_xmit,
110354a30c97SStephen Hemminger 	.ndo_change_mtu		= macvlan_change_mtu,
11042be5c767SVlad Yasevich 	.ndo_fix_features	= macvlan_fix_features,
110554a30c97SStephen Hemminger 	.ndo_change_rx_flags	= macvlan_change_rx_flags,
110654a30c97SStephen Hemminger 	.ndo_set_mac_address	= macvlan_set_mac_address,
1107df8ef8f3SJohn Fastabend 	.ndo_set_rx_mode	= macvlan_set_mac_lists,
1108bc66154eSEric Dumazet 	.ndo_get_stats64	= macvlan_dev_get_stats64,
110954a30c97SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
11108d13e670SJohn Fastabend 	.ndo_vlan_rx_add_vid	= macvlan_vlan_rx_add_vid,
11118d13e670SJohn Fastabend 	.ndo_vlan_rx_kill_vid	= macvlan_vlan_rx_kill_vid,
1112df8ef8f3SJohn Fastabend 	.ndo_fdb_add		= macvlan_fdb_add,
1113df8ef8f3SJohn Fastabend 	.ndo_fdb_del		= macvlan_fdb_del,
1114df8ef8f3SJohn Fastabend 	.ndo_fdb_dump		= ndo_dflt_fdb_dump,
1115c674ac30SVlad Yasevich 	.ndo_get_lock_subclass  = macvlan_get_nest_level,
1116688cea83Sdingtianhong #ifdef CONFIG_NET_POLL_CONTROLLER
1117688cea83Sdingtianhong 	.ndo_poll_controller	= macvlan_dev_poll_controller,
1118688cea83Sdingtianhong 	.ndo_netpoll_setup	= macvlan_dev_netpoll_setup,
1119688cea83Sdingtianhong 	.ndo_netpoll_cleanup	= macvlan_dev_netpoll_cleanup,
1120688cea83Sdingtianhong #endif
1121ef5fa6bcSNicolas Dichtel 	.ndo_get_iflink		= macvlan_dev_get_iflink,
1122f56e67b5SToshiaki Makita 	.ndo_features_check	= passthru_features_check,
112354a30c97SStephen Hemminger };
112454a30c97SStephen Hemminger 
11258a35747aSHerbert Xu void macvlan_common_setup(struct net_device *dev)
1126b863ceb7SPatrick McHardy {
1127b863ceb7SPatrick McHardy 	ether_setup(dev);
1128b863ceb7SPatrick McHardy 
112991572088SJarod Wilson 	dev->min_mtu		= 0;
113091572088SJarod Wilson 	dev->max_mtu		= ETH_MAX_MTU;
113102875878SEric Dumazet 	dev->priv_flags	       &= ~IFF_TX_SKB_SHARING;
113202875878SEric Dumazet 	netif_keep_dst(dev);
113387ab7f6fSVlad Yasevich 	dev->priv_flags	       |= IFF_UNICAST_FLT;
113454a30c97SStephen Hemminger 	dev->netdev_ops		= &macvlan_netdev_ops;
1135cf124db5SDavid S. Miller 	dev->needs_free_netdev	= true;
113671740129SLutz Jaenicke 	dev->header_ops		= &macvlan_hard_header_ops;
1137b863ceb7SPatrick McHardy 	dev->ethtool_ops	= &macvlan_ethtool_ops;
11388a35747aSHerbert Xu }
11398a35747aSHerbert Xu EXPORT_SYMBOL_GPL(macvlan_common_setup);
11408a35747aSHerbert Xu 
11418a35747aSHerbert Xu static void macvlan_setup(struct net_device *dev)
11428a35747aSHerbert Xu {
11438a35747aSHerbert Xu 	macvlan_common_setup(dev);
11447009212bSZhang Shengju 	dev->priv_flags |= IFF_NO_QUEUE;
1145b863ceb7SPatrick McHardy }
1146b863ceb7SPatrick McHardy 
1147b863ceb7SPatrick McHardy static int macvlan_port_create(struct net_device *dev)
1148b863ceb7SPatrick McHardy {
1149b863ceb7SPatrick McHardy 	struct macvlan_port *port;
1150b863ceb7SPatrick McHardy 	unsigned int i;
1151ab95bfe0SJiri Pirko 	int err;
1152b863ceb7SPatrick McHardy 
1153b863ceb7SPatrick McHardy 	if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
1154b863ceb7SPatrick McHardy 		return -EINVAL;
1155b863ceb7SPatrick McHardy 
1156322dc6e0SMahesh Bandewar 	if (netdev_is_rx_handler_busy(dev))
1157d6b00fecSMahesh Bandewar 		return -EBUSY;
1158d6b00fecSMahesh Bandewar 
1159b863ceb7SPatrick McHardy 	port = kzalloc(sizeof(*port), GFP_KERNEL);
1160b863ceb7SPatrick McHardy 	if (port == NULL)
1161b863ceb7SPatrick McHardy 		return -ENOMEM;
1162b863ceb7SPatrick McHardy 
1163b863ceb7SPatrick McHardy 	port->dev = dev;
116418c8c54dSVlad Yasevich 	ether_addr_copy(port->perm_addr, dev->dev_addr);
1165b863ceb7SPatrick McHardy 	INIT_LIST_HEAD(&port->vlans);
1166b863ceb7SPatrick McHardy 	for (i = 0; i < MACVLAN_HASH_SIZE; i++)
1167b863ceb7SPatrick McHardy 		INIT_HLIST_HEAD(&port->vlan_hash[i]);
116879cf79abSMichael Braun 	for (i = 0; i < MACVLAN_HASH_SIZE; i++)
116979cf79abSMichael Braun 		INIT_HLIST_HEAD(&port->vlan_source_hash[i]);
1170ab95bfe0SJiri Pirko 
1171412ca155SHerbert Xu 	skb_queue_head_init(&port->bc_queue);
1172412ca155SHerbert Xu 	INIT_WORK(&port->bc_work, macvlan_process_broadcast);
1173412ca155SHerbert Xu 
1174a35e2c1bSJiri Pirko 	err = netdev_rx_handler_register(dev, macvlan_handle_frame, port);
1175a35e2c1bSJiri Pirko 	if (err)
1176ab95bfe0SJiri Pirko 		kfree(port);
1177d9351561SEric Dumazet 	else
1178a35e2c1bSJiri Pirko 		dev->priv_flags |= IFF_MACVLAN_PORT;
1179ab95bfe0SJiri Pirko 	return err;
1180b863ceb7SPatrick McHardy }
1181b863ceb7SPatrick McHardy 
1182b863ceb7SPatrick McHardy static void macvlan_port_destroy(struct net_device *dev)
1183b863ceb7SPatrick McHardy {
1184e052f7e6SEric Dumazet 	struct macvlan_port *port = macvlan_port_get_rtnl(dev);
1185f6478218SHerbert Xu 	struct sk_buff *skb;
1186b863ceb7SPatrick McHardy 
1187a35e2c1bSJiri Pirko 	dev->priv_flags &= ~IFF_MACVLAN_PORT;
1188ab95bfe0SJiri Pirko 	netdev_rx_handler_unregister(dev);
1189fe0ca732SEric Dumazet 
1190fe0ca732SEric Dumazet 	/* After this point, no packet can schedule bc_work anymore,
1191fe0ca732SEric Dumazet 	 * but we need to cancel it and purge left skbs if any.
1192fe0ca732SEric Dumazet 	 */
1193fe0ca732SEric Dumazet 	cancel_work_sync(&port->bc_work);
1194f6478218SHerbert Xu 
1195f6478218SHerbert Xu 	while ((skb = __skb_dequeue(&port->bc_queue))) {
1196f6478218SHerbert Xu 		const struct macvlan_dev *src = MACVLAN_SKB_CB(skb)->src;
1197f6478218SHerbert Xu 
1198f6478218SHerbert Xu 		if (src)
1199f6478218SHerbert Xu 			dev_put(src->dev);
1200f6478218SHerbert Xu 
1201f6478218SHerbert Xu 		kfree_skb(skb);
1202f6478218SHerbert Xu 	}
1203fe0ca732SEric Dumazet 
120418c8c54dSVlad Yasevich 	/* If the lower device address has been changed by passthru
120518c8c54dSVlad Yasevich 	 * macvlan, put it back.
120618c8c54dSVlad Yasevich 	 */
120718c8c54dSVlad Yasevich 	if (macvlan_passthru(port) &&
120818c8c54dSVlad Yasevich 	    !ether_addr_equal(port->dev->dev_addr, port->perm_addr)) {
120918c8c54dSVlad Yasevich 		struct sockaddr sa;
121018c8c54dSVlad Yasevich 
121118c8c54dSVlad Yasevich 		sa.sa_family = port->dev->type;
121218c8c54dSVlad Yasevich 		memcpy(&sa.sa_data, port->perm_addr, port->dev->addr_len);
121318c8c54dSVlad Yasevich 		dev_set_mac_address(port->dev, &sa);
121418c8c54dSVlad Yasevich 	}
121518c8c54dSVlad Yasevich 
1216a1f5315cSGao Feng 	kfree(port);
1217b863ceb7SPatrick McHardy }
1218b863ceb7SPatrick McHardy 
1219a8b8a889SMatthias Schiffer static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[],
1220a8b8a889SMatthias Schiffer 			    struct netlink_ext_ack *extack)
1221b863ceb7SPatrick McHardy {
1222b863ceb7SPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
1223b863ceb7SPatrick McHardy 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1224b863ceb7SPatrick McHardy 			return -EINVAL;
1225b863ceb7SPatrick McHardy 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1226b863ceb7SPatrick McHardy 			return -EADDRNOTAVAIL;
1227b863ceb7SPatrick McHardy 	}
122827c0b1a8SArnd Bergmann 
122907850a4fSZhang Shengju 	if (!data)
123007850a4fSZhang Shengju 		return 0;
123107850a4fSZhang Shengju 
123207850a4fSZhang Shengju 	if (data[IFLA_MACVLAN_FLAGS] &&
123315127478SMichael S. Tsirkin 	    nla_get_u16(data[IFLA_MACVLAN_FLAGS]) & ~MACVLAN_FLAG_NOPROMISC)
123415127478SMichael S. Tsirkin 		return -EINVAL;
123515127478SMichael S. Tsirkin 
123607850a4fSZhang Shengju 	if (data[IFLA_MACVLAN_MODE]) {
123727c0b1a8SArnd Bergmann 		switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) {
123827c0b1a8SArnd Bergmann 		case MACVLAN_MODE_PRIVATE:
123927c0b1a8SArnd Bergmann 		case MACVLAN_MODE_VEPA:
124027c0b1a8SArnd Bergmann 		case MACVLAN_MODE_BRIDGE:
1241eb06acdcSSridhar Samudrala 		case MACVLAN_MODE_PASSTHRU:
124279cf79abSMichael Braun 		case MACVLAN_MODE_SOURCE:
124327c0b1a8SArnd Bergmann 			break;
124427c0b1a8SArnd Bergmann 		default:
124527c0b1a8SArnd Bergmann 			return -EINVAL;
124627c0b1a8SArnd Bergmann 		}
124727c0b1a8SArnd Bergmann 	}
124879cf79abSMichael Braun 
124907850a4fSZhang Shengju 	if (data[IFLA_MACVLAN_MACADDR_MODE]) {
125079cf79abSMichael Braun 		switch (nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE])) {
125179cf79abSMichael Braun 		case MACVLAN_MACADDR_ADD:
125279cf79abSMichael Braun 		case MACVLAN_MACADDR_DEL:
125379cf79abSMichael Braun 		case MACVLAN_MACADDR_FLUSH:
125479cf79abSMichael Braun 		case MACVLAN_MACADDR_SET:
125579cf79abSMichael Braun 			break;
125679cf79abSMichael Braun 		default:
125779cf79abSMichael Braun 			return -EINVAL;
125879cf79abSMichael Braun 		}
125979cf79abSMichael Braun 	}
126079cf79abSMichael Braun 
126107850a4fSZhang Shengju 	if (data[IFLA_MACVLAN_MACADDR]) {
126279cf79abSMichael Braun 		if (nla_len(data[IFLA_MACVLAN_MACADDR]) != ETH_ALEN)
126379cf79abSMichael Braun 			return -EINVAL;
126479cf79abSMichael Braun 
126579cf79abSMichael Braun 		if (!is_valid_ether_addr(nla_data(data[IFLA_MACVLAN_MACADDR])))
126679cf79abSMichael Braun 			return -EADDRNOTAVAIL;
126779cf79abSMichael Braun 	}
126879cf79abSMichael Braun 
126907850a4fSZhang Shengju 	if (data[IFLA_MACVLAN_MACADDR_COUNT])
127079cf79abSMichael Braun 		return -EINVAL;
127179cf79abSMichael Braun 
127279cf79abSMichael Braun 	return 0;
127379cf79abSMichael Braun }
127479cf79abSMichael Braun 
127579cf79abSMichael Braun /**
127679cf79abSMichael Braun  * reconfigure list of remote source mac address
127779cf79abSMichael Braun  * (only for macvlan devices in source mode)
127879cf79abSMichael Braun  * Note regarding alignment: all netlink data is aligned to 4 Byte, which
127979cf79abSMichael Braun  * suffices for both ether_addr_copy and ether_addr_equal_64bits usage.
128079cf79abSMichael Braun  */
128179cf79abSMichael Braun static int macvlan_changelink_sources(struct macvlan_dev *vlan, u32 mode,
128279cf79abSMichael Braun 				      struct nlattr *data[])
128379cf79abSMichael Braun {
128479cf79abSMichael Braun 	char *addr = NULL;
128579cf79abSMichael Braun 	int ret, rem, len;
128679cf79abSMichael Braun 	struct nlattr *nla, *head;
128779cf79abSMichael Braun 	struct macvlan_source_entry *entry;
128879cf79abSMichael Braun 
128979cf79abSMichael Braun 	if (data[IFLA_MACVLAN_MACADDR])
129079cf79abSMichael Braun 		addr = nla_data(data[IFLA_MACVLAN_MACADDR]);
129179cf79abSMichael Braun 
129279cf79abSMichael Braun 	if (mode == MACVLAN_MACADDR_ADD) {
129379cf79abSMichael Braun 		if (!addr)
129479cf79abSMichael Braun 			return -EINVAL;
129579cf79abSMichael Braun 
129679cf79abSMichael Braun 		return macvlan_hash_add_source(vlan, addr);
129779cf79abSMichael Braun 
129879cf79abSMichael Braun 	} else if (mode == MACVLAN_MACADDR_DEL) {
129979cf79abSMichael Braun 		if (!addr)
130079cf79abSMichael Braun 			return -EINVAL;
130179cf79abSMichael Braun 
130279cf79abSMichael Braun 		entry = macvlan_hash_lookup_source(vlan, addr);
130379cf79abSMichael Braun 		if (entry) {
130479cf79abSMichael Braun 			macvlan_hash_del_source(entry);
130579cf79abSMichael Braun 			vlan->macaddr_count--;
130679cf79abSMichael Braun 		}
130779cf79abSMichael Braun 	} else if (mode == MACVLAN_MACADDR_FLUSH) {
130879cf79abSMichael Braun 		macvlan_flush_sources(vlan->port, vlan);
130979cf79abSMichael Braun 	} else if (mode == MACVLAN_MACADDR_SET) {
131079cf79abSMichael Braun 		macvlan_flush_sources(vlan->port, vlan);
131179cf79abSMichael Braun 
131279cf79abSMichael Braun 		if (addr) {
131379cf79abSMichael Braun 			ret = macvlan_hash_add_source(vlan, addr);
131479cf79abSMichael Braun 			if (ret)
131579cf79abSMichael Braun 				return ret;
131679cf79abSMichael Braun 		}
131779cf79abSMichael Braun 
131879cf79abSMichael Braun 		if (!data || !data[IFLA_MACVLAN_MACADDR_DATA])
131979cf79abSMichael Braun 			return 0;
132079cf79abSMichael Braun 
132179cf79abSMichael Braun 		head = nla_data(data[IFLA_MACVLAN_MACADDR_DATA]);
132279cf79abSMichael Braun 		len = nla_len(data[IFLA_MACVLAN_MACADDR_DATA]);
132379cf79abSMichael Braun 
132479cf79abSMichael Braun 		nla_for_each_attr(nla, head, len, rem) {
132579cf79abSMichael Braun 			if (nla_type(nla) != IFLA_MACVLAN_MACADDR ||
132679cf79abSMichael Braun 			    nla_len(nla) != ETH_ALEN)
132779cf79abSMichael Braun 				continue;
132879cf79abSMichael Braun 
132979cf79abSMichael Braun 			addr = nla_data(nla);
133079cf79abSMichael Braun 			ret = macvlan_hash_add_source(vlan, addr);
133179cf79abSMichael Braun 			if (ret)
133279cf79abSMichael Braun 				return ret;
133379cf79abSMichael Braun 		}
133479cf79abSMichael Braun 	} else {
133579cf79abSMichael Braun 		return -EINVAL;
133679cf79abSMichael Braun 	}
133779cf79abSMichael Braun 
1338b863ceb7SPatrick McHardy 	return 0;
1339b863ceb7SPatrick McHardy }
1340b863ceb7SPatrick McHardy 
1341fc0663d6SArnd Bergmann int macvlan_common_newlink(struct net *src_net, struct net_device *dev,
134242ab19eeSDavid Ahern 			   struct nlattr *tb[], struct nlattr *data[],
134342ab19eeSDavid Ahern 			   struct netlink_ext_ack *extack)
1344b863ceb7SPatrick McHardy {
1345b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
1346b863ceb7SPatrick McHardy 	struct macvlan_port *port;
1347b863ceb7SPatrick McHardy 	struct net_device *lowerdev;
1348b863ceb7SPatrick McHardy 	int err;
134979cf79abSMichael Braun 	int macmode;
1350aa5fd0fbSGao Feng 	bool create = false;
1351b863ceb7SPatrick McHardy 
1352b863ceb7SPatrick McHardy 	if (!tb[IFLA_LINK])
1353b863ceb7SPatrick McHardy 		return -EINVAL;
1354b863ceb7SPatrick McHardy 
135581adee47SEric W. Biederman 	lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
1356b863ceb7SPatrick McHardy 	if (lowerdev == NULL)
1357b863ceb7SPatrick McHardy 		return -ENODEV;
1358b863ceb7SPatrick McHardy 
1359d70f2cf5SKevin Wallace 	/* When creating macvlans or macvtaps on top of other macvlans - use
1360b0832a29SEric Biederman 	 * the real device as the lowerdev.
1361a6ca5f1dSPatrick McHardy 	 */
1362d70f2cf5SKevin Wallace 	if (netif_is_macvlan(lowerdev))
1363d70f2cf5SKevin Wallace 		lowerdev = macvlan_dev_real_dev(lowerdev);
1364a6ca5f1dSPatrick McHardy 
1365b863ceb7SPatrick McHardy 	if (!tb[IFLA_MTU])
1366b863ceb7SPatrick McHardy 		dev->mtu = lowerdev->mtu;
1367b863ceb7SPatrick McHardy 	else if (dev->mtu > lowerdev->mtu)
1368b863ceb7SPatrick McHardy 		return -EINVAL;
1369b863ceb7SPatrick McHardy 
137091572088SJarod Wilson 	/* MTU range: 68 - lowerdev->max_mtu */
137191572088SJarod Wilson 	dev->min_mtu = ETH_MIN_MTU;
137291572088SJarod Wilson 	dev->max_mtu = lowerdev->max_mtu;
137391572088SJarod Wilson 
1374b863ceb7SPatrick McHardy 	if (!tb[IFLA_ADDRESS])
13757ce5d222SDanny Kukawka 		eth_hw_addr_random(dev);
1376b863ceb7SPatrick McHardy 
1377a35e2c1bSJiri Pirko 	if (!macvlan_port_exists(lowerdev)) {
1378b863ceb7SPatrick McHardy 		err = macvlan_port_create(lowerdev);
1379b863ceb7SPatrick McHardy 		if (err < 0)
1380b863ceb7SPatrick McHardy 			return err;
1381aa5fd0fbSGao Feng 		create = true;
1382b863ceb7SPatrick McHardy 	}
1383e052f7e6SEric Dumazet 	port = macvlan_port_get_rtnl(lowerdev);
1384b863ceb7SPatrick McHardy 
1385eb06acdcSSridhar Samudrala 	/* Only 1 macvlan device can be created in passthru mode */
138643c2d578SVlad Yasevich 	if (macvlan_passthru(port)) {
1387aa5fd0fbSGao Feng 		/* The macvlan port must be not created this time,
1388aa5fd0fbSGao Feng 		 * still goto destroy_macvlan_port for readability.
1389aa5fd0fbSGao Feng 		 */
1390aa5fd0fbSGao Feng 		err = -EINVAL;
1391aa5fd0fbSGao Feng 		goto destroy_macvlan_port;
1392aa5fd0fbSGao Feng 	}
1393eb06acdcSSridhar Samudrala 
1394b863ceb7SPatrick McHardy 	vlan->lowerdev = lowerdev;
1395b863ceb7SPatrick McHardy 	vlan->dev      = dev;
1396b863ceb7SPatrick McHardy 	vlan->port     = port;
13972be5c767SVlad Yasevich 	vlan->set_features = MACVLAN_FEATURES;
1398952fcfd0SSabrina Dubroca 	vlan->nest_level = dev_get_nest_level(lowerdev) + 1;
1399b863ceb7SPatrick McHardy 
140027c0b1a8SArnd Bergmann 	vlan->mode     = MACVLAN_MODE_VEPA;
140127c0b1a8SArnd Bergmann 	if (data && data[IFLA_MACVLAN_MODE])
140227c0b1a8SArnd Bergmann 		vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
140327c0b1a8SArnd Bergmann 
1404df8ef8f3SJohn Fastabend 	if (data && data[IFLA_MACVLAN_FLAGS])
1405df8ef8f3SJohn Fastabend 		vlan->flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
1406df8ef8f3SJohn Fastabend 
1407eb06acdcSSridhar Samudrala 	if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
1408aa5fd0fbSGao Feng 		if (port->count) {
1409aa5fd0fbSGao Feng 			err = -EINVAL;
1410aa5fd0fbSGao Feng 			goto destroy_macvlan_port;
1411aa5fd0fbSGao Feng 		}
141243c2d578SVlad Yasevich 		macvlan_set_passthru(port);
14138b98604eSBjørn Mork 		eth_hw_addr_inherit(dev, lowerdev);
1414eb06acdcSSridhar Samudrala 	}
1415eb06acdcSSridhar Samudrala 
141679cf79abSMichael Braun 	if (data && data[IFLA_MACVLAN_MACADDR_MODE]) {
1417aa5fd0fbSGao Feng 		if (vlan->mode != MACVLAN_MODE_SOURCE) {
1418aa5fd0fbSGao Feng 			err = -EINVAL;
1419aa5fd0fbSGao Feng 			goto destroy_macvlan_port;
1420aa5fd0fbSGao Feng 		}
142179cf79abSMichael Braun 		macmode = nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE]);
142279cf79abSMichael Braun 		err = macvlan_changelink_sources(vlan, macmode, data);
142379cf79abSMichael Braun 		if (err)
1424aa5fd0fbSGao Feng 			goto destroy_macvlan_port;
142579cf79abSMichael Braun 	}
142679cf79abSMichael Braun 
142747d4ab91SJohn Fastabend 	err = register_netdevice(dev);
142847d4ab91SJohn Fastabend 	if (err < 0)
1429aa5fd0fbSGao Feng 		goto destroy_macvlan_port;
143047d4ab91SJohn Fastabend 
1431a6cc0cfaSJohn Fastabend 	dev->priv_flags |= IFF_MACVLAN;
143242ab19eeSDavid Ahern 	err = netdev_upper_dev_link(lowerdev, dev, extack);
14337cd43db7SJiri Pirko 	if (err)
1434da37705cSCong Wang 		goto unregister_netdev;
1435b863ceb7SPatrick McHardy 
1436233c7df0SJiri Pirko 	list_add_tail_rcu(&vlan->list, &port->vlans);
1437fc4a7489SPatrick Mullaney 	netif_stacked_transfer_operstate(lowerdev, dev);
1438de7d244dSNikolay Aleksandrov 	linkwatch_fire_event(dev);
1439f16d3d57SJiri Pirko 
1440b863ceb7SPatrick McHardy 	return 0;
1441f16d3d57SJiri Pirko 
1442da37705cSCong Wang unregister_netdev:
1443d02fd6e7SGao Feng 	/* macvlan_uninit would free the macvlan port */
1444da37705cSCong Wang 	unregister_netdevice(dev);
1445d02fd6e7SGao Feng 	return err;
1446aa5fd0fbSGao Feng destroy_macvlan_port:
1447d02fd6e7SGao Feng 	/* the macvlan port may be freed by macvlan_uninit when fail to register.
1448d02fd6e7SGao Feng 	 * so we destroy the macvlan port only when it's valid.
1449d02fd6e7SGao Feng 	 */
14504e14bf42SAlexey Kodanev 	if (create && macvlan_port_get_rtnl(lowerdev))
1451aa5fd0fbSGao Feng 		macvlan_port_destroy(port->dev);
1452f16d3d57SJiri Pirko 	return err;
1453b863ceb7SPatrick McHardy }
1454fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_common_newlink);
1455b863ceb7SPatrick McHardy 
1456fc0663d6SArnd Bergmann static int macvlan_newlink(struct net *src_net, struct net_device *dev,
14577a3f4a18SMatthias Schiffer 			   struct nlattr *tb[], struct nlattr *data[],
14587a3f4a18SMatthias Schiffer 			   struct netlink_ext_ack *extack)
1459fc0663d6SArnd Bergmann {
146042ab19eeSDavid Ahern 	return macvlan_common_newlink(src_net, dev, tb, data, extack);
1461fc0663d6SArnd Bergmann }
1462fc0663d6SArnd Bergmann 
1463fc0663d6SArnd Bergmann void macvlan_dellink(struct net_device *dev, struct list_head *head)
1464b863ceb7SPatrick McHardy {
1465b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
1466b863ceb7SPatrick McHardy 
146779cf79abSMichael Braun 	if (vlan->mode == MACVLAN_MODE_SOURCE)
146879cf79abSMichael Braun 		macvlan_flush_sources(vlan->port, vlan);
1469233c7df0SJiri Pirko 	list_del_rcu(&vlan->list);
147023289a37SEric Dumazet 	unregister_netdevice_queue(dev, head);
14717cd43db7SJiri Pirko 	netdev_upper_dev_unlink(vlan->lowerdev, dev);
1472b863ceb7SPatrick McHardy }
1473fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_dellink);
1474b863ceb7SPatrick McHardy 
147527c0b1a8SArnd Bergmann static int macvlan_changelink(struct net_device *dev,
1476ad744b22SMatthias Schiffer 			      struct nlattr *tb[], struct nlattr *data[],
1477ad744b22SMatthias Schiffer 			      struct netlink_ext_ack *extack)
147827c0b1a8SArnd Bergmann {
147927c0b1a8SArnd Bergmann 	struct macvlan_dev *vlan = netdev_priv(dev);
1480266e8347SMichael S. Tsirkin 	enum macvlan_mode mode;
1481266e8347SMichael S. Tsirkin 	bool set_mode = false;
148279cf79abSMichael Braun 	enum macvlan_macaddr_mode macmode;
148379cf79abSMichael Braun 	int ret;
1484266e8347SMichael S. Tsirkin 
1485266e8347SMichael S. Tsirkin 	/* Validate mode, but don't set yet: setting flags may fail. */
1486266e8347SMichael S. Tsirkin 	if (data && data[IFLA_MACVLAN_MODE]) {
1487266e8347SMichael S. Tsirkin 		set_mode = true;
1488266e8347SMichael S. Tsirkin 		mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
1489266e8347SMichael S. Tsirkin 		/* Passthrough mode can't be set or cleared dynamically */
1490266e8347SMichael S. Tsirkin 		if ((mode == MACVLAN_MODE_PASSTHRU) !=
1491266e8347SMichael S. Tsirkin 		    (vlan->mode == MACVLAN_MODE_PASSTHRU))
1492266e8347SMichael S. Tsirkin 			return -EINVAL;
149379cf79abSMichael Braun 		if (vlan->mode == MACVLAN_MODE_SOURCE &&
149479cf79abSMichael Braun 		    vlan->mode != mode)
149579cf79abSMichael Braun 			macvlan_flush_sources(vlan->port, vlan);
1496266e8347SMichael S. Tsirkin 	}
149799ffc3e7SMichael S. Tsirkin 
1498df8ef8f3SJohn Fastabend 	if (data && data[IFLA_MACVLAN_FLAGS]) {
1499df8ef8f3SJohn Fastabend 		__u16 flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
1500df8ef8f3SJohn Fastabend 		bool promisc = (flags ^ vlan->flags) & MACVLAN_FLAG_NOPROMISC;
150143c2d578SVlad Yasevich 		if (macvlan_passthru(vlan->port) && promisc) {
150299ffc3e7SMichael S. Tsirkin 			int err;
1503df8ef8f3SJohn Fastabend 
150499ffc3e7SMichael S. Tsirkin 			if (flags & MACVLAN_FLAG_NOPROMISC)
150599ffc3e7SMichael S. Tsirkin 				err = dev_set_promiscuity(vlan->lowerdev, -1);
150699ffc3e7SMichael S. Tsirkin 			else
150799ffc3e7SMichael S. Tsirkin 				err = dev_set_promiscuity(vlan->lowerdev, 1);
150899ffc3e7SMichael S. Tsirkin 			if (err < 0)
150999ffc3e7SMichael S. Tsirkin 				return err;
151099ffc3e7SMichael S. Tsirkin 		}
1511df8ef8f3SJohn Fastabend 		vlan->flags = flags;
1512df8ef8f3SJohn Fastabend 	}
1513266e8347SMichael S. Tsirkin 	if (set_mode)
1514266e8347SMichael S. Tsirkin 		vlan->mode = mode;
151579cf79abSMichael Braun 	if (data && data[IFLA_MACVLAN_MACADDR_MODE]) {
151679cf79abSMichael Braun 		if (vlan->mode != MACVLAN_MODE_SOURCE)
151779cf79abSMichael Braun 			return -EINVAL;
151879cf79abSMichael Braun 		macmode = nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE]);
151979cf79abSMichael Braun 		ret = macvlan_changelink_sources(vlan, macmode, data);
152079cf79abSMichael Braun 		if (ret)
152179cf79abSMichael Braun 			return ret;
152279cf79abSMichael Braun 	}
152327c0b1a8SArnd Bergmann 	return 0;
152427c0b1a8SArnd Bergmann }
152527c0b1a8SArnd Bergmann 
152679cf79abSMichael Braun static size_t macvlan_get_size_mac(const struct macvlan_dev *vlan)
152779cf79abSMichael Braun {
152879cf79abSMichael Braun 	if (vlan->macaddr_count == 0)
152979cf79abSMichael Braun 		return 0;
153079cf79abSMichael Braun 	return nla_total_size(0) /* IFLA_MACVLAN_MACADDR_DATA */
153179cf79abSMichael Braun 		+ vlan->macaddr_count * nla_total_size(sizeof(u8) * ETH_ALEN);
153279cf79abSMichael Braun }
153379cf79abSMichael Braun 
153427c0b1a8SArnd Bergmann static size_t macvlan_get_size(const struct net_device *dev)
153527c0b1a8SArnd Bergmann {
153679cf79abSMichael Braun 	struct macvlan_dev *vlan = netdev_priv(dev);
153779cf79abSMichael Braun 
153801fe944fSEric Dumazet 	return (0
153901fe944fSEric Dumazet 		+ nla_total_size(4) /* IFLA_MACVLAN_MODE */
154001fe944fSEric Dumazet 		+ nla_total_size(2) /* IFLA_MACVLAN_FLAGS */
154179cf79abSMichael Braun 		+ nla_total_size(4) /* IFLA_MACVLAN_MACADDR_COUNT */
154279cf79abSMichael Braun 		+ macvlan_get_size_mac(vlan) /* IFLA_MACVLAN_MACADDR */
154301fe944fSEric Dumazet 		);
154427c0b1a8SArnd Bergmann }
154527c0b1a8SArnd Bergmann 
154679cf79abSMichael Braun static int macvlan_fill_info_macaddr(struct sk_buff *skb,
154779cf79abSMichael Braun 				     const struct macvlan_dev *vlan,
154879cf79abSMichael Braun 				     const int i)
154979cf79abSMichael Braun {
155079cf79abSMichael Braun 	struct hlist_head *h = &vlan->port->vlan_source_hash[i];
155179cf79abSMichael Braun 	struct macvlan_source_entry *entry;
155279cf79abSMichael Braun 
155379cf79abSMichael Braun 	hlist_for_each_entry_rcu(entry, h, hlist) {
155479cf79abSMichael Braun 		if (entry->vlan != vlan)
155579cf79abSMichael Braun 			continue;
155679cf79abSMichael Braun 		if (nla_put(skb, IFLA_MACVLAN_MACADDR, ETH_ALEN, entry->addr))
155779cf79abSMichael Braun 			return 1;
155879cf79abSMichael Braun 	}
155979cf79abSMichael Braun 	return 0;
156079cf79abSMichael Braun }
156179cf79abSMichael Braun 
156227c0b1a8SArnd Bergmann static int macvlan_fill_info(struct sk_buff *skb,
156327c0b1a8SArnd Bergmann 				const struct net_device *dev)
156427c0b1a8SArnd Bergmann {
156527c0b1a8SArnd Bergmann 	struct macvlan_dev *vlan = netdev_priv(dev);
156679cf79abSMichael Braun 	int i;
156779cf79abSMichael Braun 	struct nlattr *nest;
156827c0b1a8SArnd Bergmann 
1569ead9a76cSDavid S. Miller 	if (nla_put_u32(skb, IFLA_MACVLAN_MODE, vlan->mode))
1570ead9a76cSDavid S. Miller 		goto nla_put_failure;
1571df8ef8f3SJohn Fastabend 	if (nla_put_u16(skb, IFLA_MACVLAN_FLAGS, vlan->flags))
1572df8ef8f3SJohn Fastabend 		goto nla_put_failure;
157379cf79abSMichael Braun 	if (nla_put_u32(skb, IFLA_MACVLAN_MACADDR_COUNT, vlan->macaddr_count))
157479cf79abSMichael Braun 		goto nla_put_failure;
157579cf79abSMichael Braun 	if (vlan->macaddr_count > 0) {
157679cf79abSMichael Braun 		nest = nla_nest_start(skb, IFLA_MACVLAN_MACADDR_DATA);
157779cf79abSMichael Braun 		if (nest == NULL)
157879cf79abSMichael Braun 			goto nla_put_failure;
157979cf79abSMichael Braun 
158079cf79abSMichael Braun 		for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
158179cf79abSMichael Braun 			if (macvlan_fill_info_macaddr(skb, vlan, i))
158279cf79abSMichael Braun 				goto nla_put_failure;
158379cf79abSMichael Braun 		}
158479cf79abSMichael Braun 		nla_nest_end(skb, nest);
158579cf79abSMichael Braun 	}
158627c0b1a8SArnd Bergmann 	return 0;
158727c0b1a8SArnd Bergmann 
158827c0b1a8SArnd Bergmann nla_put_failure:
158927c0b1a8SArnd Bergmann 	return -EMSGSIZE;
159027c0b1a8SArnd Bergmann }
159127c0b1a8SArnd Bergmann 
159227c0b1a8SArnd Bergmann static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = {
159327c0b1a8SArnd Bergmann 	[IFLA_MACVLAN_MODE]  = { .type = NLA_U32 },
1594df8ef8f3SJohn Fastabend 	[IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 },
159579cf79abSMichael Braun 	[IFLA_MACVLAN_MACADDR_MODE] = { .type = NLA_U32 },
159679cf79abSMichael Braun 	[IFLA_MACVLAN_MACADDR] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
159779cf79abSMichael Braun 	[IFLA_MACVLAN_MACADDR_DATA] = { .type = NLA_NESTED },
159879cf79abSMichael Braun 	[IFLA_MACVLAN_MACADDR_COUNT] = { .type = NLA_U32 },
159927c0b1a8SArnd Bergmann };
160027c0b1a8SArnd Bergmann 
1601fc0663d6SArnd Bergmann int macvlan_link_register(struct rtnl_link_ops *ops)
1602fc0663d6SArnd Bergmann {
1603fc0663d6SArnd Bergmann 	/* common fields */
1604fc0663d6SArnd Bergmann 	ops->validate		= macvlan_validate;
1605fc0663d6SArnd Bergmann 	ops->maxtype		= IFLA_MACVLAN_MAX;
1606fc0663d6SArnd Bergmann 	ops->policy		= macvlan_policy;
1607fc0663d6SArnd Bergmann 	ops->changelink		= macvlan_changelink;
1608fc0663d6SArnd Bergmann 	ops->get_size		= macvlan_get_size;
1609fc0663d6SArnd Bergmann 	ops->fill_info		= macvlan_fill_info;
1610fc0663d6SArnd Bergmann 
1611fc0663d6SArnd Bergmann 	return rtnl_link_register(ops);
1612fc0663d6SArnd Bergmann };
1613fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_link_register);
1614fc0663d6SArnd Bergmann 
1615eaca400fSNicolas Dichtel static struct net *macvlan_get_link_net(const struct net_device *dev)
1616eaca400fSNicolas Dichtel {
1617eaca400fSNicolas Dichtel 	return dev_net(macvlan_dev_real_dev(dev));
1618eaca400fSNicolas Dichtel }
1619eaca400fSNicolas Dichtel 
1620fc0663d6SArnd Bergmann static struct rtnl_link_ops macvlan_link_ops = {
1621b863ceb7SPatrick McHardy 	.kind		= "macvlan",
16228a35747aSHerbert Xu 	.setup		= macvlan_setup,
1623b863ceb7SPatrick McHardy 	.newlink	= macvlan_newlink,
1624b863ceb7SPatrick McHardy 	.dellink	= macvlan_dellink,
1625eaca400fSNicolas Dichtel 	.get_link_net	= macvlan_get_link_net,
16266fe3faf8SSainath Grandhi 	.priv_size      = sizeof(struct macvlan_dev),
1627b863ceb7SPatrick McHardy };
1628b863ceb7SPatrick McHardy 
1629b863ceb7SPatrick McHardy static int macvlan_device_event(struct notifier_block *unused,
1630b863ceb7SPatrick McHardy 				unsigned long event, void *ptr)
1631b863ceb7SPatrick McHardy {
1632351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1633b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan, *next;
1634b863ceb7SPatrick McHardy 	struct macvlan_port *port;
1635226bd341SEric Dumazet 	LIST_HEAD(list_kill);
1636b863ceb7SPatrick McHardy 
1637a35e2c1bSJiri Pirko 	if (!macvlan_port_exists(dev))
1638b863ceb7SPatrick McHardy 		return NOTIFY_DONE;
1639b863ceb7SPatrick McHardy 
1640e052f7e6SEric Dumazet 	port = macvlan_port_get_rtnl(dev);
1641a35e2c1bSJiri Pirko 
1642b863ceb7SPatrick McHardy 	switch (event) {
1643de7d244dSNikolay Aleksandrov 	case NETDEV_UP:
164480fd2d6cSTravis Brown 	case NETDEV_DOWN:
1645b863ceb7SPatrick McHardy 	case NETDEV_CHANGE:
1646b863ceb7SPatrick McHardy 		list_for_each_entry(vlan, &port->vlans, list)
1647fc4a7489SPatrick Mullaney 			netif_stacked_transfer_operstate(vlan->lowerdev,
1648fc4a7489SPatrick Mullaney 							 vlan->dev);
1649b863ceb7SPatrick McHardy 		break;
1650b863ceb7SPatrick McHardy 	case NETDEV_FEAT_CHANGE:
1651b863ceb7SPatrick McHardy 		list_for_each_entry(vlan, &port->vlans, list) {
16528c2acc53SPatrick McHardy 			vlan->dev->gso_max_size = dev->gso_max_size;
1653f6773c5eSEric Dumazet 			vlan->dev->gso_max_segs = dev->gso_max_segs;
1654797f87f8SFlorian Westphal 			netdev_update_features(vlan->dev);
1655b863ceb7SPatrick McHardy 		}
1656b863ceb7SPatrick McHardy 		break;
16573763e7efSdingtianhong 	case NETDEV_CHANGEMTU:
16583763e7efSdingtianhong 		list_for_each_entry(vlan, &port->vlans, list) {
16593763e7efSdingtianhong 			if (vlan->dev->mtu <= dev->mtu)
16603763e7efSdingtianhong 				continue;
16613763e7efSdingtianhong 			dev_set_mtu(vlan->dev, dev->mtu);
16623763e7efSdingtianhong 		}
16633763e7efSdingtianhong 		break;
1664e289fd28Sdingtianhong 	case NETDEV_CHANGEADDR:
166543c2d578SVlad Yasevich 		if (!macvlan_passthru(port))
1666e289fd28Sdingtianhong 			return NOTIFY_DONE;
1667e289fd28Sdingtianhong 
1668e289fd28Sdingtianhong 		vlan = list_first_entry_or_null(&port->vlans,
1669e289fd28Sdingtianhong 						struct macvlan_dev,
1670e289fd28Sdingtianhong 						list);
1671e289fd28Sdingtianhong 
1672e289fd28Sdingtianhong 		if (macvlan_sync_address(vlan->dev, dev->dev_addr))
1673e289fd28Sdingtianhong 			return NOTIFY_BAD;
1674e289fd28Sdingtianhong 
1675e289fd28Sdingtianhong 		break;
1676b863ceb7SPatrick McHardy 	case NETDEV_UNREGISTER:
16773b27e105SDavid Lamparter 		/* twiddle thumbs on netns device moves */
16783b27e105SDavid Lamparter 		if (dev->reg_state != NETREG_UNREGISTERING)
16793b27e105SDavid Lamparter 			break;
16803b27e105SDavid Lamparter 
1681b863ceb7SPatrick McHardy 		list_for_each_entry_safe(vlan, next, &port->vlans, list)
1682226bd341SEric Dumazet 			vlan->dev->rtnl_link_ops->dellink(vlan->dev, &list_kill);
1683226bd341SEric Dumazet 		unregister_netdevice_many(&list_kill);
1684b863ceb7SPatrick McHardy 		break;
16851c01fe14SJiri Pirko 	case NETDEV_PRE_TYPE_CHANGE:
16861c01fe14SJiri Pirko 		/* Forbid underlaying device to change its type. */
16871c01fe14SJiri Pirko 		return NOTIFY_BAD;
16884c991255SVlad Yasevich 
16894c991255SVlad Yasevich 	case NETDEV_NOTIFY_PEERS:
16904c991255SVlad Yasevich 	case NETDEV_BONDING_FAILOVER:
16914c991255SVlad Yasevich 	case NETDEV_RESEND_IGMP:
16924c991255SVlad Yasevich 		/* Propagate to all vlans */
16934c991255SVlad Yasevich 		list_for_each_entry(vlan, &port->vlans, list)
16944c991255SVlad Yasevich 			call_netdevice_notifiers(event, vlan->dev);
1695b863ceb7SPatrick McHardy 	}
1696b863ceb7SPatrick McHardy 	return NOTIFY_DONE;
1697b863ceb7SPatrick McHardy }
1698b863ceb7SPatrick McHardy 
1699b863ceb7SPatrick McHardy static struct notifier_block macvlan_notifier_block __read_mostly = {
1700b863ceb7SPatrick McHardy 	.notifier_call	= macvlan_device_event,
1701b863ceb7SPatrick McHardy };
1702b863ceb7SPatrick McHardy 
1703b863ceb7SPatrick McHardy static int __init macvlan_init_module(void)
1704b863ceb7SPatrick McHardy {
1705b863ceb7SPatrick McHardy 	int err;
1706b863ceb7SPatrick McHardy 
1707b863ceb7SPatrick McHardy 	register_netdevice_notifier(&macvlan_notifier_block);
1708b863ceb7SPatrick McHardy 
1709fc0663d6SArnd Bergmann 	err = macvlan_link_register(&macvlan_link_ops);
1710b863ceb7SPatrick McHardy 	if (err < 0)
1711b863ceb7SPatrick McHardy 		goto err1;
1712b863ceb7SPatrick McHardy 	return 0;
1713b863ceb7SPatrick McHardy err1:
1714b863ceb7SPatrick McHardy 	unregister_netdevice_notifier(&macvlan_notifier_block);
1715b863ceb7SPatrick McHardy 	return err;
1716b863ceb7SPatrick McHardy }
1717b863ceb7SPatrick McHardy 
1718b863ceb7SPatrick McHardy static void __exit macvlan_cleanup_module(void)
1719b863ceb7SPatrick McHardy {
1720b863ceb7SPatrick McHardy 	rtnl_link_unregister(&macvlan_link_ops);
1721b863ceb7SPatrick McHardy 	unregister_netdevice_notifier(&macvlan_notifier_block);
1722b863ceb7SPatrick McHardy }
1723b863ceb7SPatrick McHardy 
1724b863ceb7SPatrick McHardy module_init(macvlan_init_module);
1725b863ceb7SPatrick McHardy module_exit(macvlan_cleanup_module);
1726b863ceb7SPatrick McHardy 
1727b863ceb7SPatrick McHardy MODULE_LICENSE("GPL");
1728b863ceb7SPatrick McHardy MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
1729b863ceb7SPatrick McHardy MODULE_DESCRIPTION("Driver for MAC address based VLANs");
1730b863ceb7SPatrick McHardy MODULE_ALIAS_RTNL_LINK("macvlan");
1731