xref: /linux/drivers/net/macvlan.c (revision 5e3c516b512c0f8f18359413b04918f6347f67e7)
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 
38b863ceb7SPatrick McHardy #define MACVLAN_HASH_SIZE	(1 << BITS_PER_BYTE)
39b863ceb7SPatrick McHardy 
40b863ceb7SPatrick McHardy struct macvlan_port {
41b863ceb7SPatrick McHardy 	struct net_device	*dev;
42b863ceb7SPatrick McHardy 	struct hlist_head	vlan_hash[MACVLAN_HASH_SIZE];
43b863ceb7SPatrick McHardy 	struct list_head	vlans;
448b37ef0aSJiri Pirko 	struct rcu_head		rcu;
45412ca155SHerbert Xu 	struct sk_buff_head	bc_queue;
46412ca155SHerbert Xu 	struct work_struct	bc_work;
47eb06acdcSSridhar Samudrala 	bool 			passthru;
48*5e3c516bSDavid S. Miller 	int			count;
49b863ceb7SPatrick McHardy };
50b863ceb7SPatrick McHardy 
51412ca155SHerbert Xu struct macvlan_skb_cb {
52412ca155SHerbert Xu 	const struct macvlan_dev *src;
53412ca155SHerbert Xu };
54412ca155SHerbert Xu 
55412ca155SHerbert Xu #define MACVLAN_SKB_CB(__skb) ((struct macvlan_skb_cb *)&((__skb)->cb[0]))
56412ca155SHerbert Xu 
57d5cd9244SEric W. Biederman static void macvlan_port_destroy(struct net_device *dev);
58d5cd9244SEric W. Biederman 
59e052f7e6SEric Dumazet static struct macvlan_port *macvlan_port_get_rcu(const struct net_device *dev)
60e052f7e6SEric Dumazet {
61e052f7e6SEric Dumazet 	return rcu_dereference(dev->rx_handler_data);
62e052f7e6SEric Dumazet }
63e052f7e6SEric Dumazet 
64e052f7e6SEric Dumazet static struct macvlan_port *macvlan_port_get_rtnl(const struct net_device *dev)
65e052f7e6SEric Dumazet {
66e052f7e6SEric Dumazet 	return rtnl_dereference(dev->rx_handler_data);
67e052f7e6SEric Dumazet }
68e052f7e6SEric Dumazet 
69a35e2c1bSJiri Pirko #define macvlan_port_exists(dev) (dev->priv_flags & IFF_MACVLAN_PORT)
70a35e2c1bSJiri Pirko 
71b863ceb7SPatrick McHardy static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
72b863ceb7SPatrick McHardy 					       const unsigned char *addr)
73b863ceb7SPatrick McHardy {
74b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan;
75b863ceb7SPatrick McHardy 
76b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(vlan, &port->vlan_hash[addr[5]], hlist) {
77a6700db1SJoe Perches 		if (ether_addr_equal_64bits(vlan->dev->dev_addr, addr))
78b863ceb7SPatrick McHardy 			return vlan;
79b863ceb7SPatrick McHardy 	}
80b863ceb7SPatrick McHardy 	return NULL;
81b863ceb7SPatrick McHardy }
82b863ceb7SPatrick McHardy 
83f9ac30f0SEric Biederman static void macvlan_hash_add(struct macvlan_dev *vlan)
84f9ac30f0SEric Biederman {
85f9ac30f0SEric Biederman 	struct macvlan_port *port = vlan->port;
86f9ac30f0SEric Biederman 	const unsigned char *addr = vlan->dev->dev_addr;
87f9ac30f0SEric Biederman 
88f9ac30f0SEric Biederman 	hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]);
89f9ac30f0SEric Biederman }
90f9ac30f0SEric Biederman 
91449f4544SEric Dumazet static void macvlan_hash_del(struct macvlan_dev *vlan, bool sync)
92f9ac30f0SEric Biederman {
93f9ac30f0SEric Biederman 	hlist_del_rcu(&vlan->hlist);
94449f4544SEric Dumazet 	if (sync)
95f9ac30f0SEric Biederman 		synchronize_rcu();
96f9ac30f0SEric Biederman }
97f9ac30f0SEric Biederman 
98f9ac30f0SEric Biederman static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
99f9ac30f0SEric Biederman 					const unsigned char *addr)
100f9ac30f0SEric Biederman {
101449f4544SEric Dumazet 	macvlan_hash_del(vlan, true);
102f9ac30f0SEric Biederman 	/* Now that we are unhashed it is safe to change the device
103f9ac30f0SEric Biederman 	 * address without confusing packet delivery.
104f9ac30f0SEric Biederman 	 */
105f9ac30f0SEric Biederman 	memcpy(vlan->dev->dev_addr, addr, ETH_ALEN);
106f9ac30f0SEric Biederman 	macvlan_hash_add(vlan);
107f9ac30f0SEric Biederman }
108f9ac30f0SEric Biederman 
109f9ac30f0SEric Biederman static int macvlan_addr_busy(const struct macvlan_port *port,
110f9ac30f0SEric Biederman 				const unsigned char *addr)
111f9ac30f0SEric Biederman {
112f9ac30f0SEric Biederman 	/* Test to see if the specified multicast address is
113f9ac30f0SEric Biederman 	 * currently in use by the underlying device or
114f9ac30f0SEric Biederman 	 * another macvlan.
115f9ac30f0SEric Biederman 	 */
116a6700db1SJoe Perches 	if (ether_addr_equal_64bits(port->dev->dev_addr, addr))
117f9ac30f0SEric Biederman 		return 1;
118f9ac30f0SEric Biederman 
119f9ac30f0SEric Biederman 	if (macvlan_hash_lookup(port, addr))
120f9ac30f0SEric Biederman 		return 1;
121f9ac30f0SEric Biederman 
122f9ac30f0SEric Biederman 	return 0;
123f9ac30f0SEric Biederman }
124f9ac30f0SEric Biederman 
125a1e514c5SArnd Bergmann 
126fc0663d6SArnd Bergmann static int macvlan_broadcast_one(struct sk_buff *skb,
127fc0663d6SArnd Bergmann 				 const struct macvlan_dev *vlan,
128618e1b74SArnd Bergmann 				 const struct ethhdr *eth, bool local)
129a1e514c5SArnd Bergmann {
130fc0663d6SArnd Bergmann 	struct net_device *dev = vlan->dev;
131a1e514c5SArnd Bergmann 
132618e1b74SArnd Bergmann 	if (local)
133412ca155SHerbert Xu 		return __dev_forward_skb(dev, skb);
134618e1b74SArnd Bergmann 
135a1e514c5SArnd Bergmann 	skb->dev = dev;
136a6700db1SJoe Perches 	if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast))
137a1e514c5SArnd Bergmann 		skb->pkt_type = PACKET_BROADCAST;
138a1e514c5SArnd Bergmann 	else
139a1e514c5SArnd Bergmann 		skb->pkt_type = PACKET_MULTICAST;
140a1e514c5SArnd Bergmann 
141412ca155SHerbert Xu 	return 0;
142a1e514c5SArnd Bergmann }
143a1e514c5SArnd Bergmann 
1443807ff58SEric Dumazet static u32 macvlan_hash_mix(const struct macvlan_dev *vlan)
1453807ff58SEric Dumazet {
1463807ff58SEric Dumazet 	return (u32)(((unsigned long)vlan) >> L1_CACHE_SHIFT);
1473807ff58SEric Dumazet }
1483807ff58SEric Dumazet 
1493807ff58SEric Dumazet 
1503807ff58SEric Dumazet static unsigned int mc_hash(const struct macvlan_dev *vlan,
1513807ff58SEric Dumazet 			    const unsigned char *addr)
152cd431e73SEric Dumazet {
153cd431e73SEric Dumazet 	u32 val = __get_unaligned_cpu32(addr + 2);
154cd431e73SEric Dumazet 
1553807ff58SEric Dumazet 	val ^= macvlan_hash_mix(vlan);
156cd431e73SEric Dumazet 	return hash_32(val, MACVLAN_MC_FILTER_BITS);
157cd431e73SEric Dumazet }
158cd431e73SEric Dumazet 
159b863ceb7SPatrick McHardy static void macvlan_broadcast(struct sk_buff *skb,
160618e1b74SArnd Bergmann 			      const struct macvlan_port *port,
161618e1b74SArnd Bergmann 			      struct net_device *src,
162618e1b74SArnd Bergmann 			      enum macvlan_mode mode)
163b863ceb7SPatrick McHardy {
164b863ceb7SPatrick McHardy 	const struct ethhdr *eth = eth_hdr(skb);
165b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan;
166b863ceb7SPatrick McHardy 	struct sk_buff *nskb;
167b863ceb7SPatrick McHardy 	unsigned int i;
168a1e514c5SArnd Bergmann 	int err;
1693807ff58SEric Dumazet 	unsigned int hash;
170b863ceb7SPatrick McHardy 
171efbbced3SPatrick McHardy 	if (skb->protocol == htons(ETH_P_PAUSE))
172efbbced3SPatrick McHardy 		return;
173efbbced3SPatrick McHardy 
174b863ceb7SPatrick McHardy 	for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
175b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(vlan, &port->vlan_hash[i], hlist) {
176618e1b74SArnd Bergmann 			if (vlan->dev == src || !(vlan->mode & mode))
177618e1b74SArnd Bergmann 				continue;
178618e1b74SArnd Bergmann 
1793807ff58SEric Dumazet 			hash = mc_hash(vlan, eth->h_dest);
180cd431e73SEric Dumazet 			if (!test_bit(hash, vlan->mc_filter))
181cd431e73SEric Dumazet 				continue;
182de9e8f3fSHerbert Xu 
183de9e8f3fSHerbert Xu 			err = NET_RX_DROP;
184b863ceb7SPatrick McHardy 			nskb = skb_clone(skb, GFP_ATOMIC);
185de9e8f3fSHerbert Xu 			if (likely(nskb))
186de9e8f3fSHerbert Xu 				err = macvlan_broadcast_one(
187de9e8f3fSHerbert Xu 					nskb, vlan, eth,
188412ca155SHerbert Xu 					mode == MACVLAN_MODE_BRIDGE) ?:
189412ca155SHerbert Xu 				      netif_rx_ni(nskb);
190a1e514c5SArnd Bergmann 			macvlan_count_rx(vlan, skb->len + ETH_HLEN,
191a1e514c5SArnd Bergmann 					 err == NET_RX_SUCCESS, 1);
192b863ceb7SPatrick McHardy 		}
193b863ceb7SPatrick McHardy 	}
194b863ceb7SPatrick McHardy }
195b863ceb7SPatrick McHardy 
196412ca155SHerbert Xu static void macvlan_process_broadcast(struct work_struct *w)
197412ca155SHerbert Xu {
198412ca155SHerbert Xu 	struct macvlan_port *port = container_of(w, struct macvlan_port,
199412ca155SHerbert Xu 						 bc_work);
200412ca155SHerbert Xu 	struct sk_buff *skb;
201412ca155SHerbert Xu 	struct sk_buff_head list;
202412ca155SHerbert Xu 
203412ca155SHerbert Xu 	skb_queue_head_init(&list);
204412ca155SHerbert Xu 
205412ca155SHerbert Xu 	spin_lock_bh(&port->bc_queue.lock);
206412ca155SHerbert Xu 	skb_queue_splice_tail_init(&port->bc_queue, &list);
207412ca155SHerbert Xu 	spin_unlock_bh(&port->bc_queue.lock);
208412ca155SHerbert Xu 
209412ca155SHerbert Xu 	while ((skb = __skb_dequeue(&list))) {
210412ca155SHerbert Xu 		const struct macvlan_dev *src = MACVLAN_SKB_CB(skb)->src;
211412ca155SHerbert Xu 
212412ca155SHerbert Xu 		rcu_read_lock();
213412ca155SHerbert Xu 
214412ca155SHerbert Xu 		if (!src)
215412ca155SHerbert Xu 			/* frame comes from an external address */
216412ca155SHerbert Xu 			macvlan_broadcast(skb, port, NULL,
217412ca155SHerbert Xu 					  MACVLAN_MODE_PRIVATE |
218412ca155SHerbert Xu 					  MACVLAN_MODE_VEPA    |
219412ca155SHerbert Xu 					  MACVLAN_MODE_PASSTHRU|
220412ca155SHerbert Xu 					  MACVLAN_MODE_BRIDGE);
221412ca155SHerbert Xu 		else if (src->mode == MACVLAN_MODE_VEPA)
222412ca155SHerbert Xu 			/* flood to everyone except source */
223412ca155SHerbert Xu 			macvlan_broadcast(skb, port, src->dev,
224412ca155SHerbert Xu 					  MACVLAN_MODE_VEPA |
225412ca155SHerbert Xu 					  MACVLAN_MODE_BRIDGE);
226412ca155SHerbert Xu 		else
227412ca155SHerbert Xu 			/*
228412ca155SHerbert Xu 			 * flood only to VEPA ports, bridge ports
229412ca155SHerbert Xu 			 * already saw the frame on the way out.
230412ca155SHerbert Xu 			 */
231412ca155SHerbert Xu 			macvlan_broadcast(skb, port, src->dev,
232412ca155SHerbert Xu 					  MACVLAN_MODE_VEPA);
233412ca155SHerbert Xu 
234412ca155SHerbert Xu 		rcu_read_unlock();
235412ca155SHerbert Xu 
236412ca155SHerbert Xu 		kfree_skb(skb);
237412ca155SHerbert Xu 	}
238412ca155SHerbert Xu }
239412ca155SHerbert Xu 
240412ca155SHerbert Xu static void macvlan_broadcast_enqueue(struct macvlan_port *port,
241412ca155SHerbert Xu 				      struct sk_buff *skb)
242412ca155SHerbert Xu {
243e676f197SHerbert Xu 	struct sk_buff *nskb;
244412ca155SHerbert Xu 	int err = -ENOMEM;
245412ca155SHerbert Xu 
246e676f197SHerbert Xu 	nskb = skb_clone(skb, GFP_ATOMIC);
247e676f197SHerbert Xu 	if (!nskb)
248412ca155SHerbert Xu 		goto err;
249412ca155SHerbert Xu 
250412ca155SHerbert Xu 	spin_lock(&port->bc_queue.lock);
251412ca155SHerbert Xu 	if (skb_queue_len(&port->bc_queue) < skb->dev->tx_queue_len) {
252e676f197SHerbert Xu 		__skb_queue_tail(&port->bc_queue, nskb);
253412ca155SHerbert Xu 		err = 0;
254412ca155SHerbert Xu 	}
255412ca155SHerbert Xu 	spin_unlock(&port->bc_queue.lock);
256412ca155SHerbert Xu 
257412ca155SHerbert Xu 	if (err)
258e676f197SHerbert Xu 		goto free_nskb;
259412ca155SHerbert Xu 
260412ca155SHerbert Xu 	schedule_work(&port->bc_work);
261412ca155SHerbert Xu 	return;
262412ca155SHerbert Xu 
263e676f197SHerbert Xu free_nskb:
264e676f197SHerbert Xu 	kfree_skb(nskb);
265412ca155SHerbert Xu err:
266412ca155SHerbert Xu 	atomic_long_inc(&skb->dev->rx_dropped);
267412ca155SHerbert Xu }
268412ca155SHerbert Xu 
269b863ceb7SPatrick McHardy /* called under rcu_read_lock() from netif_receive_skb */
2708a4eb573SJiri Pirko static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb)
271b863ceb7SPatrick McHardy {
272ab95bfe0SJiri Pirko 	struct macvlan_port *port;
2738a4eb573SJiri Pirko 	struct sk_buff *skb = *pskb;
274b863ceb7SPatrick McHardy 	const struct ethhdr *eth = eth_hdr(skb);
275b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan;
276618e1b74SArnd Bergmann 	const struct macvlan_dev *src;
277b863ceb7SPatrick McHardy 	struct net_device *dev;
278ba01877fSSridhar Samudrala 	unsigned int len = 0;
279ba01877fSSridhar Samudrala 	int ret = NET_RX_DROP;
280b863ceb7SPatrick McHardy 
281a35e2c1bSJiri Pirko 	port = macvlan_port_get_rcu(skb->dev);
282b863ceb7SPatrick McHardy 	if (is_multicast_ether_addr(eth->h_dest)) {
283bc416d97SEric Dumazet 		skb = ip_check_defrag(skb, IP_DEFRAG_MACVLAN);
284bc416d97SEric Dumazet 		if (!skb)
285bc416d97SEric Dumazet 			return RX_HANDLER_CONSUMED;
2864ec7ac12SEric Dumazet 		eth = eth_hdr(skb);
287618e1b74SArnd Bergmann 		src = macvlan_hash_lookup(port, eth->h_source);
288412ca155SHerbert Xu 		if (src && src->mode != MACVLAN_MODE_VEPA &&
289412ca155SHerbert Xu 		    src->mode != MACVLAN_MODE_BRIDGE) {
290729e72a1Sstephen hemminger 			/* forward to original port. */
291729e72a1Sstephen hemminger 			vlan = src;
292412ca155SHerbert Xu 			ret = macvlan_broadcast_one(skb, vlan, eth, 0) ?:
293412ca155SHerbert Xu 			      netif_rx(skb);
294729e72a1Sstephen hemminger 			goto out;
295729e72a1Sstephen hemminger 		}
296729e72a1Sstephen hemminger 
297412ca155SHerbert Xu 		MACVLAN_SKB_CB(skb)->src = src;
298412ca155SHerbert Xu 		macvlan_broadcast_enqueue(port, skb);
299412ca155SHerbert Xu 
3008a4eb573SJiri Pirko 		return RX_HANDLER_PASS;
301b863ceb7SPatrick McHardy 	}
302b863ceb7SPatrick McHardy 
303eb06acdcSSridhar Samudrala 	if (port->passthru)
304233c7df0SJiri Pirko 		vlan = list_first_or_null_rcu(&port->vlans,
305233c7df0SJiri Pirko 					      struct macvlan_dev, list);
306eb06acdcSSridhar Samudrala 	else
307b863ceb7SPatrick McHardy 		vlan = macvlan_hash_lookup(port, eth->h_dest);
308b863ceb7SPatrick McHardy 	if (vlan == NULL)
3098a4eb573SJiri Pirko 		return RX_HANDLER_PASS;
310b863ceb7SPatrick McHardy 
311b863ceb7SPatrick McHardy 	dev = vlan->dev;
312b863ceb7SPatrick McHardy 	if (unlikely(!(dev->flags & IFF_UP))) {
313b863ceb7SPatrick McHardy 		kfree_skb(skb);
3148a4eb573SJiri Pirko 		return RX_HANDLER_CONSUMED;
315b863ceb7SPatrick McHardy 	}
316a1e514c5SArnd Bergmann 	len = skb->len + ETH_HLEN;
317b863ceb7SPatrick McHardy 	skb = skb_share_check(skb, GFP_ATOMIC);
318a1e514c5SArnd Bergmann 	if (!skb)
319ba01877fSSridhar Samudrala 		goto out;
320b863ceb7SPatrick McHardy 
321b863ceb7SPatrick McHardy 	skb->dev = dev;
322b863ceb7SPatrick McHardy 	skb->pkt_type = PACKET_HOST;
323b863ceb7SPatrick McHardy 
3242f6a1b66SVlad Yasevich 	ret = netif_rx(skb);
325ba01877fSSridhar Samudrala 
326ba01877fSSridhar Samudrala out:
327ba01877fSSridhar Samudrala 	macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, 0);
3288a4eb573SJiri Pirko 	return RX_HANDLER_CONSUMED;
329b863ceb7SPatrick McHardy }
330b863ceb7SPatrick McHardy 
331618e1b74SArnd Bergmann static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
332618e1b74SArnd Bergmann {
333618e1b74SArnd Bergmann 	const struct macvlan_dev *vlan = netdev_priv(dev);
334618e1b74SArnd Bergmann 	const struct macvlan_port *port = vlan->port;
335618e1b74SArnd Bergmann 	const struct macvlan_dev *dest;
336618e1b74SArnd Bergmann 
337618e1b74SArnd Bergmann 	if (vlan->mode == MACVLAN_MODE_BRIDGE) {
338618e1b74SArnd Bergmann 		const struct ethhdr *eth = (void *)skb->data;
339618e1b74SArnd Bergmann 
340618e1b74SArnd Bergmann 		/* send to other bridge ports directly */
341618e1b74SArnd Bergmann 		if (is_multicast_ether_addr(eth->h_dest)) {
342618e1b74SArnd Bergmann 			macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE);
343618e1b74SArnd Bergmann 			goto xmit_world;
344618e1b74SArnd Bergmann 		}
345618e1b74SArnd Bergmann 
346618e1b74SArnd Bergmann 		dest = macvlan_hash_lookup(port, eth->h_dest);
347618e1b74SArnd Bergmann 		if (dest && dest->mode == MACVLAN_MODE_BRIDGE) {
348a37dd333SDavid Ward 			/* send to lowerdev first for its network taps */
349cb2d0f3eSDavid Ward 			dev_forward_skb(vlan->lowerdev, skb);
350618e1b74SArnd Bergmann 
351618e1b74SArnd Bergmann 			return NET_XMIT_SUCCESS;
352618e1b74SArnd Bergmann 		}
353618e1b74SArnd Bergmann 	}
354618e1b74SArnd Bergmann 
355618e1b74SArnd Bergmann xmit_world:
35659b9997bSDavid S. Miller 	skb->dev = vlan->lowerdev;
357618e1b74SArnd Bergmann 	return dev_queue_xmit(skb);
358618e1b74SArnd Bergmann }
359618e1b74SArnd Bergmann 
360688cea83Sdingtianhong static inline netdev_tx_t macvlan_netpoll_send_skb(struct macvlan_dev *vlan, struct sk_buff *skb)
361688cea83Sdingtianhong {
362688cea83Sdingtianhong #ifdef CONFIG_NET_POLL_CONTROLLER
363688cea83Sdingtianhong 	if (vlan->netpoll)
364688cea83Sdingtianhong 		netpoll_send_skb(vlan->netpoll, skb);
365688cea83Sdingtianhong #else
366688cea83Sdingtianhong 	BUG();
367688cea83Sdingtianhong #endif
368688cea83Sdingtianhong 	return NETDEV_TX_OK;
369688cea83Sdingtianhong }
370688cea83Sdingtianhong 
3710db901bdSstephen hemminger static netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
372424efe9cSStephen Hemminger 				      struct net_device *dev)
373b863ceb7SPatrick McHardy {
374b863ceb7SPatrick McHardy 	unsigned int len = skb->len;
375b863ceb7SPatrick McHardy 	int ret;
376688cea83Sdingtianhong 	struct macvlan_dev *vlan = netdev_priv(dev);
377688cea83Sdingtianhong 
378688cea83Sdingtianhong 	if (unlikely(netpoll_tx_running(dev)))
379688cea83Sdingtianhong 		return macvlan_netpoll_send_skb(vlan, skb);
380b863ceb7SPatrick McHardy 
381a6cc0cfaSJohn Fastabend 	if (vlan->fwd_priv) {
382a6cc0cfaSJohn Fastabend 		skb->dev = vlan->lowerdev;
383f663dd9aSJason Wang 		ret = dev_queue_xmit_accel(skb, vlan->fwd_priv);
384a6cc0cfaSJohn Fastabend 	} else {
385618e1b74SArnd Bergmann 		ret = macvlan_queue_xmit(skb, dev);
386a6cc0cfaSJohn Fastabend 	}
387a6cc0cfaSJohn Fastabend 
3882d6c9ffcSEric Dumazet 	if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
389cdf3e274SLi RongQing 		struct vlan_pcpu_stats *pcpu_stats;
3902c114553SEric Dumazet 
3918ffab51bSEric Dumazet 		pcpu_stats = this_cpu_ptr(vlan->pcpu_stats);
3928ffab51bSEric Dumazet 		u64_stats_update_begin(&pcpu_stats->syncp);
3938ffab51bSEric Dumazet 		pcpu_stats->tx_packets++;
3948ffab51bSEric Dumazet 		pcpu_stats->tx_bytes += len;
3958ffab51bSEric Dumazet 		u64_stats_update_end(&pcpu_stats->syncp);
3968ffab51bSEric Dumazet 	} else {
3978ffab51bSEric Dumazet 		this_cpu_inc(vlan->pcpu_stats->tx_dropped);
3988ffab51bSEric Dumazet 	}
399cbbef5e1SPatrick McHardy 	return ret;
400b863ceb7SPatrick McHardy }
401b863ceb7SPatrick McHardy 
402b863ceb7SPatrick McHardy static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
4033b04dddeSStephen Hemminger 			       unsigned short type, const void *daddr,
4043b04dddeSStephen Hemminger 			       const void *saddr, unsigned len)
405b863ceb7SPatrick McHardy {
406b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan = netdev_priv(dev);
407b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
408b863ceb7SPatrick McHardy 
4090c4e8581SStephen Hemminger 	return dev_hard_header(skb, lowerdev, type, daddr,
410b863ceb7SPatrick McHardy 			       saddr ? : dev->dev_addr, len);
411b863ceb7SPatrick McHardy }
412b863ceb7SPatrick McHardy 
4133b04dddeSStephen Hemminger static const struct header_ops macvlan_hard_header_ops = {
4143b04dddeSStephen Hemminger 	.create  	= macvlan_hard_header,
4153b04dddeSStephen Hemminger 	.rebuild	= eth_rebuild_header,
4163b04dddeSStephen Hemminger 	.parse		= eth_header_parse,
4173b04dddeSStephen Hemminger 	.cache		= eth_header_cache,
4183b04dddeSStephen Hemminger 	.cache_update	= eth_header_cache_update,
4193b04dddeSStephen Hemminger };
4203b04dddeSStephen Hemminger 
421b13ba1b8SJason Wang static struct rtnl_link_ops macvlan_link_ops;
422b13ba1b8SJason Wang 
423b863ceb7SPatrick McHardy static int macvlan_open(struct net_device *dev)
424b863ceb7SPatrick McHardy {
425b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
426b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
427b863ceb7SPatrick McHardy 	int err;
428b863ceb7SPatrick McHardy 
429eb06acdcSSridhar Samudrala 	if (vlan->port->passthru) {
43078738141SMichael S. Tsirkin 		if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) {
43178738141SMichael S. Tsirkin 			err = dev_set_promiscuity(lowerdev, 1);
43278738141SMichael S. Tsirkin 			if (err < 0)
43378738141SMichael S. Tsirkin 				goto out;
43478738141SMichael S. Tsirkin 		}
435eb06acdcSSridhar Samudrala 		goto hash_add;
436eb06acdcSSridhar Samudrala 	}
437eb06acdcSSridhar Samudrala 
438b13ba1b8SJason Wang 	if (lowerdev->features & NETIF_F_HW_L2FW_DOFFLOAD &&
439b13ba1b8SJason Wang 	    dev->rtnl_link_ops == &macvlan_link_ops) {
440a6cc0cfaSJohn Fastabend 		vlan->fwd_priv =
441a6cc0cfaSJohn Fastabend 		      lowerdev->netdev_ops->ndo_dfwd_add_station(lowerdev, dev);
442a6cc0cfaSJohn Fastabend 
443a6cc0cfaSJohn Fastabend 		/* If we get a NULL pointer back, or if we get an error
444a6cc0cfaSJohn Fastabend 		 * then we should just fall through to the non accelerated path
445a6cc0cfaSJohn Fastabend 		 */
446a6cc0cfaSJohn Fastabend 		if (IS_ERR_OR_NULL(vlan->fwd_priv)) {
447a6cc0cfaSJohn Fastabend 			vlan->fwd_priv = NULL;
448f663dd9aSJason Wang 		} else
449a6cc0cfaSJohn Fastabend 			return 0;
450a6cc0cfaSJohn Fastabend 	}
451a6cc0cfaSJohn Fastabend 
452f9ac30f0SEric Biederman 	err = -EBUSY;
453f9ac30f0SEric Biederman 	if (macvlan_addr_busy(vlan->port, dev->dev_addr))
454f9ac30f0SEric Biederman 		goto out;
455f9ac30f0SEric Biederman 
456a748ee24SJiri Pirko 	err = dev_uc_add(lowerdev, dev->dev_addr);
457b863ceb7SPatrick McHardy 	if (err < 0)
458b89fb7daSWang Chen 		goto out;
459b89fb7daSWang Chen 	if (dev->flags & IFF_ALLMULTI) {
460b89fb7daSWang Chen 		err = dev_set_allmulti(lowerdev, 1);
461b89fb7daSWang Chen 		if (err < 0)
462b89fb7daSWang Chen 			goto del_unicast;
463b89fb7daSWang Chen 	}
464eb06acdcSSridhar Samudrala 
465eb06acdcSSridhar Samudrala hash_add:
466f9ac30f0SEric Biederman 	macvlan_hash_add(vlan);
467b863ceb7SPatrick McHardy 	return 0;
468b89fb7daSWang Chen 
469b89fb7daSWang Chen del_unicast:
470a748ee24SJiri Pirko 	dev_uc_del(lowerdev, dev->dev_addr);
471b89fb7daSWang Chen out:
472a6cc0cfaSJohn Fastabend 	if (vlan->fwd_priv) {
473a6cc0cfaSJohn Fastabend 		lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev,
474a6cc0cfaSJohn Fastabend 							   vlan->fwd_priv);
475a6cc0cfaSJohn Fastabend 		vlan->fwd_priv = NULL;
476a6cc0cfaSJohn Fastabend 	}
477b89fb7daSWang Chen 	return err;
478b863ceb7SPatrick McHardy }
479b863ceb7SPatrick McHardy 
480b863ceb7SPatrick McHardy static int macvlan_stop(struct net_device *dev)
481b863ceb7SPatrick McHardy {
482b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
483b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
484b863ceb7SPatrick McHardy 
485a6cc0cfaSJohn Fastabend 	if (vlan->fwd_priv) {
486a6cc0cfaSJohn Fastabend 		lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev,
487a6cc0cfaSJohn Fastabend 							   vlan->fwd_priv);
488a6cc0cfaSJohn Fastabend 		vlan->fwd_priv = NULL;
489a6cc0cfaSJohn Fastabend 		return 0;
490a6cc0cfaSJohn Fastabend 	}
491a6cc0cfaSJohn Fastabend 
492df8ef8f3SJohn Fastabend 	dev_uc_unsync(lowerdev, dev);
493df8ef8f3SJohn Fastabend 	dev_mc_unsync(lowerdev, dev);
494df8ef8f3SJohn Fastabend 
495eb06acdcSSridhar Samudrala 	if (vlan->port->passthru) {
496df8ef8f3SJohn Fastabend 		if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC))
497eb06acdcSSridhar Samudrala 			dev_set_promiscuity(lowerdev, -1);
498eb06acdcSSridhar Samudrala 		goto hash_del;
499eb06acdcSSridhar Samudrala 	}
500eb06acdcSSridhar Samudrala 
501b863ceb7SPatrick McHardy 	if (dev->flags & IFF_ALLMULTI)
502b863ceb7SPatrick McHardy 		dev_set_allmulti(lowerdev, -1);
503b863ceb7SPatrick McHardy 
504a748ee24SJiri Pirko 	dev_uc_del(lowerdev, dev->dev_addr);
505b863ceb7SPatrick McHardy 
506eb06acdcSSridhar Samudrala hash_del:
507449f4544SEric Dumazet 	macvlan_hash_del(vlan, !dev->dismantle);
508b863ceb7SPatrick McHardy 	return 0;
509b863ceb7SPatrick McHardy }
510b863ceb7SPatrick McHardy 
511e289fd28Sdingtianhong static int macvlan_sync_address(struct net_device *dev, unsigned char *addr)
512ad5d20a6SPatrick McHardy {
513ad5d20a6SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
514ad5d20a6SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
515ad5d20a6SPatrick McHardy 	int err;
516ad5d20a6SPatrick McHardy 
517f9ac30f0SEric Biederman 	if (!(dev->flags & IFF_UP)) {
518f9ac30f0SEric Biederman 		/* Just copy in the new address */
519e289fd28Sdingtianhong 		ether_addr_copy(dev->dev_addr, addr);
520f9ac30f0SEric Biederman 	} else {
521f9ac30f0SEric Biederman 		/* Rehash and update the device filters */
522e289fd28Sdingtianhong 		if (macvlan_addr_busy(vlan->port, addr))
523f9ac30f0SEric Biederman 			return -EBUSY;
524ad5d20a6SPatrick McHardy 
525e289fd28Sdingtianhong 		if (!vlan->port->passthru) {
526e289fd28Sdingtianhong 			err = dev_uc_add(lowerdev, addr);
527ccffad25SJiri Pirko 			if (err)
528ad5d20a6SPatrick McHardy 				return err;
529f9ac30f0SEric Biederman 
530a748ee24SJiri Pirko 			dev_uc_del(lowerdev, dev->dev_addr);
531e289fd28Sdingtianhong 		}
532ad5d20a6SPatrick McHardy 
533e289fd28Sdingtianhong 		macvlan_hash_change_addr(vlan, addr);
534f9ac30f0SEric Biederman 	}
535ad5d20a6SPatrick McHardy 	return 0;
536ad5d20a6SPatrick McHardy }
537ad5d20a6SPatrick McHardy 
538e289fd28Sdingtianhong static int macvlan_set_mac_address(struct net_device *dev, void *p)
539e289fd28Sdingtianhong {
540e289fd28Sdingtianhong 	struct macvlan_dev *vlan = netdev_priv(dev);
541e289fd28Sdingtianhong 	struct sockaddr *addr = p;
542e289fd28Sdingtianhong 
543e289fd28Sdingtianhong 	if (!is_valid_ether_addr(addr->sa_data))
544e289fd28Sdingtianhong 		return -EADDRNOTAVAIL;
545e289fd28Sdingtianhong 
546e289fd28Sdingtianhong 	if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
547e289fd28Sdingtianhong 		dev_set_mac_address(vlan->lowerdev, addr);
548e289fd28Sdingtianhong 		return 0;
549e289fd28Sdingtianhong 	}
550e289fd28Sdingtianhong 
551e289fd28Sdingtianhong 	return macvlan_sync_address(dev, addr->sa_data);
552e289fd28Sdingtianhong }
553e289fd28Sdingtianhong 
554b863ceb7SPatrick McHardy static void macvlan_change_rx_flags(struct net_device *dev, int change)
555b863ceb7SPatrick McHardy {
556b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
557b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
558b863ceb7SPatrick McHardy 
559bbeb0eadSPeter Christensen 	if (dev->flags & IFF_UP) {
560b863ceb7SPatrick McHardy 		if (change & IFF_ALLMULTI)
561b863ceb7SPatrick McHardy 			dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
562b863ceb7SPatrick McHardy 	}
563bbeb0eadSPeter Christensen }
564b863ceb7SPatrick McHardy 
565df8ef8f3SJohn Fastabend static void macvlan_set_mac_lists(struct net_device *dev)
566b863ceb7SPatrick McHardy {
567b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
568b863ceb7SPatrick McHardy 
569cd431e73SEric Dumazet 	if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
570cd431e73SEric Dumazet 		bitmap_fill(vlan->mc_filter, MACVLAN_MC_FILTER_SZ);
571cd431e73SEric Dumazet 	} else {
572cd431e73SEric Dumazet 		struct netdev_hw_addr *ha;
573cd431e73SEric Dumazet 		DECLARE_BITMAP(filter, MACVLAN_MC_FILTER_SZ);
574cd431e73SEric Dumazet 
575cd431e73SEric Dumazet 		bitmap_zero(filter, MACVLAN_MC_FILTER_SZ);
576cd431e73SEric Dumazet 		netdev_for_each_mc_addr(ha, dev) {
5773807ff58SEric Dumazet 			__set_bit(mc_hash(vlan, ha->addr), filter);
578cd431e73SEric Dumazet 		}
579d5270430SEric Dumazet 
5803807ff58SEric Dumazet 		__set_bit(mc_hash(vlan, dev->broadcast), filter);
581d5270430SEric Dumazet 
582cd431e73SEric Dumazet 		bitmap_copy(vlan->mc_filter, filter, MACVLAN_MC_FILTER_SZ);
583cd431e73SEric Dumazet 	}
584df8ef8f3SJohn Fastabend 	dev_uc_sync(vlan->lowerdev, dev);
585b863ceb7SPatrick McHardy 	dev_mc_sync(vlan->lowerdev, dev);
586b863ceb7SPatrick McHardy }
587b863ceb7SPatrick McHardy 
588b863ceb7SPatrick McHardy static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
589b863ceb7SPatrick McHardy {
590b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
591b863ceb7SPatrick McHardy 
592b863ceb7SPatrick McHardy 	if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
593b863ceb7SPatrick McHardy 		return -EINVAL;
594b863ceb7SPatrick McHardy 	dev->mtu = new_mtu;
595b863ceb7SPatrick McHardy 	return 0;
596b863ceb7SPatrick McHardy }
597b863ceb7SPatrick McHardy 
598b863ceb7SPatrick McHardy /*
599b863ceb7SPatrick McHardy  * macvlan network devices have devices nesting below it and are a special
600b863ceb7SPatrick McHardy  * "super class" of normal network devices; split their locks off into a
601b863ceb7SPatrick McHardy  * separate class since they always nest.
602b863ceb7SPatrick McHardy  */
603b863ceb7SPatrick McHardy static struct lock_class_key macvlan_netdev_xmit_lock_key;
604cf508b12SDavid S. Miller static struct lock_class_key macvlan_netdev_addr_lock_key;
605b863ceb7SPatrick McHardy 
6068b4703e9SVlad Yasevich #define ALWAYS_ON_FEATURES \
6078b4703e9SVlad Yasevich 	(NETIF_F_SG | NETIF_F_GEN_CSUM | NETIF_F_GSO_SOFTWARE | NETIF_F_LLTX)
6088b4703e9SVlad Yasevich 
609b863ceb7SPatrick McHardy #define MACVLAN_FEATURES \
610b863ceb7SPatrick McHardy 	(NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
611b863ceb7SPatrick McHardy 	 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
6128d13e670SJohn Fastabend 	 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
61328d2b136SPatrick McHardy 	 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
614b863ceb7SPatrick McHardy 
615b863ceb7SPatrick McHardy #define MACVLAN_STATE_MASK \
616b863ceb7SPatrick McHardy 	((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
617b863ceb7SPatrick McHardy 
618c674ac30SVlad Yasevich static int macvlan_get_nest_level(struct net_device *dev)
619c674ac30SVlad Yasevich {
620c674ac30SVlad Yasevich 	return ((struct macvlan_dev *)netdev_priv(dev))->nest_level;
621c674ac30SVlad Yasevich }
622c674ac30SVlad Yasevich 
623e8a0464cSDavid S. Miller static void macvlan_set_lockdep_class_one(struct net_device *dev,
624e8a0464cSDavid S. Miller 					  struct netdev_queue *txq,
625e8a0464cSDavid S. Miller 					  void *_unused)
626c773e847SDavid S. Miller {
627c773e847SDavid S. Miller 	lockdep_set_class(&txq->_xmit_lock,
628c773e847SDavid S. Miller 			  &macvlan_netdev_xmit_lock_key);
629c773e847SDavid S. Miller }
630c773e847SDavid S. Miller 
631c773e847SDavid S. Miller static void macvlan_set_lockdep_class(struct net_device *dev)
632c773e847SDavid S. Miller {
633c674ac30SVlad Yasevich 	lockdep_set_class_and_subclass(&dev->addr_list_lock,
634c674ac30SVlad Yasevich 				       &macvlan_netdev_addr_lock_key,
635c674ac30SVlad Yasevich 				       macvlan_get_nest_level(dev));
636e8a0464cSDavid S. Miller 	netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL);
637c773e847SDavid S. Miller }
638c773e847SDavid S. Miller 
639b863ceb7SPatrick McHardy static int macvlan_init(struct net_device *dev)
640b863ceb7SPatrick McHardy {
641b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
642b863ceb7SPatrick McHardy 	const struct net_device *lowerdev = vlan->lowerdev;
643b863ceb7SPatrick McHardy 
644b863ceb7SPatrick McHardy 	dev->state		= (dev->state & ~MACVLAN_STATE_MASK) |
645b863ceb7SPatrick McHardy 				  (lowerdev->state & MACVLAN_STATE_MASK);
646b863ceb7SPatrick McHardy 	dev->features 		= lowerdev->features & MACVLAN_FEATURES;
6478b4703e9SVlad Yasevich 	dev->features		|= ALWAYS_ON_FEATURES;
648081e83a7SVlad Yasevich 	dev->vlan_features	= lowerdev->vlan_features & MACVLAN_FEATURES;
6498c2acc53SPatrick McHardy 	dev->gso_max_size	= lowerdev->gso_max_size;
650b863ceb7SPatrick McHardy 	dev->iflink		= lowerdev->ifindex;
651ef5c8996Ssg.tweak@gmail.com 	dev->hard_header_len	= lowerdev->hard_header_len;
652b863ceb7SPatrick McHardy 
653c773e847SDavid S. Miller 	macvlan_set_lockdep_class(dev);
654c773e847SDavid S. Miller 
6551c213bd2SWANG Cong 	vlan->pcpu_stats = netdev_alloc_pcpu_stats(struct vlan_pcpu_stats);
6568ffab51bSEric Dumazet 	if (!vlan->pcpu_stats)
657fccaf710SEric Dumazet 		return -ENOMEM;
658fccaf710SEric Dumazet 
659b863ceb7SPatrick McHardy 	return 0;
660b863ceb7SPatrick McHardy }
661b863ceb7SPatrick McHardy 
662fccaf710SEric Dumazet static void macvlan_uninit(struct net_device *dev)
663fccaf710SEric Dumazet {
664fccaf710SEric Dumazet 	struct macvlan_dev *vlan = netdev_priv(dev);
665d5cd9244SEric W. Biederman 	struct macvlan_port *port = vlan->port;
666fccaf710SEric Dumazet 
6678ffab51bSEric Dumazet 	free_percpu(vlan->pcpu_stats);
668d5cd9244SEric W. Biederman 
669*5e3c516bSDavid S. Miller 	port->count -= 1;
670*5e3c516bSDavid S. Miller 	if (!port->count)
671d5cd9244SEric W. Biederman 		macvlan_port_destroy(port->dev);
672fccaf710SEric Dumazet }
673fccaf710SEric Dumazet 
67428172739SEric Dumazet static struct rtnl_link_stats64 *macvlan_dev_get_stats64(struct net_device *dev,
67528172739SEric Dumazet 							 struct rtnl_link_stats64 *stats)
676fccaf710SEric Dumazet {
677fccaf710SEric Dumazet 	struct macvlan_dev *vlan = netdev_priv(dev);
678fccaf710SEric Dumazet 
6798ffab51bSEric Dumazet 	if (vlan->pcpu_stats) {
680cdf3e274SLi RongQing 		struct vlan_pcpu_stats *p;
6818ffab51bSEric Dumazet 		u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
6828ffab51bSEric Dumazet 		u32 rx_errors = 0, tx_dropped = 0;
683bc66154eSEric Dumazet 		unsigned int start;
684fccaf710SEric Dumazet 		int i;
685fccaf710SEric Dumazet 
686fccaf710SEric Dumazet 		for_each_possible_cpu(i) {
6878ffab51bSEric Dumazet 			p = per_cpu_ptr(vlan->pcpu_stats, i);
688bc66154eSEric Dumazet 			do {
68957a7744eSEric W. Biederman 				start = u64_stats_fetch_begin_irq(&p->syncp);
690bc66154eSEric Dumazet 				rx_packets	= p->rx_packets;
691bc66154eSEric Dumazet 				rx_bytes	= p->rx_bytes;
692bc66154eSEric Dumazet 				rx_multicast	= p->rx_multicast;
6938ffab51bSEric Dumazet 				tx_packets	= p->tx_packets;
6948ffab51bSEric Dumazet 				tx_bytes	= p->tx_bytes;
69557a7744eSEric W. Biederman 			} while (u64_stats_fetch_retry_irq(&p->syncp, start));
6968ffab51bSEric Dumazet 
6978ffab51bSEric Dumazet 			stats->rx_packets	+= rx_packets;
6988ffab51bSEric Dumazet 			stats->rx_bytes		+= rx_bytes;
6998ffab51bSEric Dumazet 			stats->multicast	+= rx_multicast;
7008ffab51bSEric Dumazet 			stats->tx_packets	+= tx_packets;
7018ffab51bSEric Dumazet 			stats->tx_bytes		+= tx_bytes;
7028ffab51bSEric Dumazet 			/* rx_errors & tx_dropped are u32, updated
7038ffab51bSEric Dumazet 			 * without syncp protection.
7048ffab51bSEric Dumazet 			 */
7058ffab51bSEric Dumazet 			rx_errors	+= p->rx_errors;
7068ffab51bSEric Dumazet 			tx_dropped	+= p->tx_dropped;
707fccaf710SEric Dumazet 		}
7088ffab51bSEric Dumazet 		stats->rx_errors	= rx_errors;
7098ffab51bSEric Dumazet 		stats->rx_dropped	= rx_errors;
7108ffab51bSEric Dumazet 		stats->tx_dropped	= tx_dropped;
711fccaf710SEric Dumazet 	}
712fccaf710SEric Dumazet 	return stats;
713fccaf710SEric Dumazet }
714fccaf710SEric Dumazet 
7158e586137SJiri Pirko static int macvlan_vlan_rx_add_vid(struct net_device *dev,
71680d5c368SPatrick McHardy 				   __be16 proto, u16 vid)
7178d13e670SJohn Fastabend {
7188d13e670SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
7198d13e670SJohn Fastabend 	struct net_device *lowerdev = vlan->lowerdev;
7208d13e670SJohn Fastabend 
72180d5c368SPatrick McHardy 	return vlan_vid_add(lowerdev, proto, vid);
7228d13e670SJohn Fastabend }
7238d13e670SJohn Fastabend 
7248e586137SJiri Pirko static int macvlan_vlan_rx_kill_vid(struct net_device *dev,
72580d5c368SPatrick McHardy 				    __be16 proto, u16 vid)
7268d13e670SJohn Fastabend {
7278d13e670SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
7288d13e670SJohn Fastabend 	struct net_device *lowerdev = vlan->lowerdev;
7298d13e670SJohn Fastabend 
73080d5c368SPatrick McHardy 	vlan_vid_del(lowerdev, proto, vid);
7318e586137SJiri Pirko 	return 0;
7328d13e670SJohn Fastabend }
7338d13e670SJohn Fastabend 
734edc7d573Sstephen hemminger static int macvlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
735df8ef8f3SJohn Fastabend 			   struct net_device *dev,
7366b6e2725Sstephen hemminger 			   const unsigned char *addr,
737df8ef8f3SJohn Fastabend 			   u16 flags)
738df8ef8f3SJohn Fastabend {
739df8ef8f3SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
740df8ef8f3SJohn Fastabend 	int err = -EINVAL;
741df8ef8f3SJohn Fastabend 
742df8ef8f3SJohn Fastabend 	if (!vlan->port->passthru)
743df8ef8f3SJohn Fastabend 		return -EOPNOTSUPP;
744df8ef8f3SJohn Fastabend 
745ab2cfbb2SThomas Richter 	if (flags & NLM_F_REPLACE)
746ab2cfbb2SThomas Richter 		return -EOPNOTSUPP;
747ab2cfbb2SThomas Richter 
748df8ef8f3SJohn Fastabend 	if (is_unicast_ether_addr(addr))
749df8ef8f3SJohn Fastabend 		err = dev_uc_add_excl(dev, addr);
750df8ef8f3SJohn Fastabend 	else if (is_multicast_ether_addr(addr))
751df8ef8f3SJohn Fastabend 		err = dev_mc_add_excl(dev, addr);
752df8ef8f3SJohn Fastabend 
753df8ef8f3SJohn Fastabend 	return err;
754df8ef8f3SJohn Fastabend }
755df8ef8f3SJohn Fastabend 
7561690be63SVlad Yasevich static int macvlan_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
757df8ef8f3SJohn Fastabend 			   struct net_device *dev,
7586b6e2725Sstephen hemminger 			   const unsigned char *addr)
759df8ef8f3SJohn Fastabend {
760df8ef8f3SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
761df8ef8f3SJohn Fastabend 	int err = -EINVAL;
762df8ef8f3SJohn Fastabend 
763df8ef8f3SJohn Fastabend 	if (!vlan->port->passthru)
764df8ef8f3SJohn Fastabend 		return -EOPNOTSUPP;
765df8ef8f3SJohn Fastabend 
766df8ef8f3SJohn Fastabend 	if (is_unicast_ether_addr(addr))
767df8ef8f3SJohn Fastabend 		err = dev_uc_del(dev, addr);
768df8ef8f3SJohn Fastabend 	else if (is_multicast_ether_addr(addr))
769df8ef8f3SJohn Fastabend 		err = dev_mc_del(dev, addr);
770df8ef8f3SJohn Fastabend 
771df8ef8f3SJohn Fastabend 	return err;
772df8ef8f3SJohn Fastabend }
773df8ef8f3SJohn Fastabend 
774b863ceb7SPatrick McHardy static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
775b863ceb7SPatrick McHardy 					struct ethtool_drvinfo *drvinfo)
776b863ceb7SPatrick McHardy {
7777826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "macvlan", sizeof(drvinfo->driver));
7787826d43fSJiri Pirko 	strlcpy(drvinfo->version, "0.1", sizeof(drvinfo->version));
779b863ceb7SPatrick McHardy }
780b863ceb7SPatrick McHardy 
7819edb8bb6SStephen Hemminger static int macvlan_ethtool_get_settings(struct net_device *dev,
7829edb8bb6SStephen Hemminger 					struct ethtool_cmd *cmd)
7839edb8bb6SStephen Hemminger {
7849edb8bb6SStephen Hemminger 	const struct macvlan_dev *vlan = netdev_priv(dev);
7854bc71cb9SJiri Pirko 
7864bc71cb9SJiri Pirko 	return __ethtool_get_settings(vlan->lowerdev, cmd);
7879edb8bb6SStephen Hemminger }
7889edb8bb6SStephen Hemminger 
7892be5c767SVlad Yasevich static netdev_features_t macvlan_fix_features(struct net_device *dev,
7902be5c767SVlad Yasevich 					      netdev_features_t features)
7912be5c767SVlad Yasevich {
7922be5c767SVlad Yasevich 	struct macvlan_dev *vlan = netdev_priv(dev);
793797f87f8SFlorian Westphal 	netdev_features_t mask;
7942be5c767SVlad Yasevich 
795797f87f8SFlorian Westphal 	features |= NETIF_F_ALL_FOR_ALL;
796797f87f8SFlorian Westphal 	features &= (vlan->set_features | ~MACVLAN_FEATURES);
797797f87f8SFlorian Westphal 	mask = features;
798797f87f8SFlorian Westphal 
799797f87f8SFlorian Westphal 	features = netdev_increment_features(vlan->lowerdev->features,
800797f87f8SFlorian Westphal 					     features,
801797f87f8SFlorian Westphal 					     mask);
8028b4703e9SVlad Yasevich 	features |= ALWAYS_ON_FEATURES;
803797f87f8SFlorian Westphal 
804797f87f8SFlorian Westphal 	return features;
8052be5c767SVlad Yasevich }
8062be5c767SVlad Yasevich 
807688cea83Sdingtianhong #ifdef CONFIG_NET_POLL_CONTROLLER
808688cea83Sdingtianhong static void macvlan_dev_poll_controller(struct net_device *dev)
809688cea83Sdingtianhong {
810688cea83Sdingtianhong 	return;
811688cea83Sdingtianhong }
812688cea83Sdingtianhong 
813688cea83Sdingtianhong static int macvlan_dev_netpoll_setup(struct net_device *dev, struct netpoll_info *npinfo)
814688cea83Sdingtianhong {
815688cea83Sdingtianhong 	struct macvlan_dev *vlan = netdev_priv(dev);
816688cea83Sdingtianhong 	struct net_device *real_dev = vlan->lowerdev;
817688cea83Sdingtianhong 	struct netpoll *netpoll;
818688cea83Sdingtianhong 	int err = 0;
819688cea83Sdingtianhong 
820688cea83Sdingtianhong 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
821688cea83Sdingtianhong 	err = -ENOMEM;
822688cea83Sdingtianhong 	if (!netpoll)
823688cea83Sdingtianhong 		goto out;
824688cea83Sdingtianhong 
825688cea83Sdingtianhong 	err = __netpoll_setup(netpoll, real_dev);
826688cea83Sdingtianhong 	if (err) {
827688cea83Sdingtianhong 		kfree(netpoll);
828688cea83Sdingtianhong 		goto out;
829688cea83Sdingtianhong 	}
830688cea83Sdingtianhong 
831688cea83Sdingtianhong 	vlan->netpoll = netpoll;
832688cea83Sdingtianhong 
833688cea83Sdingtianhong out:
834688cea83Sdingtianhong 	return err;
835688cea83Sdingtianhong }
836688cea83Sdingtianhong 
837688cea83Sdingtianhong static void macvlan_dev_netpoll_cleanup(struct net_device *dev)
838688cea83Sdingtianhong {
839688cea83Sdingtianhong 	struct macvlan_dev *vlan = netdev_priv(dev);
840688cea83Sdingtianhong 	struct netpoll *netpoll = vlan->netpoll;
841688cea83Sdingtianhong 
842688cea83Sdingtianhong 	if (!netpoll)
843688cea83Sdingtianhong 		return;
844688cea83Sdingtianhong 
845688cea83Sdingtianhong 	vlan->netpoll = NULL;
846688cea83Sdingtianhong 
847688cea83Sdingtianhong 	__netpoll_free_async(netpoll);
848688cea83Sdingtianhong }
849688cea83Sdingtianhong #endif	/* CONFIG_NET_POLL_CONTROLLER */
850688cea83Sdingtianhong 
851b863ceb7SPatrick McHardy static const struct ethtool_ops macvlan_ethtool_ops = {
852b863ceb7SPatrick McHardy 	.get_link		= ethtool_op_get_link,
8539edb8bb6SStephen Hemminger 	.get_settings		= macvlan_ethtool_get_settings,
854b863ceb7SPatrick McHardy 	.get_drvinfo		= macvlan_ethtool_get_drvinfo,
855b863ceb7SPatrick McHardy };
856b863ceb7SPatrick McHardy 
85754a30c97SStephen Hemminger static const struct net_device_ops macvlan_netdev_ops = {
85854a30c97SStephen Hemminger 	.ndo_init		= macvlan_init,
859fccaf710SEric Dumazet 	.ndo_uninit		= macvlan_uninit,
86054a30c97SStephen Hemminger 	.ndo_open		= macvlan_open,
86154a30c97SStephen Hemminger 	.ndo_stop		= macvlan_stop,
86200829823SStephen Hemminger 	.ndo_start_xmit		= macvlan_start_xmit,
86354a30c97SStephen Hemminger 	.ndo_change_mtu		= macvlan_change_mtu,
8642be5c767SVlad Yasevich 	.ndo_fix_features	= macvlan_fix_features,
86554a30c97SStephen Hemminger 	.ndo_change_rx_flags	= macvlan_change_rx_flags,
86654a30c97SStephen Hemminger 	.ndo_set_mac_address	= macvlan_set_mac_address,
867df8ef8f3SJohn Fastabend 	.ndo_set_rx_mode	= macvlan_set_mac_lists,
868bc66154eSEric Dumazet 	.ndo_get_stats64	= macvlan_dev_get_stats64,
86954a30c97SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
8708d13e670SJohn Fastabend 	.ndo_vlan_rx_add_vid	= macvlan_vlan_rx_add_vid,
8718d13e670SJohn Fastabend 	.ndo_vlan_rx_kill_vid	= macvlan_vlan_rx_kill_vid,
872df8ef8f3SJohn Fastabend 	.ndo_fdb_add		= macvlan_fdb_add,
873df8ef8f3SJohn Fastabend 	.ndo_fdb_del		= macvlan_fdb_del,
874df8ef8f3SJohn Fastabend 	.ndo_fdb_dump		= ndo_dflt_fdb_dump,
875c674ac30SVlad Yasevich 	.ndo_get_lock_subclass  = macvlan_get_nest_level,
876688cea83Sdingtianhong #ifdef CONFIG_NET_POLL_CONTROLLER
877688cea83Sdingtianhong 	.ndo_poll_controller	= macvlan_dev_poll_controller,
878688cea83Sdingtianhong 	.ndo_netpoll_setup	= macvlan_dev_netpoll_setup,
879688cea83Sdingtianhong 	.ndo_netpoll_cleanup	= macvlan_dev_netpoll_cleanup,
880688cea83Sdingtianhong #endif
88154a30c97SStephen Hemminger };
88254a30c97SStephen Hemminger 
8838a35747aSHerbert Xu void macvlan_common_setup(struct net_device *dev)
884b863ceb7SPatrick McHardy {
885b863ceb7SPatrick McHardy 	ether_setup(dev);
886b863ceb7SPatrick McHardy 
887550fd08cSNeil Horman 	dev->priv_flags	       &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
88887ab7f6fSVlad Yasevich 	dev->priv_flags	       |= IFF_UNICAST_FLT;
88954a30c97SStephen Hemminger 	dev->netdev_ops		= &macvlan_netdev_ops;
890b863ceb7SPatrick McHardy 	dev->destructor		= free_netdev;
89171740129SLutz Jaenicke 	dev->header_ops		= &macvlan_hard_header_ops;
892b863ceb7SPatrick McHardy 	dev->ethtool_ops	= &macvlan_ethtool_ops;
8938a35747aSHerbert Xu }
8948a35747aSHerbert Xu EXPORT_SYMBOL_GPL(macvlan_common_setup);
8958a35747aSHerbert Xu 
8968a35747aSHerbert Xu static void macvlan_setup(struct net_device *dev)
8978a35747aSHerbert Xu {
8988a35747aSHerbert Xu 	macvlan_common_setup(dev);
899b863ceb7SPatrick McHardy 	dev->tx_queue_len	= 0;
900b863ceb7SPatrick McHardy }
901b863ceb7SPatrick McHardy 
902b863ceb7SPatrick McHardy static int macvlan_port_create(struct net_device *dev)
903b863ceb7SPatrick McHardy {
904b863ceb7SPatrick McHardy 	struct macvlan_port *port;
905b863ceb7SPatrick McHardy 	unsigned int i;
906ab95bfe0SJiri Pirko 	int err;
907b863ceb7SPatrick McHardy 
908b863ceb7SPatrick McHardy 	if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
909b863ceb7SPatrick McHardy 		return -EINVAL;
910b863ceb7SPatrick McHardy 
911b863ceb7SPatrick McHardy 	port = kzalloc(sizeof(*port), GFP_KERNEL);
912b863ceb7SPatrick McHardy 	if (port == NULL)
913b863ceb7SPatrick McHardy 		return -ENOMEM;
914b863ceb7SPatrick McHardy 
915eb06acdcSSridhar Samudrala 	port->passthru = false;
916b863ceb7SPatrick McHardy 	port->dev = dev;
917b863ceb7SPatrick McHardy 	INIT_LIST_HEAD(&port->vlans);
918b863ceb7SPatrick McHardy 	for (i = 0; i < MACVLAN_HASH_SIZE; i++)
919b863ceb7SPatrick McHardy 		INIT_HLIST_HEAD(&port->vlan_hash[i]);
920ab95bfe0SJiri Pirko 
921412ca155SHerbert Xu 	skb_queue_head_init(&port->bc_queue);
922412ca155SHerbert Xu 	INIT_WORK(&port->bc_work, macvlan_process_broadcast);
923412ca155SHerbert Xu 
924a35e2c1bSJiri Pirko 	err = netdev_rx_handler_register(dev, macvlan_handle_frame, port);
925a35e2c1bSJiri Pirko 	if (err)
926ab95bfe0SJiri Pirko 		kfree(port);
927d9351561SEric Dumazet 	else
928a35e2c1bSJiri Pirko 		dev->priv_flags |= IFF_MACVLAN_PORT;
929ab95bfe0SJiri Pirko 	return err;
930b863ceb7SPatrick McHardy }
931b863ceb7SPatrick McHardy 
932b863ceb7SPatrick McHardy static void macvlan_port_destroy(struct net_device *dev)
933b863ceb7SPatrick McHardy {
934e052f7e6SEric Dumazet 	struct macvlan_port *port = macvlan_port_get_rtnl(dev);
935b863ceb7SPatrick McHardy 
936412ca155SHerbert Xu 	cancel_work_sync(&port->bc_work);
937a35e2c1bSJiri Pirko 	dev->priv_flags &= ~IFF_MACVLAN_PORT;
938ab95bfe0SJiri Pirko 	netdev_rx_handler_unregister(dev);
9396e070aecSLai Jiangshan 	kfree_rcu(port, rcu);
940b863ceb7SPatrick McHardy }
941b863ceb7SPatrick McHardy 
942b863ceb7SPatrick McHardy static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
943b863ceb7SPatrick McHardy {
944b863ceb7SPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
945b863ceb7SPatrick McHardy 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
946b863ceb7SPatrick McHardy 			return -EINVAL;
947b863ceb7SPatrick McHardy 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
948b863ceb7SPatrick McHardy 			return -EADDRNOTAVAIL;
949b863ceb7SPatrick McHardy 	}
95027c0b1a8SArnd Bergmann 
95115127478SMichael S. Tsirkin 	if (data && data[IFLA_MACVLAN_FLAGS] &&
95215127478SMichael S. Tsirkin 	    nla_get_u16(data[IFLA_MACVLAN_FLAGS]) & ~MACVLAN_FLAG_NOPROMISC)
95315127478SMichael S. Tsirkin 		return -EINVAL;
95415127478SMichael S. Tsirkin 
95527c0b1a8SArnd Bergmann 	if (data && data[IFLA_MACVLAN_MODE]) {
95627c0b1a8SArnd Bergmann 		switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) {
95727c0b1a8SArnd Bergmann 		case MACVLAN_MODE_PRIVATE:
95827c0b1a8SArnd Bergmann 		case MACVLAN_MODE_VEPA:
95927c0b1a8SArnd Bergmann 		case MACVLAN_MODE_BRIDGE:
960eb06acdcSSridhar Samudrala 		case MACVLAN_MODE_PASSTHRU:
96127c0b1a8SArnd Bergmann 			break;
96227c0b1a8SArnd Bergmann 		default:
96327c0b1a8SArnd Bergmann 			return -EINVAL;
96427c0b1a8SArnd Bergmann 		}
96527c0b1a8SArnd Bergmann 	}
966b863ceb7SPatrick McHardy 	return 0;
967b863ceb7SPatrick McHardy }
968b863ceb7SPatrick McHardy 
969fc0663d6SArnd Bergmann int macvlan_common_newlink(struct net *src_net, struct net_device *dev,
9702f6a1b66SVlad Yasevich 			   struct nlattr *tb[], struct nlattr *data[])
971b863ceb7SPatrick McHardy {
972b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
973b863ceb7SPatrick McHardy 	struct macvlan_port *port;
974b863ceb7SPatrick McHardy 	struct net_device *lowerdev;
975b863ceb7SPatrick McHardy 	int err;
976b863ceb7SPatrick McHardy 
977b863ceb7SPatrick McHardy 	if (!tb[IFLA_LINK])
978b863ceb7SPatrick McHardy 		return -EINVAL;
979b863ceb7SPatrick McHardy 
98081adee47SEric W. Biederman 	lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
981b863ceb7SPatrick McHardy 	if (lowerdev == NULL)
982b863ceb7SPatrick McHardy 		return -ENODEV;
983b863ceb7SPatrick McHardy 
984d70f2cf5SKevin Wallace 	/* When creating macvlans or macvtaps on top of other macvlans - use
985b0832a29SEric Biederman 	 * the real device as the lowerdev.
986a6ca5f1dSPatrick McHardy 	 */
987d70f2cf5SKevin Wallace 	if (netif_is_macvlan(lowerdev))
988d70f2cf5SKevin Wallace 		lowerdev = macvlan_dev_real_dev(lowerdev);
989a6ca5f1dSPatrick McHardy 
990b863ceb7SPatrick McHardy 	if (!tb[IFLA_MTU])
991b863ceb7SPatrick McHardy 		dev->mtu = lowerdev->mtu;
992b863ceb7SPatrick McHardy 	else if (dev->mtu > lowerdev->mtu)
993b863ceb7SPatrick McHardy 		return -EINVAL;
994b863ceb7SPatrick McHardy 
995b863ceb7SPatrick McHardy 	if (!tb[IFLA_ADDRESS])
9967ce5d222SDanny Kukawka 		eth_hw_addr_random(dev);
997b863ceb7SPatrick McHardy 
998a35e2c1bSJiri Pirko 	if (!macvlan_port_exists(lowerdev)) {
999b863ceb7SPatrick McHardy 		err = macvlan_port_create(lowerdev);
1000b863ceb7SPatrick McHardy 		if (err < 0)
1001b863ceb7SPatrick McHardy 			return err;
1002b863ceb7SPatrick McHardy 	}
1003e052f7e6SEric Dumazet 	port = macvlan_port_get_rtnl(lowerdev);
1004b863ceb7SPatrick McHardy 
1005eb06acdcSSridhar Samudrala 	/* Only 1 macvlan device can be created in passthru mode */
1006eb06acdcSSridhar Samudrala 	if (port->passthru)
1007eb06acdcSSridhar Samudrala 		return -EINVAL;
1008eb06acdcSSridhar Samudrala 
1009b863ceb7SPatrick McHardy 	vlan->lowerdev = lowerdev;
1010b863ceb7SPatrick McHardy 	vlan->dev      = dev;
1011b863ceb7SPatrick McHardy 	vlan->port     = port;
10122be5c767SVlad Yasevich 	vlan->set_features = MACVLAN_FEATURES;
1013c674ac30SVlad Yasevich 	vlan->nest_level = dev_get_nest_level(lowerdev, netif_is_macvlan) + 1;
1014b863ceb7SPatrick McHardy 
101527c0b1a8SArnd Bergmann 	vlan->mode     = MACVLAN_MODE_VEPA;
101627c0b1a8SArnd Bergmann 	if (data && data[IFLA_MACVLAN_MODE])
101727c0b1a8SArnd Bergmann 		vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
101827c0b1a8SArnd Bergmann 
1019df8ef8f3SJohn Fastabend 	if (data && data[IFLA_MACVLAN_FLAGS])
1020df8ef8f3SJohn Fastabend 		vlan->flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
1021df8ef8f3SJohn Fastabend 
1022eb06acdcSSridhar Samudrala 	if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
1023*5e3c516bSDavid S. Miller 		if (port->count)
1024eb06acdcSSridhar Samudrala 			return -EINVAL;
1025eb06acdcSSridhar Samudrala 		port->passthru = true;
10268b98604eSBjørn Mork 		eth_hw_addr_inherit(dev, lowerdev);
1027eb06acdcSSridhar Samudrala 	}
1028eb06acdcSSridhar Samudrala 
1029*5e3c516bSDavid S. Miller 	port->count += 1;
103047d4ab91SJohn Fastabend 	err = register_netdevice(dev);
103147d4ab91SJohn Fastabend 	if (err < 0)
103247d4ab91SJohn Fastabend 		goto destroy_port;
103347d4ab91SJohn Fastabend 
1034a6cc0cfaSJohn Fastabend 	dev->priv_flags |= IFF_MACVLAN;
10357cd43db7SJiri Pirko 	err = netdev_upper_dev_link(lowerdev, dev);
10367cd43db7SJiri Pirko 	if (err)
1037da37705cSCong Wang 		goto unregister_netdev;
1038b863ceb7SPatrick McHardy 
1039233c7df0SJiri Pirko 	list_add_tail_rcu(&vlan->list, &port->vlans);
1040fc4a7489SPatrick Mullaney 	netif_stacked_transfer_operstate(lowerdev, dev);
1041f16d3d57SJiri Pirko 
1042b863ceb7SPatrick McHardy 	return 0;
1043f16d3d57SJiri Pirko 
1044da37705cSCong Wang unregister_netdev:
1045da37705cSCong Wang 	unregister_netdevice(dev);
1046f16d3d57SJiri Pirko destroy_port:
1047*5e3c516bSDavid S. Miller 	port->count -= 1;
1048*5e3c516bSDavid S. Miller 	if (!port->count)
1049f16d3d57SJiri Pirko 		macvlan_port_destroy(lowerdev);
1050f16d3d57SJiri Pirko 
1051f16d3d57SJiri Pirko 	return err;
1052b863ceb7SPatrick McHardy }
1053fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_common_newlink);
1054b863ceb7SPatrick McHardy 
1055fc0663d6SArnd Bergmann static int macvlan_newlink(struct net *src_net, struct net_device *dev,
1056fc0663d6SArnd Bergmann 			   struct nlattr *tb[], struct nlattr *data[])
1057fc0663d6SArnd Bergmann {
10582f6a1b66SVlad Yasevich 	return macvlan_common_newlink(src_net, dev, tb, data);
1059fc0663d6SArnd Bergmann }
1060fc0663d6SArnd Bergmann 
1061fc0663d6SArnd Bergmann void macvlan_dellink(struct net_device *dev, struct list_head *head)
1062b863ceb7SPatrick McHardy {
1063b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
1064b863ceb7SPatrick McHardy 
1065233c7df0SJiri Pirko 	list_del_rcu(&vlan->list);
106623289a37SEric Dumazet 	unregister_netdevice_queue(dev, head);
10677cd43db7SJiri Pirko 	netdev_upper_dev_unlink(vlan->lowerdev, dev);
1068b863ceb7SPatrick McHardy }
1069fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_dellink);
1070b863ceb7SPatrick McHardy 
107127c0b1a8SArnd Bergmann static int macvlan_changelink(struct net_device *dev,
107227c0b1a8SArnd Bergmann 		struct nlattr *tb[], struct nlattr *data[])
107327c0b1a8SArnd Bergmann {
107427c0b1a8SArnd Bergmann 	struct macvlan_dev *vlan = netdev_priv(dev);
1075266e8347SMichael S. Tsirkin 	enum macvlan_mode mode;
1076266e8347SMichael S. Tsirkin 	bool set_mode = false;
1077266e8347SMichael S. Tsirkin 
1078266e8347SMichael S. Tsirkin 	/* Validate mode, but don't set yet: setting flags may fail. */
1079266e8347SMichael S. Tsirkin 	if (data && data[IFLA_MACVLAN_MODE]) {
1080266e8347SMichael S. Tsirkin 		set_mode = true;
1081266e8347SMichael S. Tsirkin 		mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
1082266e8347SMichael S. Tsirkin 		/* Passthrough mode can't be set or cleared dynamically */
1083266e8347SMichael S. Tsirkin 		if ((mode == MACVLAN_MODE_PASSTHRU) !=
1084266e8347SMichael S. Tsirkin 		    (vlan->mode == MACVLAN_MODE_PASSTHRU))
1085266e8347SMichael S. Tsirkin 			return -EINVAL;
1086266e8347SMichael S. Tsirkin 	}
108799ffc3e7SMichael S. Tsirkin 
1088df8ef8f3SJohn Fastabend 	if (data && data[IFLA_MACVLAN_FLAGS]) {
1089df8ef8f3SJohn Fastabend 		__u16 flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
1090df8ef8f3SJohn Fastabend 		bool promisc = (flags ^ vlan->flags) & MACVLAN_FLAG_NOPROMISC;
109199ffc3e7SMichael S. Tsirkin 		if (vlan->port->passthru && promisc) {
109299ffc3e7SMichael S. Tsirkin 			int err;
1093df8ef8f3SJohn Fastabend 
109499ffc3e7SMichael S. Tsirkin 			if (flags & MACVLAN_FLAG_NOPROMISC)
109599ffc3e7SMichael S. Tsirkin 				err = dev_set_promiscuity(vlan->lowerdev, -1);
109699ffc3e7SMichael S. Tsirkin 			else
109799ffc3e7SMichael S. Tsirkin 				err = dev_set_promiscuity(vlan->lowerdev, 1);
109899ffc3e7SMichael S. Tsirkin 			if (err < 0)
109999ffc3e7SMichael S. Tsirkin 				return err;
110099ffc3e7SMichael S. Tsirkin 		}
1101df8ef8f3SJohn Fastabend 		vlan->flags = flags;
1102df8ef8f3SJohn Fastabend 	}
1103266e8347SMichael S. Tsirkin 	if (set_mode)
1104266e8347SMichael S. Tsirkin 		vlan->mode = mode;
110527c0b1a8SArnd Bergmann 	return 0;
110627c0b1a8SArnd Bergmann }
110727c0b1a8SArnd Bergmann 
110827c0b1a8SArnd Bergmann static size_t macvlan_get_size(const struct net_device *dev)
110927c0b1a8SArnd Bergmann {
111001fe944fSEric Dumazet 	return (0
111101fe944fSEric Dumazet 		+ nla_total_size(4) /* IFLA_MACVLAN_MODE */
111201fe944fSEric Dumazet 		+ nla_total_size(2) /* IFLA_MACVLAN_FLAGS */
111301fe944fSEric Dumazet 		);
111427c0b1a8SArnd Bergmann }
111527c0b1a8SArnd Bergmann 
111627c0b1a8SArnd Bergmann static int macvlan_fill_info(struct sk_buff *skb,
111727c0b1a8SArnd Bergmann 				const struct net_device *dev)
111827c0b1a8SArnd Bergmann {
111927c0b1a8SArnd Bergmann 	struct macvlan_dev *vlan = netdev_priv(dev);
112027c0b1a8SArnd Bergmann 
1121ead9a76cSDavid S. Miller 	if (nla_put_u32(skb, IFLA_MACVLAN_MODE, vlan->mode))
1122ead9a76cSDavid S. Miller 		goto nla_put_failure;
1123df8ef8f3SJohn Fastabend 	if (nla_put_u16(skb, IFLA_MACVLAN_FLAGS, vlan->flags))
1124df8ef8f3SJohn Fastabend 		goto nla_put_failure;
112527c0b1a8SArnd Bergmann 	return 0;
112627c0b1a8SArnd Bergmann 
112727c0b1a8SArnd Bergmann nla_put_failure:
112827c0b1a8SArnd Bergmann 	return -EMSGSIZE;
112927c0b1a8SArnd Bergmann }
113027c0b1a8SArnd Bergmann 
113127c0b1a8SArnd Bergmann static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = {
113227c0b1a8SArnd Bergmann 	[IFLA_MACVLAN_MODE]  = { .type = NLA_U32 },
1133df8ef8f3SJohn Fastabend 	[IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 },
113427c0b1a8SArnd Bergmann };
113527c0b1a8SArnd Bergmann 
1136fc0663d6SArnd Bergmann int macvlan_link_register(struct rtnl_link_ops *ops)
1137fc0663d6SArnd Bergmann {
1138fc0663d6SArnd Bergmann 	/* common fields */
1139fc0663d6SArnd Bergmann 	ops->priv_size		= sizeof(struct macvlan_dev);
1140fc0663d6SArnd Bergmann 	ops->validate		= macvlan_validate;
1141fc0663d6SArnd Bergmann 	ops->maxtype		= IFLA_MACVLAN_MAX;
1142fc0663d6SArnd Bergmann 	ops->policy		= macvlan_policy;
1143fc0663d6SArnd Bergmann 	ops->changelink		= macvlan_changelink;
1144fc0663d6SArnd Bergmann 	ops->get_size		= macvlan_get_size;
1145fc0663d6SArnd Bergmann 	ops->fill_info		= macvlan_fill_info;
1146fc0663d6SArnd Bergmann 
1147fc0663d6SArnd Bergmann 	return rtnl_link_register(ops);
1148fc0663d6SArnd Bergmann };
1149fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_link_register);
1150fc0663d6SArnd Bergmann 
1151fc0663d6SArnd Bergmann static struct rtnl_link_ops macvlan_link_ops = {
1152b863ceb7SPatrick McHardy 	.kind		= "macvlan",
11538a35747aSHerbert Xu 	.setup		= macvlan_setup,
1154b863ceb7SPatrick McHardy 	.newlink	= macvlan_newlink,
1155b863ceb7SPatrick McHardy 	.dellink	= macvlan_dellink,
1156b863ceb7SPatrick McHardy };
1157b863ceb7SPatrick McHardy 
1158b863ceb7SPatrick McHardy static int macvlan_device_event(struct notifier_block *unused,
1159b863ceb7SPatrick McHardy 				unsigned long event, void *ptr)
1160b863ceb7SPatrick McHardy {
1161351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1162b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan, *next;
1163b863ceb7SPatrick McHardy 	struct macvlan_port *port;
1164226bd341SEric Dumazet 	LIST_HEAD(list_kill);
1165b863ceb7SPatrick McHardy 
1166a35e2c1bSJiri Pirko 	if (!macvlan_port_exists(dev))
1167b863ceb7SPatrick McHardy 		return NOTIFY_DONE;
1168b863ceb7SPatrick McHardy 
1169e052f7e6SEric Dumazet 	port = macvlan_port_get_rtnl(dev);
1170a35e2c1bSJiri Pirko 
1171b863ceb7SPatrick McHardy 	switch (event) {
1172b863ceb7SPatrick McHardy 	case NETDEV_CHANGE:
1173b863ceb7SPatrick McHardy 		list_for_each_entry(vlan, &port->vlans, list)
1174fc4a7489SPatrick Mullaney 			netif_stacked_transfer_operstate(vlan->lowerdev,
1175fc4a7489SPatrick Mullaney 							 vlan->dev);
1176b863ceb7SPatrick McHardy 		break;
1177b863ceb7SPatrick McHardy 	case NETDEV_FEAT_CHANGE:
1178b863ceb7SPatrick McHardy 		list_for_each_entry(vlan, &port->vlans, list) {
11798c2acc53SPatrick McHardy 			vlan->dev->gso_max_size = dev->gso_max_size;
1180797f87f8SFlorian Westphal 			netdev_update_features(vlan->dev);
1181b863ceb7SPatrick McHardy 		}
1182b863ceb7SPatrick McHardy 		break;
11833763e7efSdingtianhong 	case NETDEV_CHANGEMTU:
11843763e7efSdingtianhong 		list_for_each_entry(vlan, &port->vlans, list) {
11853763e7efSdingtianhong 			if (vlan->dev->mtu <= dev->mtu)
11863763e7efSdingtianhong 				continue;
11873763e7efSdingtianhong 			dev_set_mtu(vlan->dev, dev->mtu);
11883763e7efSdingtianhong 		}
11893763e7efSdingtianhong 		break;
1190e289fd28Sdingtianhong 	case NETDEV_CHANGEADDR:
1191e289fd28Sdingtianhong 		if (!port->passthru)
1192e289fd28Sdingtianhong 			return NOTIFY_DONE;
1193e289fd28Sdingtianhong 
1194e289fd28Sdingtianhong 		vlan = list_first_entry_or_null(&port->vlans,
1195e289fd28Sdingtianhong 						struct macvlan_dev,
1196e289fd28Sdingtianhong 						list);
1197e289fd28Sdingtianhong 
1198e289fd28Sdingtianhong 		if (macvlan_sync_address(vlan->dev, dev->dev_addr))
1199e289fd28Sdingtianhong 			return NOTIFY_BAD;
1200e289fd28Sdingtianhong 
1201e289fd28Sdingtianhong 		break;
1202b863ceb7SPatrick McHardy 	case NETDEV_UNREGISTER:
12033b27e105SDavid Lamparter 		/* twiddle thumbs on netns device moves */
12043b27e105SDavid Lamparter 		if (dev->reg_state != NETREG_UNREGISTERING)
12053b27e105SDavid Lamparter 			break;
12063b27e105SDavid Lamparter 
1207b863ceb7SPatrick McHardy 		list_for_each_entry_safe(vlan, next, &port->vlans, list)
1208226bd341SEric Dumazet 			vlan->dev->rtnl_link_ops->dellink(vlan->dev, &list_kill);
1209226bd341SEric Dumazet 		unregister_netdevice_many(&list_kill);
1210b863ceb7SPatrick McHardy 		break;
12111c01fe14SJiri Pirko 	case NETDEV_PRE_TYPE_CHANGE:
12121c01fe14SJiri Pirko 		/* Forbid underlaying device to change its type. */
12131c01fe14SJiri Pirko 		return NOTIFY_BAD;
12144c991255SVlad Yasevich 
12154c991255SVlad Yasevich 	case NETDEV_NOTIFY_PEERS:
12164c991255SVlad Yasevich 	case NETDEV_BONDING_FAILOVER:
12174c991255SVlad Yasevich 	case NETDEV_RESEND_IGMP:
12184c991255SVlad Yasevich 		/* Propagate to all vlans */
12194c991255SVlad Yasevich 		list_for_each_entry(vlan, &port->vlans, list)
12204c991255SVlad Yasevich 			call_netdevice_notifiers(event, vlan->dev);
1221b863ceb7SPatrick McHardy 	}
1222b863ceb7SPatrick McHardy 	return NOTIFY_DONE;
1223b863ceb7SPatrick McHardy }
1224b863ceb7SPatrick McHardy 
1225b863ceb7SPatrick McHardy static struct notifier_block macvlan_notifier_block __read_mostly = {
1226b863ceb7SPatrick McHardy 	.notifier_call	= macvlan_device_event,
1227b863ceb7SPatrick McHardy };
1228b863ceb7SPatrick McHardy 
1229b863ceb7SPatrick McHardy static int __init macvlan_init_module(void)
1230b863ceb7SPatrick McHardy {
1231b863ceb7SPatrick McHardy 	int err;
1232b863ceb7SPatrick McHardy 
1233b863ceb7SPatrick McHardy 	register_netdevice_notifier(&macvlan_notifier_block);
1234b863ceb7SPatrick McHardy 
1235fc0663d6SArnd Bergmann 	err = macvlan_link_register(&macvlan_link_ops);
1236b863ceb7SPatrick McHardy 	if (err < 0)
1237b863ceb7SPatrick McHardy 		goto err1;
1238b863ceb7SPatrick McHardy 	return 0;
1239b863ceb7SPatrick McHardy err1:
1240b863ceb7SPatrick McHardy 	unregister_netdevice_notifier(&macvlan_notifier_block);
1241b863ceb7SPatrick McHardy 	return err;
1242b863ceb7SPatrick McHardy }
1243b863ceb7SPatrick McHardy 
1244b863ceb7SPatrick McHardy static void __exit macvlan_cleanup_module(void)
1245b863ceb7SPatrick McHardy {
1246b863ceb7SPatrick McHardy 	rtnl_link_unregister(&macvlan_link_ops);
1247b863ceb7SPatrick McHardy 	unregister_netdevice_notifier(&macvlan_notifier_block);
1248b863ceb7SPatrick McHardy }
1249b863ceb7SPatrick McHardy 
1250b863ceb7SPatrick McHardy module_init(macvlan_init_module);
1251b863ceb7SPatrick McHardy module_exit(macvlan_cleanup_module);
1252b863ceb7SPatrick McHardy 
1253b863ceb7SPatrick McHardy MODULE_LICENSE("GPL");
1254b863ceb7SPatrick McHardy MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
1255b863ceb7SPatrick McHardy MODULE_DESCRIPTION("Driver for MAC address based VLANs");
1256b863ceb7SPatrick McHardy MODULE_ALIAS_RTNL_LINK("macvlan");
1257