xref: /openbmc/linux/drivers/net/macvlan.c (revision 688cea83f4396fa98b77a126ed278b89daccccdc)
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>
36*688cea83Sdingtianhong #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;
48b863ceb7SPatrick McHardy };
49b863ceb7SPatrick McHardy 
50a188a54dSdingtianhong #define MACVLAN_PORT_IS_EMPTY(port)    list_empty(&port->vlans)
51a188a54dSdingtianhong 
52412ca155SHerbert Xu struct macvlan_skb_cb {
53412ca155SHerbert Xu 	const struct macvlan_dev *src;
54412ca155SHerbert Xu };
55412ca155SHerbert Xu 
56412ca155SHerbert Xu #define MACVLAN_SKB_CB(__skb) ((struct macvlan_skb_cb *)&((__skb)->cb[0]))
57412ca155SHerbert Xu 
58d5cd9244SEric W. Biederman static void macvlan_port_destroy(struct net_device *dev);
59d5cd9244SEric W. Biederman 
60e052f7e6SEric Dumazet static struct macvlan_port *macvlan_port_get_rcu(const struct net_device *dev)
61e052f7e6SEric Dumazet {
62e052f7e6SEric Dumazet 	return rcu_dereference(dev->rx_handler_data);
63e052f7e6SEric Dumazet }
64e052f7e6SEric Dumazet 
65e052f7e6SEric Dumazet static struct macvlan_port *macvlan_port_get_rtnl(const struct net_device *dev)
66e052f7e6SEric Dumazet {
67e052f7e6SEric Dumazet 	return rtnl_dereference(dev->rx_handler_data);
68e052f7e6SEric Dumazet }
69e052f7e6SEric Dumazet 
70a35e2c1bSJiri Pirko #define macvlan_port_exists(dev) (dev->priv_flags & IFF_MACVLAN_PORT)
71a35e2c1bSJiri Pirko 
72b863ceb7SPatrick McHardy static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
73b863ceb7SPatrick McHardy 					       const unsigned char *addr)
74b863ceb7SPatrick McHardy {
75b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan;
76b863ceb7SPatrick McHardy 
77b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(vlan, &port->vlan_hash[addr[5]], hlist) {
78a6700db1SJoe Perches 		if (ether_addr_equal_64bits(vlan->dev->dev_addr, addr))
79b863ceb7SPatrick McHardy 			return vlan;
80b863ceb7SPatrick McHardy 	}
81b863ceb7SPatrick McHardy 	return NULL;
82b863ceb7SPatrick McHardy }
83b863ceb7SPatrick McHardy 
84f9ac30f0SEric Biederman static void macvlan_hash_add(struct macvlan_dev *vlan)
85f9ac30f0SEric Biederman {
86f9ac30f0SEric Biederman 	struct macvlan_port *port = vlan->port;
87f9ac30f0SEric Biederman 	const unsigned char *addr = vlan->dev->dev_addr;
88f9ac30f0SEric Biederman 
89f9ac30f0SEric Biederman 	hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]);
90f9ac30f0SEric Biederman }
91f9ac30f0SEric Biederman 
92449f4544SEric Dumazet static void macvlan_hash_del(struct macvlan_dev *vlan, bool sync)
93f9ac30f0SEric Biederman {
94f9ac30f0SEric Biederman 	hlist_del_rcu(&vlan->hlist);
95449f4544SEric Dumazet 	if (sync)
96f9ac30f0SEric Biederman 		synchronize_rcu();
97f9ac30f0SEric Biederman }
98f9ac30f0SEric Biederman 
99f9ac30f0SEric Biederman static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
100f9ac30f0SEric Biederman 					const unsigned char *addr)
101f9ac30f0SEric Biederman {
102449f4544SEric Dumazet 	macvlan_hash_del(vlan, true);
103f9ac30f0SEric Biederman 	/* Now that we are unhashed it is safe to change the device
104f9ac30f0SEric Biederman 	 * address without confusing packet delivery.
105f9ac30f0SEric Biederman 	 */
106f9ac30f0SEric Biederman 	memcpy(vlan->dev->dev_addr, addr, ETH_ALEN);
107f9ac30f0SEric Biederman 	macvlan_hash_add(vlan);
108f9ac30f0SEric Biederman }
109f9ac30f0SEric Biederman 
110f9ac30f0SEric Biederman static int macvlan_addr_busy(const struct macvlan_port *port,
111f9ac30f0SEric Biederman 				const unsigned char *addr)
112f9ac30f0SEric Biederman {
113f9ac30f0SEric Biederman 	/* Test to see if the specified multicast address is
114f9ac30f0SEric Biederman 	 * currently in use by the underlying device or
115f9ac30f0SEric Biederman 	 * another macvlan.
116f9ac30f0SEric Biederman 	 */
117a6700db1SJoe Perches 	if (ether_addr_equal_64bits(port->dev->dev_addr, addr))
118f9ac30f0SEric Biederman 		return 1;
119f9ac30f0SEric Biederman 
120f9ac30f0SEric Biederman 	if (macvlan_hash_lookup(port, addr))
121f9ac30f0SEric Biederman 		return 1;
122f9ac30f0SEric Biederman 
123f9ac30f0SEric Biederman 	return 0;
124f9ac30f0SEric Biederman }
125f9ac30f0SEric Biederman 
126a1e514c5SArnd Bergmann 
127fc0663d6SArnd Bergmann static int macvlan_broadcast_one(struct sk_buff *skb,
128fc0663d6SArnd Bergmann 				 const struct macvlan_dev *vlan,
129618e1b74SArnd Bergmann 				 const struct ethhdr *eth, bool local)
130a1e514c5SArnd Bergmann {
131fc0663d6SArnd Bergmann 	struct net_device *dev = vlan->dev;
132a1e514c5SArnd Bergmann 
133618e1b74SArnd Bergmann 	if (local)
134412ca155SHerbert Xu 		return __dev_forward_skb(dev, skb);
135618e1b74SArnd Bergmann 
136a1e514c5SArnd Bergmann 	skb->dev = dev;
137a6700db1SJoe Perches 	if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast))
138a1e514c5SArnd Bergmann 		skb->pkt_type = PACKET_BROADCAST;
139a1e514c5SArnd Bergmann 	else
140a1e514c5SArnd Bergmann 		skb->pkt_type = PACKET_MULTICAST;
141a1e514c5SArnd Bergmann 
142412ca155SHerbert Xu 	return 0;
143a1e514c5SArnd Bergmann }
144a1e514c5SArnd Bergmann 
1453807ff58SEric Dumazet static u32 macvlan_hash_mix(const struct macvlan_dev *vlan)
1463807ff58SEric Dumazet {
1473807ff58SEric Dumazet 	return (u32)(((unsigned long)vlan) >> L1_CACHE_SHIFT);
1483807ff58SEric Dumazet }
1493807ff58SEric Dumazet 
1503807ff58SEric Dumazet 
1513807ff58SEric Dumazet static unsigned int mc_hash(const struct macvlan_dev *vlan,
1523807ff58SEric Dumazet 			    const unsigned char *addr)
153cd431e73SEric Dumazet {
154cd431e73SEric Dumazet 	u32 val = __get_unaligned_cpu32(addr + 2);
155cd431e73SEric Dumazet 
1563807ff58SEric Dumazet 	val ^= macvlan_hash_mix(vlan);
157cd431e73SEric Dumazet 	return hash_32(val, MACVLAN_MC_FILTER_BITS);
158cd431e73SEric Dumazet }
159cd431e73SEric Dumazet 
160b863ceb7SPatrick McHardy static void macvlan_broadcast(struct sk_buff *skb,
161618e1b74SArnd Bergmann 			      const struct macvlan_port *port,
162618e1b74SArnd Bergmann 			      struct net_device *src,
163618e1b74SArnd Bergmann 			      enum macvlan_mode mode)
164b863ceb7SPatrick McHardy {
165b863ceb7SPatrick McHardy 	const struct ethhdr *eth = eth_hdr(skb);
166b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan;
167b863ceb7SPatrick McHardy 	struct sk_buff *nskb;
168b863ceb7SPatrick McHardy 	unsigned int i;
169a1e514c5SArnd Bergmann 	int err;
1703807ff58SEric Dumazet 	unsigned int hash;
171b863ceb7SPatrick McHardy 
172efbbced3SPatrick McHardy 	if (skb->protocol == htons(ETH_P_PAUSE))
173efbbced3SPatrick McHardy 		return;
174efbbced3SPatrick McHardy 
175b863ceb7SPatrick McHardy 	for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
176b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(vlan, &port->vlan_hash[i], hlist) {
177618e1b74SArnd Bergmann 			if (vlan->dev == src || !(vlan->mode & mode))
178618e1b74SArnd Bergmann 				continue;
179618e1b74SArnd Bergmann 
1803807ff58SEric Dumazet 			hash = mc_hash(vlan, eth->h_dest);
181cd431e73SEric Dumazet 			if (!test_bit(hash, vlan->mc_filter))
182cd431e73SEric Dumazet 				continue;
183de9e8f3fSHerbert Xu 
184de9e8f3fSHerbert Xu 			err = NET_RX_DROP;
185b863ceb7SPatrick McHardy 			nskb = skb_clone(skb, GFP_ATOMIC);
186de9e8f3fSHerbert Xu 			if (likely(nskb))
187de9e8f3fSHerbert Xu 				err = macvlan_broadcast_one(
188de9e8f3fSHerbert Xu 					nskb, vlan, eth,
189412ca155SHerbert Xu 					mode == MACVLAN_MODE_BRIDGE) ?:
190412ca155SHerbert Xu 				      netif_rx_ni(nskb);
191a1e514c5SArnd Bergmann 			macvlan_count_rx(vlan, skb->len + ETH_HLEN,
192a1e514c5SArnd Bergmann 					 err == NET_RX_SUCCESS, 1);
193b863ceb7SPatrick McHardy 		}
194b863ceb7SPatrick McHardy 	}
195b863ceb7SPatrick McHardy }
196b863ceb7SPatrick McHardy 
197412ca155SHerbert Xu static void macvlan_process_broadcast(struct work_struct *w)
198412ca155SHerbert Xu {
199412ca155SHerbert Xu 	struct macvlan_port *port = container_of(w, struct macvlan_port,
200412ca155SHerbert Xu 						 bc_work);
201412ca155SHerbert Xu 	struct sk_buff *skb;
202412ca155SHerbert Xu 	struct sk_buff_head list;
203412ca155SHerbert Xu 
204412ca155SHerbert Xu 	skb_queue_head_init(&list);
205412ca155SHerbert Xu 
206412ca155SHerbert Xu 	spin_lock_bh(&port->bc_queue.lock);
207412ca155SHerbert Xu 	skb_queue_splice_tail_init(&port->bc_queue, &list);
208412ca155SHerbert Xu 	spin_unlock_bh(&port->bc_queue.lock);
209412ca155SHerbert Xu 
210412ca155SHerbert Xu 	while ((skb = __skb_dequeue(&list))) {
211412ca155SHerbert Xu 		const struct macvlan_dev *src = MACVLAN_SKB_CB(skb)->src;
212412ca155SHerbert Xu 
213412ca155SHerbert Xu 		rcu_read_lock();
214412ca155SHerbert Xu 
215412ca155SHerbert Xu 		if (!src)
216412ca155SHerbert Xu 			/* frame comes from an external address */
217412ca155SHerbert Xu 			macvlan_broadcast(skb, port, NULL,
218412ca155SHerbert Xu 					  MACVLAN_MODE_PRIVATE |
219412ca155SHerbert Xu 					  MACVLAN_MODE_VEPA    |
220412ca155SHerbert Xu 					  MACVLAN_MODE_PASSTHRU|
221412ca155SHerbert Xu 					  MACVLAN_MODE_BRIDGE);
222412ca155SHerbert Xu 		else if (src->mode == MACVLAN_MODE_VEPA)
223412ca155SHerbert Xu 			/* flood to everyone except source */
224412ca155SHerbert Xu 			macvlan_broadcast(skb, port, src->dev,
225412ca155SHerbert Xu 					  MACVLAN_MODE_VEPA |
226412ca155SHerbert Xu 					  MACVLAN_MODE_BRIDGE);
227412ca155SHerbert Xu 		else
228412ca155SHerbert Xu 			/*
229412ca155SHerbert Xu 			 * flood only to VEPA ports, bridge ports
230412ca155SHerbert Xu 			 * already saw the frame on the way out.
231412ca155SHerbert Xu 			 */
232412ca155SHerbert Xu 			macvlan_broadcast(skb, port, src->dev,
233412ca155SHerbert Xu 					  MACVLAN_MODE_VEPA);
234412ca155SHerbert Xu 
235412ca155SHerbert Xu 		rcu_read_unlock();
236412ca155SHerbert Xu 
237412ca155SHerbert Xu 		kfree_skb(skb);
238412ca155SHerbert Xu 	}
239412ca155SHerbert Xu }
240412ca155SHerbert Xu 
241412ca155SHerbert Xu static void macvlan_broadcast_enqueue(struct macvlan_port *port,
242412ca155SHerbert Xu 				      struct sk_buff *skb)
243412ca155SHerbert Xu {
244e676f197SHerbert Xu 	struct sk_buff *nskb;
245412ca155SHerbert Xu 	int err = -ENOMEM;
246412ca155SHerbert Xu 
247e676f197SHerbert Xu 	nskb = skb_clone(skb, GFP_ATOMIC);
248e676f197SHerbert Xu 	if (!nskb)
249412ca155SHerbert Xu 		goto err;
250412ca155SHerbert Xu 
251412ca155SHerbert Xu 	spin_lock(&port->bc_queue.lock);
252412ca155SHerbert Xu 	if (skb_queue_len(&port->bc_queue) < skb->dev->tx_queue_len) {
253e676f197SHerbert Xu 		__skb_queue_tail(&port->bc_queue, nskb);
254412ca155SHerbert Xu 		err = 0;
255412ca155SHerbert Xu 	}
256412ca155SHerbert Xu 	spin_unlock(&port->bc_queue.lock);
257412ca155SHerbert Xu 
258412ca155SHerbert Xu 	if (err)
259e676f197SHerbert Xu 		goto free_nskb;
260412ca155SHerbert Xu 
261412ca155SHerbert Xu 	schedule_work(&port->bc_work);
262412ca155SHerbert Xu 	return;
263412ca155SHerbert Xu 
264e676f197SHerbert Xu free_nskb:
265e676f197SHerbert Xu 	kfree_skb(nskb);
266412ca155SHerbert Xu err:
267412ca155SHerbert Xu 	atomic_long_inc(&skb->dev->rx_dropped);
268412ca155SHerbert Xu }
269412ca155SHerbert Xu 
270b863ceb7SPatrick McHardy /* called under rcu_read_lock() from netif_receive_skb */
2718a4eb573SJiri Pirko static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb)
272b863ceb7SPatrick McHardy {
273ab95bfe0SJiri Pirko 	struct macvlan_port *port;
2748a4eb573SJiri Pirko 	struct sk_buff *skb = *pskb;
275b863ceb7SPatrick McHardy 	const struct ethhdr *eth = eth_hdr(skb);
276b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan;
277618e1b74SArnd Bergmann 	const struct macvlan_dev *src;
278b863ceb7SPatrick McHardy 	struct net_device *dev;
279ba01877fSSridhar Samudrala 	unsigned int len = 0;
280ba01877fSSridhar Samudrala 	int ret = NET_RX_DROP;
281b863ceb7SPatrick McHardy 
282a35e2c1bSJiri Pirko 	port = macvlan_port_get_rcu(skb->dev);
283b863ceb7SPatrick McHardy 	if (is_multicast_ether_addr(eth->h_dest)) {
284bc416d97SEric Dumazet 		skb = ip_check_defrag(skb, IP_DEFRAG_MACVLAN);
285bc416d97SEric Dumazet 		if (!skb)
286bc416d97SEric Dumazet 			return RX_HANDLER_CONSUMED;
2874ec7ac12SEric Dumazet 		eth = eth_hdr(skb);
288618e1b74SArnd Bergmann 		src = macvlan_hash_lookup(port, eth->h_source);
289412ca155SHerbert Xu 		if (src && src->mode != MACVLAN_MODE_VEPA &&
290412ca155SHerbert Xu 		    src->mode != MACVLAN_MODE_BRIDGE) {
291729e72a1Sstephen hemminger 			/* forward to original port. */
292729e72a1Sstephen hemminger 			vlan = src;
293412ca155SHerbert Xu 			ret = macvlan_broadcast_one(skb, vlan, eth, 0) ?:
294412ca155SHerbert Xu 			      netif_rx(skb);
295729e72a1Sstephen hemminger 			goto out;
296729e72a1Sstephen hemminger 		}
297729e72a1Sstephen hemminger 
298412ca155SHerbert Xu 		MACVLAN_SKB_CB(skb)->src = src;
299412ca155SHerbert Xu 		macvlan_broadcast_enqueue(port, skb);
300412ca155SHerbert Xu 
3018a4eb573SJiri Pirko 		return RX_HANDLER_PASS;
302b863ceb7SPatrick McHardy 	}
303b863ceb7SPatrick McHardy 
304eb06acdcSSridhar Samudrala 	if (port->passthru)
305233c7df0SJiri Pirko 		vlan = list_first_or_null_rcu(&port->vlans,
306233c7df0SJiri Pirko 					      struct macvlan_dev, list);
307eb06acdcSSridhar Samudrala 	else
308b863ceb7SPatrick McHardy 		vlan = macvlan_hash_lookup(port, eth->h_dest);
309b863ceb7SPatrick McHardy 	if (vlan == NULL)
3108a4eb573SJiri Pirko 		return RX_HANDLER_PASS;
311b863ceb7SPatrick McHardy 
312b863ceb7SPatrick McHardy 	dev = vlan->dev;
313b863ceb7SPatrick McHardy 	if (unlikely(!(dev->flags & IFF_UP))) {
314b863ceb7SPatrick McHardy 		kfree_skb(skb);
3158a4eb573SJiri Pirko 		return RX_HANDLER_CONSUMED;
316b863ceb7SPatrick McHardy 	}
317a1e514c5SArnd Bergmann 	len = skb->len + ETH_HLEN;
318b863ceb7SPatrick McHardy 	skb = skb_share_check(skb, GFP_ATOMIC);
319a1e514c5SArnd Bergmann 	if (!skb)
320ba01877fSSridhar Samudrala 		goto out;
321b863ceb7SPatrick McHardy 
322b863ceb7SPatrick McHardy 	skb->dev = dev;
323b863ceb7SPatrick McHardy 	skb->pkt_type = PACKET_HOST;
324b863ceb7SPatrick McHardy 
3252f6a1b66SVlad Yasevich 	ret = netif_rx(skb);
326ba01877fSSridhar Samudrala 
327ba01877fSSridhar Samudrala out:
328ba01877fSSridhar Samudrala 	macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, 0);
3298a4eb573SJiri Pirko 	return RX_HANDLER_CONSUMED;
330b863ceb7SPatrick McHardy }
331b863ceb7SPatrick McHardy 
332618e1b74SArnd Bergmann static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
333618e1b74SArnd Bergmann {
334618e1b74SArnd Bergmann 	const struct macvlan_dev *vlan = netdev_priv(dev);
335618e1b74SArnd Bergmann 	const struct macvlan_port *port = vlan->port;
336618e1b74SArnd Bergmann 	const struct macvlan_dev *dest;
337618e1b74SArnd Bergmann 
338618e1b74SArnd Bergmann 	if (vlan->mode == MACVLAN_MODE_BRIDGE) {
339618e1b74SArnd Bergmann 		const struct ethhdr *eth = (void *)skb->data;
340618e1b74SArnd Bergmann 
341618e1b74SArnd Bergmann 		/* send to other bridge ports directly */
342618e1b74SArnd Bergmann 		if (is_multicast_ether_addr(eth->h_dest)) {
343618e1b74SArnd Bergmann 			macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE);
344618e1b74SArnd Bergmann 			goto xmit_world;
345618e1b74SArnd Bergmann 		}
346618e1b74SArnd Bergmann 
347618e1b74SArnd Bergmann 		dest = macvlan_hash_lookup(port, eth->h_dest);
348618e1b74SArnd Bergmann 		if (dest && dest->mode == MACVLAN_MODE_BRIDGE) {
349a37dd333SDavid Ward 			/* send to lowerdev first for its network taps */
350cb2d0f3eSDavid Ward 			dev_forward_skb(vlan->lowerdev, skb);
351618e1b74SArnd Bergmann 
352618e1b74SArnd Bergmann 			return NET_XMIT_SUCCESS;
353618e1b74SArnd Bergmann 		}
354618e1b74SArnd Bergmann 	}
355618e1b74SArnd Bergmann 
356618e1b74SArnd Bergmann xmit_world:
35759b9997bSDavid S. Miller 	skb->dev = vlan->lowerdev;
358618e1b74SArnd Bergmann 	return dev_queue_xmit(skb);
359618e1b74SArnd Bergmann }
360618e1b74SArnd Bergmann 
361*688cea83Sdingtianhong static inline netdev_tx_t macvlan_netpoll_send_skb(struct macvlan_dev *vlan, struct sk_buff *skb)
362*688cea83Sdingtianhong {
363*688cea83Sdingtianhong #ifdef CONFIG_NET_POLL_CONTROLLER
364*688cea83Sdingtianhong 	if (vlan->netpoll)
365*688cea83Sdingtianhong 		netpoll_send_skb(vlan->netpoll, skb);
366*688cea83Sdingtianhong #else
367*688cea83Sdingtianhong 	BUG();
368*688cea83Sdingtianhong #endif
369*688cea83Sdingtianhong 	return NETDEV_TX_OK;
370*688cea83Sdingtianhong }
371*688cea83Sdingtianhong 
3720db901bdSstephen hemminger static netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
373424efe9cSStephen Hemminger 				      struct net_device *dev)
374b863ceb7SPatrick McHardy {
375b863ceb7SPatrick McHardy 	unsigned int len = skb->len;
376b863ceb7SPatrick McHardy 	int ret;
377*688cea83Sdingtianhong 	struct macvlan_dev *vlan = netdev_priv(dev);
378*688cea83Sdingtianhong 
379*688cea83Sdingtianhong 	if (unlikely(netpoll_tx_running(dev)))
380*688cea83Sdingtianhong 		return macvlan_netpoll_send_skb(vlan, skb);
381b863ceb7SPatrick McHardy 
382a6cc0cfaSJohn Fastabend 	if (vlan->fwd_priv) {
383a6cc0cfaSJohn Fastabend 		skb->dev = vlan->lowerdev;
384f663dd9aSJason Wang 		ret = dev_queue_xmit_accel(skb, vlan->fwd_priv);
385a6cc0cfaSJohn Fastabend 	} else {
386618e1b74SArnd Bergmann 		ret = macvlan_queue_xmit(skb, dev);
387a6cc0cfaSJohn Fastabend 	}
388a6cc0cfaSJohn Fastabend 
3892d6c9ffcSEric Dumazet 	if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
390cdf3e274SLi RongQing 		struct vlan_pcpu_stats *pcpu_stats;
3912c114553SEric Dumazet 
3928ffab51bSEric Dumazet 		pcpu_stats = this_cpu_ptr(vlan->pcpu_stats);
3938ffab51bSEric Dumazet 		u64_stats_update_begin(&pcpu_stats->syncp);
3948ffab51bSEric Dumazet 		pcpu_stats->tx_packets++;
3958ffab51bSEric Dumazet 		pcpu_stats->tx_bytes += len;
3968ffab51bSEric Dumazet 		u64_stats_update_end(&pcpu_stats->syncp);
3978ffab51bSEric Dumazet 	} else {
3988ffab51bSEric Dumazet 		this_cpu_inc(vlan->pcpu_stats->tx_dropped);
3998ffab51bSEric Dumazet 	}
400cbbef5e1SPatrick McHardy 	return ret;
401b863ceb7SPatrick McHardy }
402b863ceb7SPatrick McHardy 
403b863ceb7SPatrick McHardy static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
4043b04dddeSStephen Hemminger 			       unsigned short type, const void *daddr,
4053b04dddeSStephen Hemminger 			       const void *saddr, unsigned len)
406b863ceb7SPatrick McHardy {
407b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan = netdev_priv(dev);
408b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
409b863ceb7SPatrick McHardy 
4100c4e8581SStephen Hemminger 	return dev_hard_header(skb, lowerdev, type, daddr,
411b863ceb7SPatrick McHardy 			       saddr ? : dev->dev_addr, len);
412b863ceb7SPatrick McHardy }
413b863ceb7SPatrick McHardy 
4143b04dddeSStephen Hemminger static const struct header_ops macvlan_hard_header_ops = {
4153b04dddeSStephen Hemminger 	.create  	= macvlan_hard_header,
4163b04dddeSStephen Hemminger 	.rebuild	= eth_rebuild_header,
4173b04dddeSStephen Hemminger 	.parse		= eth_header_parse,
4183b04dddeSStephen Hemminger 	.cache		= eth_header_cache,
4193b04dddeSStephen Hemminger 	.cache_update	= eth_header_cache_update,
4203b04dddeSStephen Hemminger };
4213b04dddeSStephen Hemminger 
422b13ba1b8SJason Wang static struct rtnl_link_ops macvlan_link_ops;
423b13ba1b8SJason Wang 
424b863ceb7SPatrick McHardy static int macvlan_open(struct net_device *dev)
425b863ceb7SPatrick McHardy {
426b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
427b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
428b863ceb7SPatrick McHardy 	int err;
429b863ceb7SPatrick McHardy 
430eb06acdcSSridhar Samudrala 	if (vlan->port->passthru) {
43178738141SMichael S. Tsirkin 		if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) {
43278738141SMichael S. Tsirkin 			err = dev_set_promiscuity(lowerdev, 1);
43378738141SMichael S. Tsirkin 			if (err < 0)
43478738141SMichael S. Tsirkin 				goto out;
43578738141SMichael S. Tsirkin 		}
436eb06acdcSSridhar Samudrala 		goto hash_add;
437eb06acdcSSridhar Samudrala 	}
438eb06acdcSSridhar Samudrala 
439b13ba1b8SJason Wang 	if (lowerdev->features & NETIF_F_HW_L2FW_DOFFLOAD &&
440b13ba1b8SJason Wang 	    dev->rtnl_link_ops == &macvlan_link_ops) {
441a6cc0cfaSJohn Fastabend 		vlan->fwd_priv =
442a6cc0cfaSJohn Fastabend 		      lowerdev->netdev_ops->ndo_dfwd_add_station(lowerdev, dev);
443a6cc0cfaSJohn Fastabend 
444a6cc0cfaSJohn Fastabend 		/* If we get a NULL pointer back, or if we get an error
445a6cc0cfaSJohn Fastabend 		 * then we should just fall through to the non accelerated path
446a6cc0cfaSJohn Fastabend 		 */
447a6cc0cfaSJohn Fastabend 		if (IS_ERR_OR_NULL(vlan->fwd_priv)) {
448a6cc0cfaSJohn Fastabend 			vlan->fwd_priv = NULL;
449f663dd9aSJason Wang 		} else
450a6cc0cfaSJohn Fastabend 			return 0;
451a6cc0cfaSJohn Fastabend 	}
452a6cc0cfaSJohn Fastabend 
453f9ac30f0SEric Biederman 	err = -EBUSY;
454f9ac30f0SEric Biederman 	if (macvlan_addr_busy(vlan->port, dev->dev_addr))
455f9ac30f0SEric Biederman 		goto out;
456f9ac30f0SEric Biederman 
457a748ee24SJiri Pirko 	err = dev_uc_add(lowerdev, dev->dev_addr);
458b863ceb7SPatrick McHardy 	if (err < 0)
459b89fb7daSWang Chen 		goto out;
460b89fb7daSWang Chen 	if (dev->flags & IFF_ALLMULTI) {
461b89fb7daSWang Chen 		err = dev_set_allmulti(lowerdev, 1);
462b89fb7daSWang Chen 		if (err < 0)
463b89fb7daSWang Chen 			goto del_unicast;
464b89fb7daSWang Chen 	}
465eb06acdcSSridhar Samudrala 
466eb06acdcSSridhar Samudrala hash_add:
467f9ac30f0SEric Biederman 	macvlan_hash_add(vlan);
468b863ceb7SPatrick McHardy 	return 0;
469b89fb7daSWang Chen 
470b89fb7daSWang Chen del_unicast:
471a748ee24SJiri Pirko 	dev_uc_del(lowerdev, dev->dev_addr);
472b89fb7daSWang Chen out:
473a6cc0cfaSJohn Fastabend 	if (vlan->fwd_priv) {
474a6cc0cfaSJohn Fastabend 		lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev,
475a6cc0cfaSJohn Fastabend 							   vlan->fwd_priv);
476a6cc0cfaSJohn Fastabend 		vlan->fwd_priv = NULL;
477a6cc0cfaSJohn Fastabend 	}
478b89fb7daSWang Chen 	return err;
479b863ceb7SPatrick McHardy }
480b863ceb7SPatrick McHardy 
481b863ceb7SPatrick McHardy static int macvlan_stop(struct net_device *dev)
482b863ceb7SPatrick McHardy {
483b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
484b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
485b863ceb7SPatrick McHardy 
486a6cc0cfaSJohn Fastabend 	if (vlan->fwd_priv) {
487a6cc0cfaSJohn Fastabend 		lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev,
488a6cc0cfaSJohn Fastabend 							   vlan->fwd_priv);
489a6cc0cfaSJohn Fastabend 		vlan->fwd_priv = NULL;
490a6cc0cfaSJohn Fastabend 		return 0;
491a6cc0cfaSJohn Fastabend 	}
492a6cc0cfaSJohn Fastabend 
493df8ef8f3SJohn Fastabend 	dev_uc_unsync(lowerdev, dev);
494df8ef8f3SJohn Fastabend 	dev_mc_unsync(lowerdev, dev);
495df8ef8f3SJohn Fastabend 
496eb06acdcSSridhar Samudrala 	if (vlan->port->passthru) {
497df8ef8f3SJohn Fastabend 		if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC))
498eb06acdcSSridhar Samudrala 			dev_set_promiscuity(lowerdev, -1);
499eb06acdcSSridhar Samudrala 		goto hash_del;
500eb06acdcSSridhar Samudrala 	}
501eb06acdcSSridhar Samudrala 
502b863ceb7SPatrick McHardy 	if (dev->flags & IFF_ALLMULTI)
503b863ceb7SPatrick McHardy 		dev_set_allmulti(lowerdev, -1);
504b863ceb7SPatrick McHardy 
505a748ee24SJiri Pirko 	dev_uc_del(lowerdev, dev->dev_addr);
506b863ceb7SPatrick McHardy 
507eb06acdcSSridhar Samudrala hash_del:
508449f4544SEric Dumazet 	macvlan_hash_del(vlan, !dev->dismantle);
509b863ceb7SPatrick McHardy 	return 0;
510b863ceb7SPatrick McHardy }
511b863ceb7SPatrick McHardy 
512e289fd28Sdingtianhong static int macvlan_sync_address(struct net_device *dev, unsigned char *addr)
513ad5d20a6SPatrick McHardy {
514ad5d20a6SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
515ad5d20a6SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
516ad5d20a6SPatrick McHardy 	int err;
517ad5d20a6SPatrick McHardy 
518f9ac30f0SEric Biederman 	if (!(dev->flags & IFF_UP)) {
519f9ac30f0SEric Biederman 		/* Just copy in the new address */
520e289fd28Sdingtianhong 		ether_addr_copy(dev->dev_addr, addr);
521f9ac30f0SEric Biederman 	} else {
522f9ac30f0SEric Biederman 		/* Rehash and update the device filters */
523e289fd28Sdingtianhong 		if (macvlan_addr_busy(vlan->port, addr))
524f9ac30f0SEric Biederman 			return -EBUSY;
525ad5d20a6SPatrick McHardy 
526e289fd28Sdingtianhong 		if (!vlan->port->passthru) {
527e289fd28Sdingtianhong 			err = dev_uc_add(lowerdev, addr);
528ccffad25SJiri Pirko 			if (err)
529ad5d20a6SPatrick McHardy 				return err;
530f9ac30f0SEric Biederman 
531a748ee24SJiri Pirko 			dev_uc_del(lowerdev, dev->dev_addr);
532e289fd28Sdingtianhong 		}
533ad5d20a6SPatrick McHardy 
534e289fd28Sdingtianhong 		macvlan_hash_change_addr(vlan, addr);
535f9ac30f0SEric Biederman 	}
536ad5d20a6SPatrick McHardy 	return 0;
537ad5d20a6SPatrick McHardy }
538ad5d20a6SPatrick McHardy 
539e289fd28Sdingtianhong static int macvlan_set_mac_address(struct net_device *dev, void *p)
540e289fd28Sdingtianhong {
541e289fd28Sdingtianhong 	struct macvlan_dev *vlan = netdev_priv(dev);
542e289fd28Sdingtianhong 	struct sockaddr *addr = p;
543e289fd28Sdingtianhong 
544e289fd28Sdingtianhong 	if (!is_valid_ether_addr(addr->sa_data))
545e289fd28Sdingtianhong 		return -EADDRNOTAVAIL;
546e289fd28Sdingtianhong 
547e289fd28Sdingtianhong 	if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
548e289fd28Sdingtianhong 		dev_set_mac_address(vlan->lowerdev, addr);
549e289fd28Sdingtianhong 		return 0;
550e289fd28Sdingtianhong 	}
551e289fd28Sdingtianhong 
552e289fd28Sdingtianhong 	return macvlan_sync_address(dev, addr->sa_data);
553e289fd28Sdingtianhong }
554e289fd28Sdingtianhong 
555b863ceb7SPatrick McHardy static void macvlan_change_rx_flags(struct net_device *dev, int change)
556b863ceb7SPatrick McHardy {
557b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
558b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
559b863ceb7SPatrick McHardy 
560bbeb0eadSPeter Christensen 	if (dev->flags & IFF_UP) {
561b863ceb7SPatrick McHardy 		if (change & IFF_ALLMULTI)
562b863ceb7SPatrick McHardy 			dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
563b863ceb7SPatrick McHardy 	}
564bbeb0eadSPeter Christensen }
565b863ceb7SPatrick McHardy 
566df8ef8f3SJohn Fastabend static void macvlan_set_mac_lists(struct net_device *dev)
567b863ceb7SPatrick McHardy {
568b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
569b863ceb7SPatrick McHardy 
570cd431e73SEric Dumazet 	if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
571cd431e73SEric Dumazet 		bitmap_fill(vlan->mc_filter, MACVLAN_MC_FILTER_SZ);
572cd431e73SEric Dumazet 	} else {
573cd431e73SEric Dumazet 		struct netdev_hw_addr *ha;
574cd431e73SEric Dumazet 		DECLARE_BITMAP(filter, MACVLAN_MC_FILTER_SZ);
575cd431e73SEric Dumazet 
576cd431e73SEric Dumazet 		bitmap_zero(filter, MACVLAN_MC_FILTER_SZ);
577cd431e73SEric Dumazet 		netdev_for_each_mc_addr(ha, dev) {
5783807ff58SEric Dumazet 			__set_bit(mc_hash(vlan, ha->addr), filter);
579cd431e73SEric Dumazet 		}
580d5270430SEric Dumazet 
5813807ff58SEric Dumazet 		__set_bit(mc_hash(vlan, dev->broadcast), filter);
582d5270430SEric Dumazet 
583cd431e73SEric Dumazet 		bitmap_copy(vlan->mc_filter, filter, MACVLAN_MC_FILTER_SZ);
584cd431e73SEric Dumazet 	}
585df8ef8f3SJohn Fastabend 	dev_uc_sync(vlan->lowerdev, dev);
586b863ceb7SPatrick McHardy 	dev_mc_sync(vlan->lowerdev, dev);
587b863ceb7SPatrick McHardy }
588b863ceb7SPatrick McHardy 
589b863ceb7SPatrick McHardy static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
590b863ceb7SPatrick McHardy {
591b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
592b863ceb7SPatrick McHardy 
593b863ceb7SPatrick McHardy 	if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
594b863ceb7SPatrick McHardy 		return -EINVAL;
595b863ceb7SPatrick McHardy 	dev->mtu = new_mtu;
596b863ceb7SPatrick McHardy 	return 0;
597b863ceb7SPatrick McHardy }
598b863ceb7SPatrick McHardy 
599b863ceb7SPatrick McHardy /*
600b863ceb7SPatrick McHardy  * macvlan network devices have devices nesting below it and are a special
601b863ceb7SPatrick McHardy  * "super class" of normal network devices; split their locks off into a
602b863ceb7SPatrick McHardy  * separate class since they always nest.
603b863ceb7SPatrick McHardy  */
604b863ceb7SPatrick McHardy static struct lock_class_key macvlan_netdev_xmit_lock_key;
605cf508b12SDavid S. Miller static struct lock_class_key macvlan_netdev_addr_lock_key;
606b863ceb7SPatrick McHardy 
6078b4703e9SVlad Yasevich #define ALWAYS_ON_FEATURES \
6088b4703e9SVlad Yasevich 	(NETIF_F_SG | NETIF_F_GEN_CSUM | NETIF_F_GSO_SOFTWARE | NETIF_F_LLTX)
6098b4703e9SVlad Yasevich 
610b863ceb7SPatrick McHardy #define MACVLAN_FEATURES \
611b863ceb7SPatrick McHardy 	(NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
612b863ceb7SPatrick McHardy 	 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
6138d13e670SJohn Fastabend 	 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
61428d2b136SPatrick McHardy 	 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
615b863ceb7SPatrick McHardy 
616b863ceb7SPatrick McHardy #define MACVLAN_STATE_MASK \
617b863ceb7SPatrick McHardy 	((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
618b863ceb7SPatrick McHardy 
619c674ac30SVlad Yasevich static int macvlan_get_nest_level(struct net_device *dev)
620c674ac30SVlad Yasevich {
621c674ac30SVlad Yasevich 	return ((struct macvlan_dev *)netdev_priv(dev))->nest_level;
622c674ac30SVlad Yasevich }
623c674ac30SVlad Yasevich 
624e8a0464cSDavid S. Miller static void macvlan_set_lockdep_class_one(struct net_device *dev,
625e8a0464cSDavid S. Miller 					  struct netdev_queue *txq,
626e8a0464cSDavid S. Miller 					  void *_unused)
627c773e847SDavid S. Miller {
628c773e847SDavid S. Miller 	lockdep_set_class(&txq->_xmit_lock,
629c773e847SDavid S. Miller 			  &macvlan_netdev_xmit_lock_key);
630c773e847SDavid S. Miller }
631c773e847SDavid S. Miller 
632c773e847SDavid S. Miller static void macvlan_set_lockdep_class(struct net_device *dev)
633c773e847SDavid S. Miller {
634c674ac30SVlad Yasevich 	lockdep_set_class_and_subclass(&dev->addr_list_lock,
635c674ac30SVlad Yasevich 				       &macvlan_netdev_addr_lock_key,
636c674ac30SVlad Yasevich 				       macvlan_get_nest_level(dev));
637e8a0464cSDavid S. Miller 	netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL);
638c773e847SDavid S. Miller }
639c773e847SDavid S. Miller 
640b863ceb7SPatrick McHardy static int macvlan_init(struct net_device *dev)
641b863ceb7SPatrick McHardy {
642b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
643b863ceb7SPatrick McHardy 	const struct net_device *lowerdev = vlan->lowerdev;
644b863ceb7SPatrick McHardy 
645b863ceb7SPatrick McHardy 	dev->state		= (dev->state & ~MACVLAN_STATE_MASK) |
646b863ceb7SPatrick McHardy 				  (lowerdev->state & MACVLAN_STATE_MASK);
647b863ceb7SPatrick McHardy 	dev->features 		= lowerdev->features & MACVLAN_FEATURES;
6488b4703e9SVlad Yasevich 	dev->features		|= ALWAYS_ON_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 
669a188a54dSdingtianhong 	if (MACVLAN_PORT_IS_EMPTY(port))
670d5cd9244SEric W. Biederman 		macvlan_port_destroy(port->dev);
671fccaf710SEric Dumazet }
672fccaf710SEric Dumazet 
67328172739SEric Dumazet static struct rtnl_link_stats64 *macvlan_dev_get_stats64(struct net_device *dev,
67428172739SEric Dumazet 							 struct rtnl_link_stats64 *stats)
675fccaf710SEric Dumazet {
676fccaf710SEric Dumazet 	struct macvlan_dev *vlan = netdev_priv(dev);
677fccaf710SEric Dumazet 
6788ffab51bSEric Dumazet 	if (vlan->pcpu_stats) {
679cdf3e274SLi RongQing 		struct vlan_pcpu_stats *p;
6808ffab51bSEric Dumazet 		u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
6818ffab51bSEric Dumazet 		u32 rx_errors = 0, tx_dropped = 0;
682bc66154eSEric Dumazet 		unsigned int start;
683fccaf710SEric Dumazet 		int i;
684fccaf710SEric Dumazet 
685fccaf710SEric Dumazet 		for_each_possible_cpu(i) {
6868ffab51bSEric Dumazet 			p = per_cpu_ptr(vlan->pcpu_stats, i);
687bc66154eSEric Dumazet 			do {
68857a7744eSEric W. Biederman 				start = u64_stats_fetch_begin_irq(&p->syncp);
689bc66154eSEric Dumazet 				rx_packets	= p->rx_packets;
690bc66154eSEric Dumazet 				rx_bytes	= p->rx_bytes;
691bc66154eSEric Dumazet 				rx_multicast	= p->rx_multicast;
6928ffab51bSEric Dumazet 				tx_packets	= p->tx_packets;
6938ffab51bSEric Dumazet 				tx_bytes	= p->tx_bytes;
69457a7744eSEric W. Biederman 			} while (u64_stats_fetch_retry_irq(&p->syncp, start));
6958ffab51bSEric Dumazet 
6968ffab51bSEric Dumazet 			stats->rx_packets	+= rx_packets;
6978ffab51bSEric Dumazet 			stats->rx_bytes		+= rx_bytes;
6988ffab51bSEric Dumazet 			stats->multicast	+= rx_multicast;
6998ffab51bSEric Dumazet 			stats->tx_packets	+= tx_packets;
7008ffab51bSEric Dumazet 			stats->tx_bytes		+= tx_bytes;
7018ffab51bSEric Dumazet 			/* rx_errors & tx_dropped are u32, updated
7028ffab51bSEric Dumazet 			 * without syncp protection.
7038ffab51bSEric Dumazet 			 */
7048ffab51bSEric Dumazet 			rx_errors	+= p->rx_errors;
7058ffab51bSEric Dumazet 			tx_dropped	+= p->tx_dropped;
706fccaf710SEric Dumazet 		}
7078ffab51bSEric Dumazet 		stats->rx_errors	= rx_errors;
7088ffab51bSEric Dumazet 		stats->rx_dropped	= rx_errors;
7098ffab51bSEric Dumazet 		stats->tx_dropped	= tx_dropped;
710fccaf710SEric Dumazet 	}
711fccaf710SEric Dumazet 	return stats;
712fccaf710SEric Dumazet }
713fccaf710SEric Dumazet 
7148e586137SJiri Pirko static int macvlan_vlan_rx_add_vid(struct net_device *dev,
71580d5c368SPatrick McHardy 				   __be16 proto, u16 vid)
7168d13e670SJohn Fastabend {
7178d13e670SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
7188d13e670SJohn Fastabend 	struct net_device *lowerdev = vlan->lowerdev;
7198d13e670SJohn Fastabend 
72080d5c368SPatrick McHardy 	return vlan_vid_add(lowerdev, proto, vid);
7218d13e670SJohn Fastabend }
7228d13e670SJohn Fastabend 
7238e586137SJiri Pirko static int macvlan_vlan_rx_kill_vid(struct net_device *dev,
72480d5c368SPatrick McHardy 				    __be16 proto, u16 vid)
7258d13e670SJohn Fastabend {
7268d13e670SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
7278d13e670SJohn Fastabend 	struct net_device *lowerdev = vlan->lowerdev;
7288d13e670SJohn Fastabend 
72980d5c368SPatrick McHardy 	vlan_vid_del(lowerdev, proto, vid);
7308e586137SJiri Pirko 	return 0;
7318d13e670SJohn Fastabend }
7328d13e670SJohn Fastabend 
733edc7d573Sstephen hemminger static int macvlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
734df8ef8f3SJohn Fastabend 			   struct net_device *dev,
7356b6e2725Sstephen hemminger 			   const unsigned char *addr,
736df8ef8f3SJohn Fastabend 			   u16 flags)
737df8ef8f3SJohn Fastabend {
738df8ef8f3SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
739df8ef8f3SJohn Fastabend 	int err = -EINVAL;
740df8ef8f3SJohn Fastabend 
741df8ef8f3SJohn Fastabend 	if (!vlan->port->passthru)
742df8ef8f3SJohn Fastabend 		return -EOPNOTSUPP;
743df8ef8f3SJohn Fastabend 
744ab2cfbb2SThomas Richter 	if (flags & NLM_F_REPLACE)
745ab2cfbb2SThomas Richter 		return -EOPNOTSUPP;
746ab2cfbb2SThomas Richter 
747df8ef8f3SJohn Fastabend 	if (is_unicast_ether_addr(addr))
748df8ef8f3SJohn Fastabend 		err = dev_uc_add_excl(dev, addr);
749df8ef8f3SJohn Fastabend 	else if (is_multicast_ether_addr(addr))
750df8ef8f3SJohn Fastabend 		err = dev_mc_add_excl(dev, addr);
751df8ef8f3SJohn Fastabend 
752df8ef8f3SJohn Fastabend 	return err;
753df8ef8f3SJohn Fastabend }
754df8ef8f3SJohn Fastabend 
7551690be63SVlad Yasevich static int macvlan_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
756df8ef8f3SJohn Fastabend 			   struct net_device *dev,
7576b6e2725Sstephen hemminger 			   const unsigned char *addr)
758df8ef8f3SJohn Fastabend {
759df8ef8f3SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
760df8ef8f3SJohn Fastabend 	int err = -EINVAL;
761df8ef8f3SJohn Fastabend 
762df8ef8f3SJohn Fastabend 	if (!vlan->port->passthru)
763df8ef8f3SJohn Fastabend 		return -EOPNOTSUPP;
764df8ef8f3SJohn Fastabend 
765df8ef8f3SJohn Fastabend 	if (is_unicast_ether_addr(addr))
766df8ef8f3SJohn Fastabend 		err = dev_uc_del(dev, addr);
767df8ef8f3SJohn Fastabend 	else if (is_multicast_ether_addr(addr))
768df8ef8f3SJohn Fastabend 		err = dev_mc_del(dev, addr);
769df8ef8f3SJohn Fastabend 
770df8ef8f3SJohn Fastabend 	return err;
771df8ef8f3SJohn Fastabend }
772df8ef8f3SJohn Fastabend 
773b863ceb7SPatrick McHardy static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
774b863ceb7SPatrick McHardy 					struct ethtool_drvinfo *drvinfo)
775b863ceb7SPatrick McHardy {
7767826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "macvlan", sizeof(drvinfo->driver));
7777826d43fSJiri Pirko 	strlcpy(drvinfo->version, "0.1", sizeof(drvinfo->version));
778b863ceb7SPatrick McHardy }
779b863ceb7SPatrick McHardy 
7809edb8bb6SStephen Hemminger static int macvlan_ethtool_get_settings(struct net_device *dev,
7819edb8bb6SStephen Hemminger 					struct ethtool_cmd *cmd)
7829edb8bb6SStephen Hemminger {
7839edb8bb6SStephen Hemminger 	const struct macvlan_dev *vlan = netdev_priv(dev);
7844bc71cb9SJiri Pirko 
7854bc71cb9SJiri Pirko 	return __ethtool_get_settings(vlan->lowerdev, cmd);
7869edb8bb6SStephen Hemminger }
7879edb8bb6SStephen Hemminger 
7882be5c767SVlad Yasevich static netdev_features_t macvlan_fix_features(struct net_device *dev,
7892be5c767SVlad Yasevich 					      netdev_features_t features)
7902be5c767SVlad Yasevich {
7912be5c767SVlad Yasevich 	struct macvlan_dev *vlan = netdev_priv(dev);
792797f87f8SFlorian Westphal 	netdev_features_t mask;
7932be5c767SVlad Yasevich 
794797f87f8SFlorian Westphal 	features |= NETIF_F_ALL_FOR_ALL;
795797f87f8SFlorian Westphal 	features &= (vlan->set_features | ~MACVLAN_FEATURES);
796797f87f8SFlorian Westphal 	mask = features;
797797f87f8SFlorian Westphal 
798797f87f8SFlorian Westphal 	features = netdev_increment_features(vlan->lowerdev->features,
799797f87f8SFlorian Westphal 					     features,
800797f87f8SFlorian Westphal 					     mask);
8018b4703e9SVlad Yasevich 	features |= ALWAYS_ON_FEATURES;
802797f87f8SFlorian Westphal 
803797f87f8SFlorian Westphal 	return features;
8042be5c767SVlad Yasevich }
8052be5c767SVlad Yasevich 
806*688cea83Sdingtianhong #ifdef CONFIG_NET_POLL_CONTROLLER
807*688cea83Sdingtianhong static void macvlan_dev_poll_controller(struct net_device *dev)
808*688cea83Sdingtianhong {
809*688cea83Sdingtianhong 	return;
810*688cea83Sdingtianhong }
811*688cea83Sdingtianhong 
812*688cea83Sdingtianhong static int macvlan_dev_netpoll_setup(struct net_device *dev, struct netpoll_info *npinfo)
813*688cea83Sdingtianhong {
814*688cea83Sdingtianhong 	struct macvlan_dev *vlan = netdev_priv(dev);
815*688cea83Sdingtianhong 	struct net_device *real_dev = vlan->lowerdev;
816*688cea83Sdingtianhong 	struct netpoll *netpoll;
817*688cea83Sdingtianhong 	int err = 0;
818*688cea83Sdingtianhong 
819*688cea83Sdingtianhong 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
820*688cea83Sdingtianhong 	err = -ENOMEM;
821*688cea83Sdingtianhong 	if (!netpoll)
822*688cea83Sdingtianhong 		goto out;
823*688cea83Sdingtianhong 
824*688cea83Sdingtianhong 	err = __netpoll_setup(netpoll, real_dev);
825*688cea83Sdingtianhong 	if (err) {
826*688cea83Sdingtianhong 		kfree(netpoll);
827*688cea83Sdingtianhong 		goto out;
828*688cea83Sdingtianhong 	}
829*688cea83Sdingtianhong 
830*688cea83Sdingtianhong 	vlan->netpoll = netpoll;
831*688cea83Sdingtianhong 
832*688cea83Sdingtianhong out:
833*688cea83Sdingtianhong 	return err;
834*688cea83Sdingtianhong }
835*688cea83Sdingtianhong 
836*688cea83Sdingtianhong static void macvlan_dev_netpoll_cleanup(struct net_device *dev)
837*688cea83Sdingtianhong {
838*688cea83Sdingtianhong 	struct macvlan_dev *vlan = netdev_priv(dev);
839*688cea83Sdingtianhong 	struct netpoll *netpoll = vlan->netpoll;
840*688cea83Sdingtianhong 
841*688cea83Sdingtianhong 	if (!netpoll)
842*688cea83Sdingtianhong 		return;
843*688cea83Sdingtianhong 
844*688cea83Sdingtianhong 	vlan->netpoll = NULL;
845*688cea83Sdingtianhong 
846*688cea83Sdingtianhong 	__netpoll_free_async(netpoll);
847*688cea83Sdingtianhong }
848*688cea83Sdingtianhong #endif	/* CONFIG_NET_POLL_CONTROLLER */
849*688cea83Sdingtianhong 
850b863ceb7SPatrick McHardy static const struct ethtool_ops macvlan_ethtool_ops = {
851b863ceb7SPatrick McHardy 	.get_link		= ethtool_op_get_link,
8529edb8bb6SStephen Hemminger 	.get_settings		= macvlan_ethtool_get_settings,
853b863ceb7SPatrick McHardy 	.get_drvinfo		= macvlan_ethtool_get_drvinfo,
854b863ceb7SPatrick McHardy };
855b863ceb7SPatrick McHardy 
85654a30c97SStephen Hemminger static const struct net_device_ops macvlan_netdev_ops = {
85754a30c97SStephen Hemminger 	.ndo_init		= macvlan_init,
858fccaf710SEric Dumazet 	.ndo_uninit		= macvlan_uninit,
85954a30c97SStephen Hemminger 	.ndo_open		= macvlan_open,
86054a30c97SStephen Hemminger 	.ndo_stop		= macvlan_stop,
86100829823SStephen Hemminger 	.ndo_start_xmit		= macvlan_start_xmit,
86254a30c97SStephen Hemminger 	.ndo_change_mtu		= macvlan_change_mtu,
8632be5c767SVlad Yasevich 	.ndo_fix_features	= macvlan_fix_features,
86454a30c97SStephen Hemminger 	.ndo_change_rx_flags	= macvlan_change_rx_flags,
86554a30c97SStephen Hemminger 	.ndo_set_mac_address	= macvlan_set_mac_address,
866df8ef8f3SJohn Fastabend 	.ndo_set_rx_mode	= macvlan_set_mac_lists,
867bc66154eSEric Dumazet 	.ndo_get_stats64	= macvlan_dev_get_stats64,
86854a30c97SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
8698d13e670SJohn Fastabend 	.ndo_vlan_rx_add_vid	= macvlan_vlan_rx_add_vid,
8708d13e670SJohn Fastabend 	.ndo_vlan_rx_kill_vid	= macvlan_vlan_rx_kill_vid,
871df8ef8f3SJohn Fastabend 	.ndo_fdb_add		= macvlan_fdb_add,
872df8ef8f3SJohn Fastabend 	.ndo_fdb_del		= macvlan_fdb_del,
873df8ef8f3SJohn Fastabend 	.ndo_fdb_dump		= ndo_dflt_fdb_dump,
874c674ac30SVlad Yasevich 	.ndo_get_lock_subclass  = macvlan_get_nest_level,
875*688cea83Sdingtianhong #ifdef CONFIG_NET_POLL_CONTROLLER
876*688cea83Sdingtianhong 	.ndo_poll_controller	= macvlan_dev_poll_controller,
877*688cea83Sdingtianhong 	.ndo_netpoll_setup	= macvlan_dev_netpoll_setup,
878*688cea83Sdingtianhong 	.ndo_netpoll_cleanup	= macvlan_dev_netpoll_cleanup,
879*688cea83Sdingtianhong #endif
88054a30c97SStephen Hemminger };
88154a30c97SStephen Hemminger 
8828a35747aSHerbert Xu void macvlan_common_setup(struct net_device *dev)
883b863ceb7SPatrick McHardy {
884b863ceb7SPatrick McHardy 	ether_setup(dev);
885b863ceb7SPatrick McHardy 
886550fd08cSNeil Horman 	dev->priv_flags	       &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
88787ab7f6fSVlad Yasevich 	dev->priv_flags	       |= IFF_UNICAST_FLT;
88854a30c97SStephen Hemminger 	dev->netdev_ops		= &macvlan_netdev_ops;
889b863ceb7SPatrick McHardy 	dev->destructor		= free_netdev;
89071740129SLutz Jaenicke 	dev->header_ops		= &macvlan_hard_header_ops;
891b863ceb7SPatrick McHardy 	dev->ethtool_ops	= &macvlan_ethtool_ops;
8928a35747aSHerbert Xu }
8938a35747aSHerbert Xu EXPORT_SYMBOL_GPL(macvlan_common_setup);
8948a35747aSHerbert Xu 
8958a35747aSHerbert Xu static void macvlan_setup(struct net_device *dev)
8968a35747aSHerbert Xu {
8978a35747aSHerbert Xu 	macvlan_common_setup(dev);
898b863ceb7SPatrick McHardy 	dev->tx_queue_len	= 0;
899b863ceb7SPatrick McHardy }
900b863ceb7SPatrick McHardy 
901b863ceb7SPatrick McHardy static int macvlan_port_create(struct net_device *dev)
902b863ceb7SPatrick McHardy {
903b863ceb7SPatrick McHardy 	struct macvlan_port *port;
904b863ceb7SPatrick McHardy 	unsigned int i;
905ab95bfe0SJiri Pirko 	int err;
906b863ceb7SPatrick McHardy 
907b863ceb7SPatrick McHardy 	if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
908b863ceb7SPatrick McHardy 		return -EINVAL;
909b863ceb7SPatrick McHardy 
910b863ceb7SPatrick McHardy 	port = kzalloc(sizeof(*port), GFP_KERNEL);
911b863ceb7SPatrick McHardy 	if (port == NULL)
912b863ceb7SPatrick McHardy 		return -ENOMEM;
913b863ceb7SPatrick McHardy 
914eb06acdcSSridhar Samudrala 	port->passthru = false;
915b863ceb7SPatrick McHardy 	port->dev = dev;
916b863ceb7SPatrick McHardy 	INIT_LIST_HEAD(&port->vlans);
917b863ceb7SPatrick McHardy 	for (i = 0; i < MACVLAN_HASH_SIZE; i++)
918b863ceb7SPatrick McHardy 		INIT_HLIST_HEAD(&port->vlan_hash[i]);
919ab95bfe0SJiri Pirko 
920412ca155SHerbert Xu 	skb_queue_head_init(&port->bc_queue);
921412ca155SHerbert Xu 	INIT_WORK(&port->bc_work, macvlan_process_broadcast);
922412ca155SHerbert Xu 
923a35e2c1bSJiri Pirko 	err = netdev_rx_handler_register(dev, macvlan_handle_frame, port);
924a35e2c1bSJiri Pirko 	if (err)
925ab95bfe0SJiri Pirko 		kfree(port);
926d9351561SEric Dumazet 	else
927a35e2c1bSJiri Pirko 		dev->priv_flags |= IFF_MACVLAN_PORT;
928ab95bfe0SJiri Pirko 	return err;
929b863ceb7SPatrick McHardy }
930b863ceb7SPatrick McHardy 
931b863ceb7SPatrick McHardy static void macvlan_port_destroy(struct net_device *dev)
932b863ceb7SPatrick McHardy {
933e052f7e6SEric Dumazet 	struct macvlan_port *port = macvlan_port_get_rtnl(dev);
934b863ceb7SPatrick McHardy 
935412ca155SHerbert Xu 	cancel_work_sync(&port->bc_work);
936a35e2c1bSJiri Pirko 	dev->priv_flags &= ~IFF_MACVLAN_PORT;
937ab95bfe0SJiri Pirko 	netdev_rx_handler_unregister(dev);
9386e070aecSLai Jiangshan 	kfree_rcu(port, rcu);
939b863ceb7SPatrick McHardy }
940b863ceb7SPatrick McHardy 
941b863ceb7SPatrick McHardy static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
942b863ceb7SPatrick McHardy {
943b863ceb7SPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
944b863ceb7SPatrick McHardy 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
945b863ceb7SPatrick McHardy 			return -EINVAL;
946b863ceb7SPatrick McHardy 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
947b863ceb7SPatrick McHardy 			return -EADDRNOTAVAIL;
948b863ceb7SPatrick McHardy 	}
94927c0b1a8SArnd Bergmann 
95015127478SMichael S. Tsirkin 	if (data && data[IFLA_MACVLAN_FLAGS] &&
95115127478SMichael S. Tsirkin 	    nla_get_u16(data[IFLA_MACVLAN_FLAGS]) & ~MACVLAN_FLAG_NOPROMISC)
95215127478SMichael S. Tsirkin 		return -EINVAL;
95315127478SMichael S. Tsirkin 
95427c0b1a8SArnd Bergmann 	if (data && data[IFLA_MACVLAN_MODE]) {
95527c0b1a8SArnd Bergmann 		switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) {
95627c0b1a8SArnd Bergmann 		case MACVLAN_MODE_PRIVATE:
95727c0b1a8SArnd Bergmann 		case MACVLAN_MODE_VEPA:
95827c0b1a8SArnd Bergmann 		case MACVLAN_MODE_BRIDGE:
959eb06acdcSSridhar Samudrala 		case MACVLAN_MODE_PASSTHRU:
96027c0b1a8SArnd Bergmann 			break;
96127c0b1a8SArnd Bergmann 		default:
96227c0b1a8SArnd Bergmann 			return -EINVAL;
96327c0b1a8SArnd Bergmann 		}
96427c0b1a8SArnd Bergmann 	}
965b863ceb7SPatrick McHardy 	return 0;
966b863ceb7SPatrick McHardy }
967b863ceb7SPatrick McHardy 
968fc0663d6SArnd Bergmann int macvlan_common_newlink(struct net *src_net, struct net_device *dev,
9692f6a1b66SVlad Yasevich 			   struct nlattr *tb[], struct nlattr *data[])
970b863ceb7SPatrick McHardy {
971b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
972b863ceb7SPatrick McHardy 	struct macvlan_port *port;
973b863ceb7SPatrick McHardy 	struct net_device *lowerdev;
974b863ceb7SPatrick McHardy 	int err;
975b863ceb7SPatrick McHardy 
976b863ceb7SPatrick McHardy 	if (!tb[IFLA_LINK])
977b863ceb7SPatrick McHardy 		return -EINVAL;
978b863ceb7SPatrick McHardy 
97981adee47SEric W. Biederman 	lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
980b863ceb7SPatrick McHardy 	if (lowerdev == NULL)
981b863ceb7SPatrick McHardy 		return -ENODEV;
982b863ceb7SPatrick McHardy 
983d70f2cf5SKevin Wallace 	/* When creating macvlans or macvtaps on top of other macvlans - use
984b0832a29SEric Biederman 	 * the real device as the lowerdev.
985a6ca5f1dSPatrick McHardy 	 */
986d70f2cf5SKevin Wallace 	if (netif_is_macvlan(lowerdev))
987d70f2cf5SKevin Wallace 		lowerdev = macvlan_dev_real_dev(lowerdev);
988a6ca5f1dSPatrick McHardy 
989b863ceb7SPatrick McHardy 	if (!tb[IFLA_MTU])
990b863ceb7SPatrick McHardy 		dev->mtu = lowerdev->mtu;
991b863ceb7SPatrick McHardy 	else if (dev->mtu > lowerdev->mtu)
992b863ceb7SPatrick McHardy 		return -EINVAL;
993b863ceb7SPatrick McHardy 
994b863ceb7SPatrick McHardy 	if (!tb[IFLA_ADDRESS])
9957ce5d222SDanny Kukawka 		eth_hw_addr_random(dev);
996b863ceb7SPatrick McHardy 
997a35e2c1bSJiri Pirko 	if (!macvlan_port_exists(lowerdev)) {
998b863ceb7SPatrick McHardy 		err = macvlan_port_create(lowerdev);
999b863ceb7SPatrick McHardy 		if (err < 0)
1000b863ceb7SPatrick McHardy 			return err;
1001b863ceb7SPatrick McHardy 	}
1002e052f7e6SEric Dumazet 	port = macvlan_port_get_rtnl(lowerdev);
1003b863ceb7SPatrick McHardy 
1004eb06acdcSSridhar Samudrala 	/* Only 1 macvlan device can be created in passthru mode */
1005eb06acdcSSridhar Samudrala 	if (port->passthru)
1006eb06acdcSSridhar Samudrala 		return -EINVAL;
1007eb06acdcSSridhar Samudrala 
1008b863ceb7SPatrick McHardy 	vlan->lowerdev = lowerdev;
1009b863ceb7SPatrick McHardy 	vlan->dev      = dev;
1010b863ceb7SPatrick McHardy 	vlan->port     = port;
10112be5c767SVlad Yasevich 	vlan->set_features = MACVLAN_FEATURES;
1012c674ac30SVlad Yasevich 	vlan->nest_level = dev_get_nest_level(lowerdev, netif_is_macvlan) + 1;
1013b863ceb7SPatrick McHardy 
101427c0b1a8SArnd Bergmann 	vlan->mode     = MACVLAN_MODE_VEPA;
101527c0b1a8SArnd Bergmann 	if (data && data[IFLA_MACVLAN_MODE])
101627c0b1a8SArnd Bergmann 		vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
101727c0b1a8SArnd Bergmann 
1018df8ef8f3SJohn Fastabend 	if (data && data[IFLA_MACVLAN_FLAGS])
1019df8ef8f3SJohn Fastabend 		vlan->flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
1020df8ef8f3SJohn Fastabend 
1021eb06acdcSSridhar Samudrala 	if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
1022a188a54dSdingtianhong 		if (!MACVLAN_PORT_IS_EMPTY(port))
1023eb06acdcSSridhar Samudrala 			return -EINVAL;
1024eb06acdcSSridhar Samudrala 		port->passthru = true;
10258b98604eSBjørn Mork 		eth_hw_addr_inherit(dev, lowerdev);
1026eb06acdcSSridhar Samudrala 	}
1027eb06acdcSSridhar Samudrala 
102847d4ab91SJohn Fastabend 	err = register_netdevice(dev);
102947d4ab91SJohn Fastabend 	if (err < 0)
103047d4ab91SJohn Fastabend 		goto destroy_port;
103147d4ab91SJohn Fastabend 
1032a6cc0cfaSJohn Fastabend 	dev->priv_flags |= IFF_MACVLAN;
10337cd43db7SJiri Pirko 	err = netdev_upper_dev_link(lowerdev, dev);
10347cd43db7SJiri Pirko 	if (err)
1035da37705cSCong Wang 		goto unregister_netdev;
1036b863ceb7SPatrick McHardy 
1037233c7df0SJiri Pirko 	list_add_tail_rcu(&vlan->list, &port->vlans);
1038fc4a7489SPatrick Mullaney 	netif_stacked_transfer_operstate(lowerdev, dev);
1039f16d3d57SJiri Pirko 
1040b863ceb7SPatrick McHardy 	return 0;
1041f16d3d57SJiri Pirko 
1042da37705cSCong Wang unregister_netdev:
1043da37705cSCong Wang 	unregister_netdevice(dev);
1044f16d3d57SJiri Pirko destroy_port:
1045a188a54dSdingtianhong 	if (MACVLAN_PORT_IS_EMPTY(port))
1046f16d3d57SJiri Pirko 		macvlan_port_destroy(lowerdev);
1047f16d3d57SJiri Pirko 
1048f16d3d57SJiri Pirko 	return err;
1049b863ceb7SPatrick McHardy }
1050fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_common_newlink);
1051b863ceb7SPatrick McHardy 
1052fc0663d6SArnd Bergmann static int macvlan_newlink(struct net *src_net, struct net_device *dev,
1053fc0663d6SArnd Bergmann 			   struct nlattr *tb[], struct nlattr *data[])
1054fc0663d6SArnd Bergmann {
10552f6a1b66SVlad Yasevich 	return macvlan_common_newlink(src_net, dev, tb, data);
1056fc0663d6SArnd Bergmann }
1057fc0663d6SArnd Bergmann 
1058fc0663d6SArnd Bergmann void macvlan_dellink(struct net_device *dev, struct list_head *head)
1059b863ceb7SPatrick McHardy {
1060b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
1061b863ceb7SPatrick McHardy 
1062233c7df0SJiri Pirko 	list_del_rcu(&vlan->list);
106323289a37SEric Dumazet 	unregister_netdevice_queue(dev, head);
10647cd43db7SJiri Pirko 	netdev_upper_dev_unlink(vlan->lowerdev, dev);
1065b863ceb7SPatrick McHardy }
1066fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_dellink);
1067b863ceb7SPatrick McHardy 
106827c0b1a8SArnd Bergmann static int macvlan_changelink(struct net_device *dev,
106927c0b1a8SArnd Bergmann 		struct nlattr *tb[], struct nlattr *data[])
107027c0b1a8SArnd Bergmann {
107127c0b1a8SArnd Bergmann 	struct macvlan_dev *vlan = netdev_priv(dev);
1072266e8347SMichael S. Tsirkin 	enum macvlan_mode mode;
1073266e8347SMichael S. Tsirkin 	bool set_mode = false;
1074266e8347SMichael S. Tsirkin 
1075266e8347SMichael S. Tsirkin 	/* Validate mode, but don't set yet: setting flags may fail. */
1076266e8347SMichael S. Tsirkin 	if (data && data[IFLA_MACVLAN_MODE]) {
1077266e8347SMichael S. Tsirkin 		set_mode = true;
1078266e8347SMichael S. Tsirkin 		mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
1079266e8347SMichael S. Tsirkin 		/* Passthrough mode can't be set or cleared dynamically */
1080266e8347SMichael S. Tsirkin 		if ((mode == MACVLAN_MODE_PASSTHRU) !=
1081266e8347SMichael S. Tsirkin 		    (vlan->mode == MACVLAN_MODE_PASSTHRU))
1082266e8347SMichael S. Tsirkin 			return -EINVAL;
1083266e8347SMichael S. Tsirkin 	}
108499ffc3e7SMichael S. Tsirkin 
1085df8ef8f3SJohn Fastabend 	if (data && data[IFLA_MACVLAN_FLAGS]) {
1086df8ef8f3SJohn Fastabend 		__u16 flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
1087df8ef8f3SJohn Fastabend 		bool promisc = (flags ^ vlan->flags) & MACVLAN_FLAG_NOPROMISC;
108899ffc3e7SMichael S. Tsirkin 		if (vlan->port->passthru && promisc) {
108999ffc3e7SMichael S. Tsirkin 			int err;
1090df8ef8f3SJohn Fastabend 
109199ffc3e7SMichael S. Tsirkin 			if (flags & MACVLAN_FLAG_NOPROMISC)
109299ffc3e7SMichael S. Tsirkin 				err = dev_set_promiscuity(vlan->lowerdev, -1);
109399ffc3e7SMichael S. Tsirkin 			else
109499ffc3e7SMichael S. Tsirkin 				err = dev_set_promiscuity(vlan->lowerdev, 1);
109599ffc3e7SMichael S. Tsirkin 			if (err < 0)
109699ffc3e7SMichael S. Tsirkin 				return err;
109799ffc3e7SMichael S. Tsirkin 		}
1098df8ef8f3SJohn Fastabend 		vlan->flags = flags;
1099df8ef8f3SJohn Fastabend 	}
1100266e8347SMichael S. Tsirkin 	if (set_mode)
1101266e8347SMichael S. Tsirkin 		vlan->mode = mode;
110227c0b1a8SArnd Bergmann 	return 0;
110327c0b1a8SArnd Bergmann }
110427c0b1a8SArnd Bergmann 
110527c0b1a8SArnd Bergmann static size_t macvlan_get_size(const struct net_device *dev)
110627c0b1a8SArnd Bergmann {
110701fe944fSEric Dumazet 	return (0
110801fe944fSEric Dumazet 		+ nla_total_size(4) /* IFLA_MACVLAN_MODE */
110901fe944fSEric Dumazet 		+ nla_total_size(2) /* IFLA_MACVLAN_FLAGS */
111001fe944fSEric Dumazet 		);
111127c0b1a8SArnd Bergmann }
111227c0b1a8SArnd Bergmann 
111327c0b1a8SArnd Bergmann static int macvlan_fill_info(struct sk_buff *skb,
111427c0b1a8SArnd Bergmann 				const struct net_device *dev)
111527c0b1a8SArnd Bergmann {
111627c0b1a8SArnd Bergmann 	struct macvlan_dev *vlan = netdev_priv(dev);
111727c0b1a8SArnd Bergmann 
1118ead9a76cSDavid S. Miller 	if (nla_put_u32(skb, IFLA_MACVLAN_MODE, vlan->mode))
1119ead9a76cSDavid S. Miller 		goto nla_put_failure;
1120df8ef8f3SJohn Fastabend 	if (nla_put_u16(skb, IFLA_MACVLAN_FLAGS, vlan->flags))
1121df8ef8f3SJohn Fastabend 		goto nla_put_failure;
112227c0b1a8SArnd Bergmann 	return 0;
112327c0b1a8SArnd Bergmann 
112427c0b1a8SArnd Bergmann nla_put_failure:
112527c0b1a8SArnd Bergmann 	return -EMSGSIZE;
112627c0b1a8SArnd Bergmann }
112727c0b1a8SArnd Bergmann 
112827c0b1a8SArnd Bergmann static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = {
112927c0b1a8SArnd Bergmann 	[IFLA_MACVLAN_MODE]  = { .type = NLA_U32 },
1130df8ef8f3SJohn Fastabend 	[IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 },
113127c0b1a8SArnd Bergmann };
113227c0b1a8SArnd Bergmann 
1133fc0663d6SArnd Bergmann int macvlan_link_register(struct rtnl_link_ops *ops)
1134fc0663d6SArnd Bergmann {
1135fc0663d6SArnd Bergmann 	/* common fields */
1136fc0663d6SArnd Bergmann 	ops->priv_size		= sizeof(struct macvlan_dev);
1137fc0663d6SArnd Bergmann 	ops->validate		= macvlan_validate;
1138fc0663d6SArnd Bergmann 	ops->maxtype		= IFLA_MACVLAN_MAX;
1139fc0663d6SArnd Bergmann 	ops->policy		= macvlan_policy;
1140fc0663d6SArnd Bergmann 	ops->changelink		= macvlan_changelink;
1141fc0663d6SArnd Bergmann 	ops->get_size		= macvlan_get_size;
1142fc0663d6SArnd Bergmann 	ops->fill_info		= macvlan_fill_info;
1143fc0663d6SArnd Bergmann 
1144fc0663d6SArnd Bergmann 	return rtnl_link_register(ops);
1145fc0663d6SArnd Bergmann };
1146fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_link_register);
1147fc0663d6SArnd Bergmann 
1148fc0663d6SArnd Bergmann static struct rtnl_link_ops macvlan_link_ops = {
1149b863ceb7SPatrick McHardy 	.kind		= "macvlan",
11508a35747aSHerbert Xu 	.setup		= macvlan_setup,
1151b863ceb7SPatrick McHardy 	.newlink	= macvlan_newlink,
1152b863ceb7SPatrick McHardy 	.dellink	= macvlan_dellink,
1153b863ceb7SPatrick McHardy };
1154b863ceb7SPatrick McHardy 
1155b863ceb7SPatrick McHardy static int macvlan_device_event(struct notifier_block *unused,
1156b863ceb7SPatrick McHardy 				unsigned long event, void *ptr)
1157b863ceb7SPatrick McHardy {
1158351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1159b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan, *next;
1160b863ceb7SPatrick McHardy 	struct macvlan_port *port;
1161226bd341SEric Dumazet 	LIST_HEAD(list_kill);
1162b863ceb7SPatrick McHardy 
1163a35e2c1bSJiri Pirko 	if (!macvlan_port_exists(dev))
1164b863ceb7SPatrick McHardy 		return NOTIFY_DONE;
1165b863ceb7SPatrick McHardy 
1166e052f7e6SEric Dumazet 	port = macvlan_port_get_rtnl(dev);
1167a35e2c1bSJiri Pirko 
1168b863ceb7SPatrick McHardy 	switch (event) {
1169b863ceb7SPatrick McHardy 	case NETDEV_CHANGE:
1170b863ceb7SPatrick McHardy 		list_for_each_entry(vlan, &port->vlans, list)
1171fc4a7489SPatrick Mullaney 			netif_stacked_transfer_operstate(vlan->lowerdev,
1172fc4a7489SPatrick Mullaney 							 vlan->dev);
1173b863ceb7SPatrick McHardy 		break;
1174b863ceb7SPatrick McHardy 	case NETDEV_FEAT_CHANGE:
1175b863ceb7SPatrick McHardy 		list_for_each_entry(vlan, &port->vlans, list) {
11768c2acc53SPatrick McHardy 			vlan->dev->gso_max_size = dev->gso_max_size;
1177797f87f8SFlorian Westphal 			netdev_update_features(vlan->dev);
1178b863ceb7SPatrick McHardy 		}
1179b863ceb7SPatrick McHardy 		break;
11803763e7efSdingtianhong 	case NETDEV_CHANGEMTU:
11813763e7efSdingtianhong 		list_for_each_entry(vlan, &port->vlans, list) {
11823763e7efSdingtianhong 			if (vlan->dev->mtu <= dev->mtu)
11833763e7efSdingtianhong 				continue;
11843763e7efSdingtianhong 			dev_set_mtu(vlan->dev, dev->mtu);
11853763e7efSdingtianhong 		}
11863763e7efSdingtianhong 		break;
1187e289fd28Sdingtianhong 	case NETDEV_CHANGEADDR:
1188e289fd28Sdingtianhong 		if (!port->passthru)
1189e289fd28Sdingtianhong 			return NOTIFY_DONE;
1190e289fd28Sdingtianhong 
1191e289fd28Sdingtianhong 		vlan = list_first_entry_or_null(&port->vlans,
1192e289fd28Sdingtianhong 						struct macvlan_dev,
1193e289fd28Sdingtianhong 						list);
1194e289fd28Sdingtianhong 
1195e289fd28Sdingtianhong 		if (macvlan_sync_address(vlan->dev, dev->dev_addr))
1196e289fd28Sdingtianhong 			return NOTIFY_BAD;
1197e289fd28Sdingtianhong 
1198e289fd28Sdingtianhong 		break;
1199b863ceb7SPatrick McHardy 	case NETDEV_UNREGISTER:
12003b27e105SDavid Lamparter 		/* twiddle thumbs on netns device moves */
12013b27e105SDavid Lamparter 		if (dev->reg_state != NETREG_UNREGISTERING)
12023b27e105SDavid Lamparter 			break;
12033b27e105SDavid Lamparter 
1204b863ceb7SPatrick McHardy 		list_for_each_entry_safe(vlan, next, &port->vlans, list)
1205226bd341SEric Dumazet 			vlan->dev->rtnl_link_ops->dellink(vlan->dev, &list_kill);
1206226bd341SEric Dumazet 		unregister_netdevice_many(&list_kill);
1207226bd341SEric Dumazet 		list_del(&list_kill);
1208b863ceb7SPatrick McHardy 		break;
12091c01fe14SJiri Pirko 	case NETDEV_PRE_TYPE_CHANGE:
12101c01fe14SJiri Pirko 		/* Forbid underlaying device to change its type. */
12111c01fe14SJiri Pirko 		return NOTIFY_BAD;
1212b863ceb7SPatrick McHardy 	}
1213b863ceb7SPatrick McHardy 	return NOTIFY_DONE;
1214b863ceb7SPatrick McHardy }
1215b863ceb7SPatrick McHardy 
1216b863ceb7SPatrick McHardy static struct notifier_block macvlan_notifier_block __read_mostly = {
1217b863ceb7SPatrick McHardy 	.notifier_call	= macvlan_device_event,
1218b863ceb7SPatrick McHardy };
1219b863ceb7SPatrick McHardy 
1220b863ceb7SPatrick McHardy static int __init macvlan_init_module(void)
1221b863ceb7SPatrick McHardy {
1222b863ceb7SPatrick McHardy 	int err;
1223b863ceb7SPatrick McHardy 
1224b863ceb7SPatrick McHardy 	register_netdevice_notifier(&macvlan_notifier_block);
1225b863ceb7SPatrick McHardy 
1226fc0663d6SArnd Bergmann 	err = macvlan_link_register(&macvlan_link_ops);
1227b863ceb7SPatrick McHardy 	if (err < 0)
1228b863ceb7SPatrick McHardy 		goto err1;
1229b863ceb7SPatrick McHardy 	return 0;
1230b863ceb7SPatrick McHardy err1:
1231b863ceb7SPatrick McHardy 	unregister_netdevice_notifier(&macvlan_notifier_block);
1232b863ceb7SPatrick McHardy 	return err;
1233b863ceb7SPatrick McHardy }
1234b863ceb7SPatrick McHardy 
1235b863ceb7SPatrick McHardy static void __exit macvlan_cleanup_module(void)
1236b863ceb7SPatrick McHardy {
1237b863ceb7SPatrick McHardy 	rtnl_link_unregister(&macvlan_link_ops);
1238b863ceb7SPatrick McHardy 	unregister_netdevice_notifier(&macvlan_notifier_block);
1239b863ceb7SPatrick McHardy }
1240b863ceb7SPatrick McHardy 
1241b863ceb7SPatrick McHardy module_init(macvlan_init_module);
1242b863ceb7SPatrick McHardy module_exit(macvlan_cleanup_module);
1243b863ceb7SPatrick McHardy 
1244b863ceb7SPatrick McHardy MODULE_LICENSE("GPL");
1245b863ceb7SPatrick McHardy MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
1246b863ceb7SPatrick McHardy MODULE_DESCRIPTION("Driver for MAC address based VLANs");
1247b863ceb7SPatrick McHardy MODULE_ALIAS_RTNL_LINK("macvlan");
1248