xref: /openbmc/linux/drivers/net/macvlan.c (revision 8a4eb5734e8d1dc60a8c28576bbbdfdcc643626d)
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>
29b863ceb7SPatrick McHardy #include <linux/if_link.h>
30b863ceb7SPatrick McHardy #include <linux/if_macvlan.h>
31b863ceb7SPatrick McHardy #include <net/rtnetlink.h>
32618e1b74SArnd Bergmann #include <net/xfrm.h>
33b863ceb7SPatrick McHardy 
34b863ceb7SPatrick McHardy #define MACVLAN_HASH_SIZE	(1 << BITS_PER_BYTE)
35b863ceb7SPatrick McHardy 
36b863ceb7SPatrick McHardy struct macvlan_port {
37b863ceb7SPatrick McHardy 	struct net_device	*dev;
38b863ceb7SPatrick McHardy 	struct hlist_head	vlan_hash[MACVLAN_HASH_SIZE];
39b863ceb7SPatrick McHardy 	struct list_head	vlans;
408b37ef0aSJiri Pirko 	struct rcu_head		rcu;
41eb06acdcSSridhar Samudrala 	bool 			passthru;
42b863ceb7SPatrick McHardy };
43b863ceb7SPatrick McHardy 
44a35e2c1bSJiri Pirko #define macvlan_port_get_rcu(dev) \
45a35e2c1bSJiri Pirko 	((struct macvlan_port *) rcu_dereference(dev->rx_handler_data))
46a35e2c1bSJiri Pirko #define macvlan_port_get(dev) ((struct macvlan_port *) dev->rx_handler_data)
47a35e2c1bSJiri Pirko #define macvlan_port_exists(dev) (dev->priv_flags & IFF_MACVLAN_PORT)
48a35e2c1bSJiri Pirko 
49b863ceb7SPatrick McHardy static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
50b863ceb7SPatrick McHardy 					       const unsigned char *addr)
51b863ceb7SPatrick McHardy {
52b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan;
53b863ceb7SPatrick McHardy 	struct hlist_node *n;
54b863ceb7SPatrick McHardy 
55b863ceb7SPatrick McHardy 	hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[addr[5]], hlist) {
56ac06713dSEric Dumazet 		if (!compare_ether_addr_64bits(vlan->dev->dev_addr, addr))
57b863ceb7SPatrick McHardy 			return vlan;
58b863ceb7SPatrick McHardy 	}
59b863ceb7SPatrick McHardy 	return NULL;
60b863ceb7SPatrick McHardy }
61b863ceb7SPatrick McHardy 
62f9ac30f0SEric Biederman static void macvlan_hash_add(struct macvlan_dev *vlan)
63f9ac30f0SEric Biederman {
64f9ac30f0SEric Biederman 	struct macvlan_port *port = vlan->port;
65f9ac30f0SEric Biederman 	const unsigned char *addr = vlan->dev->dev_addr;
66f9ac30f0SEric Biederman 
67f9ac30f0SEric Biederman 	hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]);
68f9ac30f0SEric Biederman }
69f9ac30f0SEric Biederman 
70f9ac30f0SEric Biederman static void macvlan_hash_del(struct macvlan_dev *vlan)
71f9ac30f0SEric Biederman {
72f9ac30f0SEric Biederman 	hlist_del_rcu(&vlan->hlist);
73f9ac30f0SEric Biederman 	synchronize_rcu();
74f9ac30f0SEric Biederman }
75f9ac30f0SEric Biederman 
76f9ac30f0SEric Biederman static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
77f9ac30f0SEric Biederman 					const unsigned char *addr)
78f9ac30f0SEric Biederman {
79f9ac30f0SEric Biederman 	macvlan_hash_del(vlan);
80f9ac30f0SEric Biederman 	/* Now that we are unhashed it is safe to change the device
81f9ac30f0SEric Biederman 	 * address without confusing packet delivery.
82f9ac30f0SEric Biederman 	 */
83f9ac30f0SEric Biederman 	memcpy(vlan->dev->dev_addr, addr, ETH_ALEN);
84f9ac30f0SEric Biederman 	macvlan_hash_add(vlan);
85f9ac30f0SEric Biederman }
86f9ac30f0SEric Biederman 
87f9ac30f0SEric Biederman static int macvlan_addr_busy(const struct macvlan_port *port,
88f9ac30f0SEric Biederman 				const unsigned char *addr)
89f9ac30f0SEric Biederman {
90f9ac30f0SEric Biederman 	/* Test to see if the specified multicast address is
91f9ac30f0SEric Biederman 	 * currently in use by the underlying device or
92f9ac30f0SEric Biederman 	 * another macvlan.
93f9ac30f0SEric Biederman 	 */
94ac06713dSEric Dumazet 	if (!compare_ether_addr_64bits(port->dev->dev_addr, addr))
95f9ac30f0SEric Biederman 		return 1;
96f9ac30f0SEric Biederman 
97f9ac30f0SEric Biederman 	if (macvlan_hash_lookup(port, addr))
98f9ac30f0SEric Biederman 		return 1;
99f9ac30f0SEric Biederman 
100f9ac30f0SEric Biederman 	return 0;
101f9ac30f0SEric Biederman }
102f9ac30f0SEric Biederman 
103a1e514c5SArnd Bergmann 
104fc0663d6SArnd Bergmann static int macvlan_broadcast_one(struct sk_buff *skb,
105fc0663d6SArnd Bergmann 				 const struct macvlan_dev *vlan,
106618e1b74SArnd Bergmann 				 const struct ethhdr *eth, bool local)
107a1e514c5SArnd Bergmann {
108fc0663d6SArnd Bergmann 	struct net_device *dev = vlan->dev;
109a1e514c5SArnd Bergmann 	if (!skb)
110a1e514c5SArnd Bergmann 		return NET_RX_DROP;
111a1e514c5SArnd Bergmann 
112618e1b74SArnd Bergmann 	if (local)
113fc0663d6SArnd Bergmann 		return vlan->forward(dev, skb);
114618e1b74SArnd Bergmann 
115a1e514c5SArnd Bergmann 	skb->dev = dev;
116a1e514c5SArnd Bergmann 	if (!compare_ether_addr_64bits(eth->h_dest,
117a1e514c5SArnd Bergmann 				       dev->broadcast))
118a1e514c5SArnd Bergmann 		skb->pkt_type = PACKET_BROADCAST;
119a1e514c5SArnd Bergmann 	else
120a1e514c5SArnd Bergmann 		skb->pkt_type = PACKET_MULTICAST;
121a1e514c5SArnd Bergmann 
122fc0663d6SArnd Bergmann 	return vlan->receive(skb);
123a1e514c5SArnd Bergmann }
124a1e514c5SArnd Bergmann 
125b863ceb7SPatrick McHardy static void macvlan_broadcast(struct sk_buff *skb,
126618e1b74SArnd Bergmann 			      const struct macvlan_port *port,
127618e1b74SArnd Bergmann 			      struct net_device *src,
128618e1b74SArnd Bergmann 			      enum macvlan_mode mode)
129b863ceb7SPatrick McHardy {
130b863ceb7SPatrick McHardy 	const struct ethhdr *eth = eth_hdr(skb);
131b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan;
132b863ceb7SPatrick McHardy 	struct hlist_node *n;
133b863ceb7SPatrick McHardy 	struct sk_buff *nskb;
134b863ceb7SPatrick McHardy 	unsigned int i;
135a1e514c5SArnd Bergmann 	int err;
136b863ceb7SPatrick McHardy 
137efbbced3SPatrick McHardy 	if (skb->protocol == htons(ETH_P_PAUSE))
138efbbced3SPatrick McHardy 		return;
139efbbced3SPatrick McHardy 
140b863ceb7SPatrick McHardy 	for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
141b863ceb7SPatrick McHardy 		hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[i], hlist) {
142618e1b74SArnd Bergmann 			if (vlan->dev == src || !(vlan->mode & mode))
143618e1b74SArnd Bergmann 				continue;
144618e1b74SArnd Bergmann 
145b863ceb7SPatrick McHardy 			nskb = skb_clone(skb, GFP_ATOMIC);
146fc0663d6SArnd Bergmann 			err = macvlan_broadcast_one(nskb, vlan, eth,
147618e1b74SArnd Bergmann 					 mode == MACVLAN_MODE_BRIDGE);
148a1e514c5SArnd Bergmann 			macvlan_count_rx(vlan, skb->len + ETH_HLEN,
149a1e514c5SArnd Bergmann 					 err == NET_RX_SUCCESS, 1);
150b863ceb7SPatrick McHardy 		}
151b863ceb7SPatrick McHardy 	}
152b863ceb7SPatrick McHardy }
153b863ceb7SPatrick McHardy 
154b863ceb7SPatrick McHardy /* called under rcu_read_lock() from netif_receive_skb */
155*8a4eb573SJiri Pirko static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb)
156b863ceb7SPatrick McHardy {
157ab95bfe0SJiri Pirko 	struct macvlan_port *port;
158*8a4eb573SJiri Pirko 	struct sk_buff *skb = *pskb;
159b863ceb7SPatrick McHardy 	const struct ethhdr *eth = eth_hdr(skb);
160b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan;
161618e1b74SArnd Bergmann 	const struct macvlan_dev *src;
162b863ceb7SPatrick McHardy 	struct net_device *dev;
163ba01877fSSridhar Samudrala 	unsigned int len = 0;
164ba01877fSSridhar Samudrala 	int ret = NET_RX_DROP;
165b863ceb7SPatrick McHardy 
166a35e2c1bSJiri Pirko 	port = macvlan_port_get_rcu(skb->dev);
167b863ceb7SPatrick McHardy 	if (is_multicast_ether_addr(eth->h_dest)) {
168618e1b74SArnd Bergmann 		src = macvlan_hash_lookup(port, eth->h_source);
169618e1b74SArnd Bergmann 		if (!src)
170618e1b74SArnd Bergmann 			/* frame comes from an external address */
171618e1b74SArnd Bergmann 			macvlan_broadcast(skb, port, NULL,
172618e1b74SArnd Bergmann 					  MACVLAN_MODE_PRIVATE |
173618e1b74SArnd Bergmann 					  MACVLAN_MODE_VEPA    |
174eb06acdcSSridhar Samudrala 					  MACVLAN_MODE_PASSTHRU|
175618e1b74SArnd Bergmann 					  MACVLAN_MODE_BRIDGE);
176618e1b74SArnd Bergmann 		else if (src->mode == MACVLAN_MODE_VEPA)
177618e1b74SArnd Bergmann 			/* flood to everyone except source */
178618e1b74SArnd Bergmann 			macvlan_broadcast(skb, port, src->dev,
179618e1b74SArnd Bergmann 					  MACVLAN_MODE_VEPA |
180618e1b74SArnd Bergmann 					  MACVLAN_MODE_BRIDGE);
181618e1b74SArnd Bergmann 		else if (src->mode == MACVLAN_MODE_BRIDGE)
182618e1b74SArnd Bergmann 			/*
183618e1b74SArnd Bergmann 			 * flood only to VEPA ports, bridge ports
184618e1b74SArnd Bergmann 			 * already saw the frame on the way out.
185618e1b74SArnd Bergmann 			 */
186618e1b74SArnd Bergmann 			macvlan_broadcast(skb, port, src->dev,
187618e1b74SArnd Bergmann 					  MACVLAN_MODE_VEPA);
188*8a4eb573SJiri Pirko 		return RX_HANDLER_PASS;
189b863ceb7SPatrick McHardy 	}
190b863ceb7SPatrick McHardy 
191eb06acdcSSridhar Samudrala 	if (port->passthru)
192eb06acdcSSridhar Samudrala 		vlan = list_first_entry(&port->vlans, struct macvlan_dev, list);
193eb06acdcSSridhar Samudrala 	else
194b863ceb7SPatrick McHardy 		vlan = macvlan_hash_lookup(port, eth->h_dest);
195b863ceb7SPatrick McHardy 	if (vlan == NULL)
196*8a4eb573SJiri Pirko 		return RX_HANDLER_PASS;
197b863ceb7SPatrick McHardy 
198b863ceb7SPatrick McHardy 	dev = vlan->dev;
199b863ceb7SPatrick McHardy 	if (unlikely(!(dev->flags & IFF_UP))) {
200b863ceb7SPatrick McHardy 		kfree_skb(skb);
201*8a4eb573SJiri Pirko 		return RX_HANDLER_CONSUMED;
202b863ceb7SPatrick McHardy 	}
203a1e514c5SArnd Bergmann 	len = skb->len + ETH_HLEN;
204b863ceb7SPatrick McHardy 	skb = skb_share_check(skb, GFP_ATOMIC);
205a1e514c5SArnd Bergmann 	if (!skb)
206ba01877fSSridhar Samudrala 		goto out;
207b863ceb7SPatrick McHardy 
208b863ceb7SPatrick McHardy 	skb->dev = dev;
209b863ceb7SPatrick McHardy 	skb->pkt_type = PACKET_HOST;
210b863ceb7SPatrick McHardy 
211ba01877fSSridhar Samudrala 	ret = vlan->receive(skb);
212ba01877fSSridhar Samudrala 
213ba01877fSSridhar Samudrala out:
214ba01877fSSridhar Samudrala 	macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, 0);
215*8a4eb573SJiri Pirko 	return RX_HANDLER_CONSUMED;
216b863ceb7SPatrick McHardy }
217b863ceb7SPatrick McHardy 
218618e1b74SArnd Bergmann static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
219618e1b74SArnd Bergmann {
220618e1b74SArnd Bergmann 	const struct macvlan_dev *vlan = netdev_priv(dev);
221618e1b74SArnd Bergmann 	const struct macvlan_port *port = vlan->port;
222618e1b74SArnd Bergmann 	const struct macvlan_dev *dest;
22312a2856bSDaniel Lezcano 	__u8 ip_summed = skb->ip_summed;
224618e1b74SArnd Bergmann 
225618e1b74SArnd Bergmann 	if (vlan->mode == MACVLAN_MODE_BRIDGE) {
226618e1b74SArnd Bergmann 		const struct ethhdr *eth = (void *)skb->data;
22712a2856bSDaniel Lezcano 		skb->ip_summed = CHECKSUM_UNNECESSARY;
228618e1b74SArnd Bergmann 
229618e1b74SArnd Bergmann 		/* send to other bridge ports directly */
230618e1b74SArnd Bergmann 		if (is_multicast_ether_addr(eth->h_dest)) {
231618e1b74SArnd Bergmann 			macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE);
232618e1b74SArnd Bergmann 			goto xmit_world;
233618e1b74SArnd Bergmann 		}
234618e1b74SArnd Bergmann 
235618e1b74SArnd Bergmann 		dest = macvlan_hash_lookup(port, eth->h_dest);
236618e1b74SArnd Bergmann 		if (dest && dest->mode == MACVLAN_MODE_BRIDGE) {
237618e1b74SArnd Bergmann 			unsigned int length = skb->len + ETH_HLEN;
238fc0663d6SArnd Bergmann 			int ret = dest->forward(dest->dev, skb);
239618e1b74SArnd Bergmann 			macvlan_count_rx(dest, length,
240618e1b74SArnd Bergmann 					 ret == NET_RX_SUCCESS, 0);
241618e1b74SArnd Bergmann 
242618e1b74SArnd Bergmann 			return NET_XMIT_SUCCESS;
243618e1b74SArnd Bergmann 		}
244618e1b74SArnd Bergmann 	}
245618e1b74SArnd Bergmann 
246618e1b74SArnd Bergmann xmit_world:
24712a2856bSDaniel Lezcano 	skb->ip_summed = ip_summed;
2488a83a00bSArnd Bergmann 	skb_set_dev(skb, vlan->lowerdev);
249618e1b74SArnd Bergmann 	return dev_queue_xmit(skb);
250618e1b74SArnd Bergmann }
251618e1b74SArnd Bergmann 
252fc0663d6SArnd Bergmann netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
253424efe9cSStephen Hemminger 			       struct net_device *dev)
254b863ceb7SPatrick McHardy {
255b863ceb7SPatrick McHardy 	unsigned int len = skb->len;
256b863ceb7SPatrick McHardy 	int ret;
2578ffab51bSEric Dumazet 	const struct macvlan_dev *vlan = netdev_priv(dev);
258b863ceb7SPatrick McHardy 
259618e1b74SArnd Bergmann 	ret = macvlan_queue_xmit(skb, dev);
2602d6c9ffcSEric Dumazet 	if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
2618ffab51bSEric Dumazet 		struct macvlan_pcpu_stats *pcpu_stats;
2622c114553SEric Dumazet 
2638ffab51bSEric Dumazet 		pcpu_stats = this_cpu_ptr(vlan->pcpu_stats);
2648ffab51bSEric Dumazet 		u64_stats_update_begin(&pcpu_stats->syncp);
2658ffab51bSEric Dumazet 		pcpu_stats->tx_packets++;
2668ffab51bSEric Dumazet 		pcpu_stats->tx_bytes += len;
2678ffab51bSEric Dumazet 		u64_stats_update_end(&pcpu_stats->syncp);
2688ffab51bSEric Dumazet 	} else {
2698ffab51bSEric Dumazet 		this_cpu_inc(vlan->pcpu_stats->tx_dropped);
2708ffab51bSEric Dumazet 	}
271cbbef5e1SPatrick McHardy 	return ret;
272b863ceb7SPatrick McHardy }
273fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_start_xmit);
274b863ceb7SPatrick McHardy 
275b863ceb7SPatrick McHardy static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
2763b04dddeSStephen Hemminger 			       unsigned short type, const void *daddr,
2773b04dddeSStephen Hemminger 			       const void *saddr, unsigned len)
278b863ceb7SPatrick McHardy {
279b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan = netdev_priv(dev);
280b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
281b863ceb7SPatrick McHardy 
2820c4e8581SStephen Hemminger 	return dev_hard_header(skb, lowerdev, type, daddr,
283b863ceb7SPatrick McHardy 			       saddr ? : dev->dev_addr, len);
284b863ceb7SPatrick McHardy }
285b863ceb7SPatrick McHardy 
2863b04dddeSStephen Hemminger static const struct header_ops macvlan_hard_header_ops = {
2873b04dddeSStephen Hemminger 	.create  	= macvlan_hard_header,
2883b04dddeSStephen Hemminger 	.rebuild	= eth_rebuild_header,
2893b04dddeSStephen Hemminger 	.parse		= eth_header_parse,
2903b04dddeSStephen Hemminger 	.cache		= eth_header_cache,
2913b04dddeSStephen Hemminger 	.cache_update	= eth_header_cache_update,
2923b04dddeSStephen Hemminger };
2933b04dddeSStephen Hemminger 
294b863ceb7SPatrick McHardy static int macvlan_open(struct net_device *dev)
295b863ceb7SPatrick McHardy {
296b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
297b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
298b863ceb7SPatrick McHardy 	int err;
299b863ceb7SPatrick McHardy 
300eb06acdcSSridhar Samudrala 	if (vlan->port->passthru) {
301eb06acdcSSridhar Samudrala 		dev_set_promiscuity(lowerdev, 1);
302eb06acdcSSridhar Samudrala 		goto hash_add;
303eb06acdcSSridhar Samudrala 	}
304eb06acdcSSridhar Samudrala 
305f9ac30f0SEric Biederman 	err = -EBUSY;
306f9ac30f0SEric Biederman 	if (macvlan_addr_busy(vlan->port, dev->dev_addr))
307f9ac30f0SEric Biederman 		goto out;
308f9ac30f0SEric Biederman 
309a748ee24SJiri Pirko 	err = dev_uc_add(lowerdev, dev->dev_addr);
310b863ceb7SPatrick McHardy 	if (err < 0)
311b89fb7daSWang Chen 		goto out;
312b89fb7daSWang Chen 	if (dev->flags & IFF_ALLMULTI) {
313b89fb7daSWang Chen 		err = dev_set_allmulti(lowerdev, 1);
314b89fb7daSWang Chen 		if (err < 0)
315b89fb7daSWang Chen 			goto del_unicast;
316b89fb7daSWang Chen 	}
317eb06acdcSSridhar Samudrala 
318eb06acdcSSridhar Samudrala hash_add:
319f9ac30f0SEric Biederman 	macvlan_hash_add(vlan);
320b863ceb7SPatrick McHardy 	return 0;
321b89fb7daSWang Chen 
322b89fb7daSWang Chen del_unicast:
323a748ee24SJiri Pirko 	dev_uc_del(lowerdev, dev->dev_addr);
324b89fb7daSWang Chen out:
325b89fb7daSWang Chen 	return err;
326b863ceb7SPatrick McHardy }
327b863ceb7SPatrick McHardy 
328b863ceb7SPatrick McHardy static int macvlan_stop(struct net_device *dev)
329b863ceb7SPatrick McHardy {
330b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
331b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
332b863ceb7SPatrick McHardy 
333eb06acdcSSridhar Samudrala 	if (vlan->port->passthru) {
334eb06acdcSSridhar Samudrala 		dev_set_promiscuity(lowerdev, -1);
335eb06acdcSSridhar Samudrala 		goto hash_del;
336eb06acdcSSridhar Samudrala 	}
337eb06acdcSSridhar Samudrala 
338b863ceb7SPatrick McHardy 	dev_mc_unsync(lowerdev, dev);
339b863ceb7SPatrick McHardy 	if (dev->flags & IFF_ALLMULTI)
340b863ceb7SPatrick McHardy 		dev_set_allmulti(lowerdev, -1);
341b863ceb7SPatrick McHardy 
342a748ee24SJiri Pirko 	dev_uc_del(lowerdev, dev->dev_addr);
343b863ceb7SPatrick McHardy 
344eb06acdcSSridhar Samudrala hash_del:
345f9ac30f0SEric Biederman 	macvlan_hash_del(vlan);
346b863ceb7SPatrick McHardy 	return 0;
347b863ceb7SPatrick McHardy }
348b863ceb7SPatrick McHardy 
349ad5d20a6SPatrick McHardy static int macvlan_set_mac_address(struct net_device *dev, void *p)
350ad5d20a6SPatrick McHardy {
351ad5d20a6SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
352ad5d20a6SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
353ad5d20a6SPatrick McHardy 	struct sockaddr *addr = p;
354ad5d20a6SPatrick McHardy 	int err;
355ad5d20a6SPatrick McHardy 
356ad5d20a6SPatrick McHardy 	if (!is_valid_ether_addr(addr->sa_data))
357ad5d20a6SPatrick McHardy 		return -EADDRNOTAVAIL;
358ad5d20a6SPatrick McHardy 
359f9ac30f0SEric Biederman 	if (!(dev->flags & IFF_UP)) {
360f9ac30f0SEric Biederman 		/* Just copy in the new address */
361f9ac30f0SEric Biederman 		memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
362f9ac30f0SEric Biederman 	} else {
363f9ac30f0SEric Biederman 		/* Rehash and update the device filters */
364f9ac30f0SEric Biederman 		if (macvlan_addr_busy(vlan->port, addr->sa_data))
365f9ac30f0SEric Biederman 			return -EBUSY;
366ad5d20a6SPatrick McHardy 
367a748ee24SJiri Pirko 		err = dev_uc_add(lowerdev, addr->sa_data);
368ccffad25SJiri Pirko 		if (err)
369ad5d20a6SPatrick McHardy 			return err;
370f9ac30f0SEric Biederman 
371a748ee24SJiri Pirko 		dev_uc_del(lowerdev, dev->dev_addr);
372ad5d20a6SPatrick McHardy 
373f9ac30f0SEric Biederman 		macvlan_hash_change_addr(vlan, addr->sa_data);
374f9ac30f0SEric Biederman 	}
375ad5d20a6SPatrick McHardy 	return 0;
376ad5d20a6SPatrick McHardy }
377ad5d20a6SPatrick McHardy 
378b863ceb7SPatrick McHardy static void macvlan_change_rx_flags(struct net_device *dev, int change)
379b863ceb7SPatrick McHardy {
380b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
381b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
382b863ceb7SPatrick McHardy 
383b863ceb7SPatrick McHardy 	if (change & IFF_ALLMULTI)
384b863ceb7SPatrick McHardy 		dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
385b863ceb7SPatrick McHardy }
386b863ceb7SPatrick McHardy 
387b863ceb7SPatrick McHardy static void macvlan_set_multicast_list(struct net_device *dev)
388b863ceb7SPatrick McHardy {
389b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
390b863ceb7SPatrick McHardy 
391b863ceb7SPatrick McHardy 	dev_mc_sync(vlan->lowerdev, dev);
392b863ceb7SPatrick McHardy }
393b863ceb7SPatrick McHardy 
394b863ceb7SPatrick McHardy static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
395b863ceb7SPatrick McHardy {
396b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
397b863ceb7SPatrick McHardy 
398b863ceb7SPatrick McHardy 	if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
399b863ceb7SPatrick McHardy 		return -EINVAL;
400b863ceb7SPatrick McHardy 	dev->mtu = new_mtu;
401b863ceb7SPatrick McHardy 	return 0;
402b863ceb7SPatrick McHardy }
403b863ceb7SPatrick McHardy 
404b863ceb7SPatrick McHardy /*
405b863ceb7SPatrick McHardy  * macvlan network devices have devices nesting below it and are a special
406b863ceb7SPatrick McHardy  * "super class" of normal network devices; split their locks off into a
407b863ceb7SPatrick McHardy  * separate class since they always nest.
408b863ceb7SPatrick McHardy  */
409b863ceb7SPatrick McHardy static struct lock_class_key macvlan_netdev_xmit_lock_key;
410cf508b12SDavid S. Miller static struct lock_class_key macvlan_netdev_addr_lock_key;
411b863ceb7SPatrick McHardy 
412b863ceb7SPatrick McHardy #define MACVLAN_FEATURES \
413b863ceb7SPatrick McHardy 	(NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
414b863ceb7SPatrick McHardy 	 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
4156eb3a855SPatrick Mullaney 	 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO)
416b863ceb7SPatrick McHardy 
417b863ceb7SPatrick McHardy #define MACVLAN_STATE_MASK \
418b863ceb7SPatrick McHardy 	((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
419b863ceb7SPatrick McHardy 
420e8a0464cSDavid S. Miller static void macvlan_set_lockdep_class_one(struct net_device *dev,
421e8a0464cSDavid S. Miller 					  struct netdev_queue *txq,
422e8a0464cSDavid S. Miller 					  void *_unused)
423c773e847SDavid S. Miller {
424c773e847SDavid S. Miller 	lockdep_set_class(&txq->_xmit_lock,
425c773e847SDavid S. Miller 			  &macvlan_netdev_xmit_lock_key);
426c773e847SDavid S. Miller }
427c773e847SDavid S. Miller 
428c773e847SDavid S. Miller static void macvlan_set_lockdep_class(struct net_device *dev)
429c773e847SDavid S. Miller {
430cf508b12SDavid S. Miller 	lockdep_set_class(&dev->addr_list_lock,
431cf508b12SDavid S. Miller 			  &macvlan_netdev_addr_lock_key);
432e8a0464cSDavid S. Miller 	netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL);
433c773e847SDavid S. Miller }
434c773e847SDavid S. Miller 
435b863ceb7SPatrick McHardy static int macvlan_init(struct net_device *dev)
436b863ceb7SPatrick McHardy {
437b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
438b863ceb7SPatrick McHardy 	const struct net_device *lowerdev = vlan->lowerdev;
439b863ceb7SPatrick McHardy 
440b863ceb7SPatrick McHardy 	dev->state		= (dev->state & ~MACVLAN_STATE_MASK) |
441b863ceb7SPatrick McHardy 				  (lowerdev->state & MACVLAN_STATE_MASK);
442b863ceb7SPatrick McHardy 	dev->features 		= lowerdev->features & MACVLAN_FEATURES;
4438ffab51bSEric Dumazet 	dev->features		|= NETIF_F_LLTX;
4448c2acc53SPatrick McHardy 	dev->gso_max_size	= lowerdev->gso_max_size;
445b863ceb7SPatrick McHardy 	dev->iflink		= lowerdev->ifindex;
446ef5c8996Ssg.tweak@gmail.com 	dev->hard_header_len	= lowerdev->hard_header_len;
447b863ceb7SPatrick McHardy 
448c773e847SDavid S. Miller 	macvlan_set_lockdep_class(dev);
449c773e847SDavid S. Miller 
4508ffab51bSEric Dumazet 	vlan->pcpu_stats = alloc_percpu(struct macvlan_pcpu_stats);
4518ffab51bSEric Dumazet 	if (!vlan->pcpu_stats)
452fccaf710SEric Dumazet 		return -ENOMEM;
453fccaf710SEric Dumazet 
454b863ceb7SPatrick McHardy 	return 0;
455b863ceb7SPatrick McHardy }
456b863ceb7SPatrick McHardy 
457fccaf710SEric Dumazet static void macvlan_uninit(struct net_device *dev)
458fccaf710SEric Dumazet {
459fccaf710SEric Dumazet 	struct macvlan_dev *vlan = netdev_priv(dev);
460fccaf710SEric Dumazet 
4618ffab51bSEric Dumazet 	free_percpu(vlan->pcpu_stats);
462fccaf710SEric Dumazet }
463fccaf710SEric Dumazet 
46428172739SEric Dumazet static struct rtnl_link_stats64 *macvlan_dev_get_stats64(struct net_device *dev,
46528172739SEric Dumazet 							 struct rtnl_link_stats64 *stats)
466fccaf710SEric Dumazet {
467fccaf710SEric Dumazet 	struct macvlan_dev *vlan = netdev_priv(dev);
468fccaf710SEric Dumazet 
4698ffab51bSEric Dumazet 	if (vlan->pcpu_stats) {
4708ffab51bSEric Dumazet 		struct macvlan_pcpu_stats *p;
4718ffab51bSEric Dumazet 		u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
4728ffab51bSEric Dumazet 		u32 rx_errors = 0, tx_dropped = 0;
473bc66154eSEric Dumazet 		unsigned int start;
474fccaf710SEric Dumazet 		int i;
475fccaf710SEric Dumazet 
476fccaf710SEric Dumazet 		for_each_possible_cpu(i) {
4778ffab51bSEric Dumazet 			p = per_cpu_ptr(vlan->pcpu_stats, i);
478bc66154eSEric Dumazet 			do {
479bc66154eSEric Dumazet 				start = u64_stats_fetch_begin_bh(&p->syncp);
480bc66154eSEric Dumazet 				rx_packets	= p->rx_packets;
481bc66154eSEric Dumazet 				rx_bytes	= p->rx_bytes;
482bc66154eSEric Dumazet 				rx_multicast	= p->rx_multicast;
4838ffab51bSEric Dumazet 				tx_packets	= p->tx_packets;
4848ffab51bSEric Dumazet 				tx_bytes	= p->tx_bytes;
485bc66154eSEric Dumazet 			} while (u64_stats_fetch_retry_bh(&p->syncp, start));
4868ffab51bSEric Dumazet 
4878ffab51bSEric Dumazet 			stats->rx_packets	+= rx_packets;
4888ffab51bSEric Dumazet 			stats->rx_bytes		+= rx_bytes;
4898ffab51bSEric Dumazet 			stats->multicast	+= rx_multicast;
4908ffab51bSEric Dumazet 			stats->tx_packets	+= tx_packets;
4918ffab51bSEric Dumazet 			stats->tx_bytes		+= tx_bytes;
4928ffab51bSEric Dumazet 			/* rx_errors & tx_dropped are u32, updated
4938ffab51bSEric Dumazet 			 * without syncp protection.
4948ffab51bSEric Dumazet 			 */
4958ffab51bSEric Dumazet 			rx_errors	+= p->rx_errors;
4968ffab51bSEric Dumazet 			tx_dropped	+= p->tx_dropped;
497fccaf710SEric Dumazet 		}
4988ffab51bSEric Dumazet 		stats->rx_errors	= rx_errors;
4998ffab51bSEric Dumazet 		stats->rx_dropped	= rx_errors;
5008ffab51bSEric Dumazet 		stats->tx_dropped	= tx_dropped;
501fccaf710SEric Dumazet 	}
502fccaf710SEric Dumazet 	return stats;
503fccaf710SEric Dumazet }
504fccaf710SEric Dumazet 
505b863ceb7SPatrick McHardy static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
506b863ceb7SPatrick McHardy 					struct ethtool_drvinfo *drvinfo)
507b863ceb7SPatrick McHardy {
508b863ceb7SPatrick McHardy 	snprintf(drvinfo->driver, 32, "macvlan");
509b863ceb7SPatrick McHardy 	snprintf(drvinfo->version, 32, "0.1");
510b863ceb7SPatrick McHardy }
511b863ceb7SPatrick McHardy 
512b863ceb7SPatrick McHardy static u32 macvlan_ethtool_get_rx_csum(struct net_device *dev)
513b863ceb7SPatrick McHardy {
514b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan = netdev_priv(dev);
515b1b67dd4SPatrick McHardy 	return dev_ethtool_get_rx_csum(vlan->lowerdev);
516b863ceb7SPatrick McHardy }
517b863ceb7SPatrick McHardy 
5189edb8bb6SStephen Hemminger static int macvlan_ethtool_get_settings(struct net_device *dev,
5199edb8bb6SStephen Hemminger 					struct ethtool_cmd *cmd)
5209edb8bb6SStephen Hemminger {
5219edb8bb6SStephen Hemminger 	const struct macvlan_dev *vlan = netdev_priv(dev);
522b1b67dd4SPatrick McHardy 	return dev_ethtool_get_settings(vlan->lowerdev, cmd);
5239edb8bb6SStephen Hemminger }
5249edb8bb6SStephen Hemminger 
5259edb8bb6SStephen Hemminger static u32 macvlan_ethtool_get_flags(struct net_device *dev)
5269edb8bb6SStephen Hemminger {
5279edb8bb6SStephen Hemminger 	const struct macvlan_dev *vlan = netdev_priv(dev);
528b1b67dd4SPatrick McHardy 	return dev_ethtool_get_flags(vlan->lowerdev);
5299edb8bb6SStephen Hemminger }
5309edb8bb6SStephen Hemminger 
531b863ceb7SPatrick McHardy static const struct ethtool_ops macvlan_ethtool_ops = {
532b863ceb7SPatrick McHardy 	.get_link		= ethtool_op_get_link,
5339edb8bb6SStephen Hemminger 	.get_settings		= macvlan_ethtool_get_settings,
534b863ceb7SPatrick McHardy 	.get_rx_csum		= macvlan_ethtool_get_rx_csum,
535b863ceb7SPatrick McHardy 	.get_drvinfo		= macvlan_ethtool_get_drvinfo,
5369edb8bb6SStephen Hemminger 	.get_flags		= macvlan_ethtool_get_flags,
537b863ceb7SPatrick McHardy };
538b863ceb7SPatrick McHardy 
53954a30c97SStephen Hemminger static const struct net_device_ops macvlan_netdev_ops = {
54054a30c97SStephen Hemminger 	.ndo_init		= macvlan_init,
541fccaf710SEric Dumazet 	.ndo_uninit		= macvlan_uninit,
54254a30c97SStephen Hemminger 	.ndo_open		= macvlan_open,
54354a30c97SStephen Hemminger 	.ndo_stop		= macvlan_stop,
54400829823SStephen Hemminger 	.ndo_start_xmit		= macvlan_start_xmit,
54554a30c97SStephen Hemminger 	.ndo_change_mtu		= macvlan_change_mtu,
54654a30c97SStephen Hemminger 	.ndo_change_rx_flags	= macvlan_change_rx_flags,
54754a30c97SStephen Hemminger 	.ndo_set_mac_address	= macvlan_set_mac_address,
54854a30c97SStephen Hemminger 	.ndo_set_multicast_list	= macvlan_set_multicast_list,
549bc66154eSEric Dumazet 	.ndo_get_stats64	= macvlan_dev_get_stats64,
55054a30c97SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
55154a30c97SStephen Hemminger };
55254a30c97SStephen Hemminger 
5538a35747aSHerbert Xu void macvlan_common_setup(struct net_device *dev)
554b863ceb7SPatrick McHardy {
555b863ceb7SPatrick McHardy 	ether_setup(dev);
556b863ceb7SPatrick McHardy 
55793f154b5SEric Dumazet 	dev->priv_flags	       &= ~IFF_XMIT_DST_RELEASE;
55854a30c97SStephen Hemminger 	dev->netdev_ops		= &macvlan_netdev_ops;
559b863ceb7SPatrick McHardy 	dev->destructor		= free_netdev;
5603b04dddeSStephen Hemminger 	dev->header_ops		= &macvlan_hard_header_ops,
561b863ceb7SPatrick McHardy 	dev->ethtool_ops	= &macvlan_ethtool_ops;
5628a35747aSHerbert Xu }
5638a35747aSHerbert Xu EXPORT_SYMBOL_GPL(macvlan_common_setup);
5648a35747aSHerbert Xu 
5658a35747aSHerbert Xu static void macvlan_setup(struct net_device *dev)
5668a35747aSHerbert Xu {
5678a35747aSHerbert Xu 	macvlan_common_setup(dev);
568b863ceb7SPatrick McHardy 	dev->tx_queue_len	= 0;
569b863ceb7SPatrick McHardy }
570b863ceb7SPatrick McHardy 
571b863ceb7SPatrick McHardy static int macvlan_port_create(struct net_device *dev)
572b863ceb7SPatrick McHardy {
573b863ceb7SPatrick McHardy 	struct macvlan_port *port;
574b863ceb7SPatrick McHardy 	unsigned int i;
575ab95bfe0SJiri Pirko 	int err;
576b863ceb7SPatrick McHardy 
577b863ceb7SPatrick McHardy 	if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
578b863ceb7SPatrick McHardy 		return -EINVAL;
579b863ceb7SPatrick McHardy 
580b863ceb7SPatrick McHardy 	port = kzalloc(sizeof(*port), GFP_KERNEL);
581b863ceb7SPatrick McHardy 	if (port == NULL)
582b863ceb7SPatrick McHardy 		return -ENOMEM;
583b863ceb7SPatrick McHardy 
584eb06acdcSSridhar Samudrala 	port->passthru = false;
585b863ceb7SPatrick McHardy 	port->dev = dev;
586b863ceb7SPatrick McHardy 	INIT_LIST_HEAD(&port->vlans);
587b863ceb7SPatrick McHardy 	for (i = 0; i < MACVLAN_HASH_SIZE; i++)
588b863ceb7SPatrick McHardy 		INIT_HLIST_HEAD(&port->vlan_hash[i]);
589ab95bfe0SJiri Pirko 
590a35e2c1bSJiri Pirko 	err = netdev_rx_handler_register(dev, macvlan_handle_frame, port);
591a35e2c1bSJiri Pirko 	if (err)
592ab95bfe0SJiri Pirko 		kfree(port);
593ab95bfe0SJiri Pirko 
594a35e2c1bSJiri Pirko 	dev->priv_flags |= IFF_MACVLAN_PORT;
595ab95bfe0SJiri Pirko 	return err;
596b863ceb7SPatrick McHardy }
597b863ceb7SPatrick McHardy 
5988b37ef0aSJiri Pirko static void macvlan_port_rcu_free(struct rcu_head *head)
5998b37ef0aSJiri Pirko {
6008b37ef0aSJiri Pirko 	struct macvlan_port *port;
6018b37ef0aSJiri Pirko 
6028b37ef0aSJiri Pirko 	port = container_of(head, struct macvlan_port, rcu);
6038b37ef0aSJiri Pirko 	kfree(port);
6048b37ef0aSJiri Pirko }
6058b37ef0aSJiri Pirko 
606b863ceb7SPatrick McHardy static void macvlan_port_destroy(struct net_device *dev)
607b863ceb7SPatrick McHardy {
608a35e2c1bSJiri Pirko 	struct macvlan_port *port = macvlan_port_get(dev);
609b863ceb7SPatrick McHardy 
610a35e2c1bSJiri Pirko 	dev->priv_flags &= ~IFF_MACVLAN_PORT;
611ab95bfe0SJiri Pirko 	netdev_rx_handler_unregister(dev);
6128b37ef0aSJiri Pirko 	call_rcu(&port->rcu, macvlan_port_rcu_free);
613b863ceb7SPatrick McHardy }
614b863ceb7SPatrick McHardy 
615b863ceb7SPatrick McHardy static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
616b863ceb7SPatrick McHardy {
617b863ceb7SPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
618b863ceb7SPatrick McHardy 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
619b863ceb7SPatrick McHardy 			return -EINVAL;
620b863ceb7SPatrick McHardy 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
621b863ceb7SPatrick McHardy 			return -EADDRNOTAVAIL;
622b863ceb7SPatrick McHardy 	}
62327c0b1a8SArnd Bergmann 
62427c0b1a8SArnd Bergmann 	if (data && data[IFLA_MACVLAN_MODE]) {
62527c0b1a8SArnd Bergmann 		switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) {
62627c0b1a8SArnd Bergmann 		case MACVLAN_MODE_PRIVATE:
62727c0b1a8SArnd Bergmann 		case MACVLAN_MODE_VEPA:
62827c0b1a8SArnd Bergmann 		case MACVLAN_MODE_BRIDGE:
629eb06acdcSSridhar Samudrala 		case MACVLAN_MODE_PASSTHRU:
63027c0b1a8SArnd Bergmann 			break;
63127c0b1a8SArnd Bergmann 		default:
63227c0b1a8SArnd Bergmann 			return -EINVAL;
63327c0b1a8SArnd Bergmann 		}
63427c0b1a8SArnd Bergmann 	}
635b863ceb7SPatrick McHardy 	return 0;
636b863ceb7SPatrick McHardy }
637b863ceb7SPatrick McHardy 
638fc0663d6SArnd Bergmann int macvlan_common_newlink(struct net *src_net, struct net_device *dev,
639fc0663d6SArnd Bergmann 			   struct nlattr *tb[], struct nlattr *data[],
640fc0663d6SArnd Bergmann 			   int (*receive)(struct sk_buff *skb),
641fc0663d6SArnd Bergmann 			   int (*forward)(struct net_device *dev,
642fc0663d6SArnd Bergmann 					  struct sk_buff *skb))
643b863ceb7SPatrick McHardy {
644b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
645b863ceb7SPatrick McHardy 	struct macvlan_port *port;
646b863ceb7SPatrick McHardy 	struct net_device *lowerdev;
647b863ceb7SPatrick McHardy 	int err;
648b863ceb7SPatrick McHardy 
649b863ceb7SPatrick McHardy 	if (!tb[IFLA_LINK])
650b863ceb7SPatrick McHardy 		return -EINVAL;
651b863ceb7SPatrick McHardy 
65281adee47SEric W. Biederman 	lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
653b863ceb7SPatrick McHardy 	if (lowerdev == NULL)
654b863ceb7SPatrick McHardy 		return -ENODEV;
655b863ceb7SPatrick McHardy 
656b0832a29SEric Biederman 	/* When creating macvlans on top of other macvlans - use
657b0832a29SEric Biederman 	 * the real device as the lowerdev.
658a6ca5f1dSPatrick McHardy 	 */
659b0832a29SEric Biederman 	if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) {
660b0832a29SEric Biederman 		struct macvlan_dev *lowervlan = netdev_priv(lowerdev);
661b0832a29SEric Biederman 		lowerdev = lowervlan->lowerdev;
662b0832a29SEric Biederman 	}
663a6ca5f1dSPatrick McHardy 
664b863ceb7SPatrick McHardy 	if (!tb[IFLA_MTU])
665b863ceb7SPatrick McHardy 		dev->mtu = lowerdev->mtu;
666b863ceb7SPatrick McHardy 	else if (dev->mtu > lowerdev->mtu)
667b863ceb7SPatrick McHardy 		return -EINVAL;
668b863ceb7SPatrick McHardy 
669b863ceb7SPatrick McHardy 	if (!tb[IFLA_ADDRESS])
670b863ceb7SPatrick McHardy 		random_ether_addr(dev->dev_addr);
671b863ceb7SPatrick McHardy 
672a35e2c1bSJiri Pirko 	if (!macvlan_port_exists(lowerdev)) {
673b863ceb7SPatrick McHardy 		err = macvlan_port_create(lowerdev);
674b863ceb7SPatrick McHardy 		if (err < 0)
675b863ceb7SPatrick McHardy 			return err;
676b863ceb7SPatrick McHardy 	}
677a35e2c1bSJiri Pirko 	port = macvlan_port_get(lowerdev);
678b863ceb7SPatrick McHardy 
679eb06acdcSSridhar Samudrala 	/* Only 1 macvlan device can be created in passthru mode */
680eb06acdcSSridhar Samudrala 	if (port->passthru)
681eb06acdcSSridhar Samudrala 		return -EINVAL;
682eb06acdcSSridhar Samudrala 
683b863ceb7SPatrick McHardy 	vlan->lowerdev = lowerdev;
684b863ceb7SPatrick McHardy 	vlan->dev      = dev;
685b863ceb7SPatrick McHardy 	vlan->port     = port;
686fc0663d6SArnd Bergmann 	vlan->receive  = receive;
687fc0663d6SArnd Bergmann 	vlan->forward  = forward;
688b863ceb7SPatrick McHardy 
68927c0b1a8SArnd Bergmann 	vlan->mode     = MACVLAN_MODE_VEPA;
69027c0b1a8SArnd Bergmann 	if (data && data[IFLA_MACVLAN_MODE])
69127c0b1a8SArnd Bergmann 		vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
69227c0b1a8SArnd Bergmann 
693eb06acdcSSridhar Samudrala 	if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
694eb06acdcSSridhar Samudrala 		if (!list_empty(&port->vlans))
695eb06acdcSSridhar Samudrala 			return -EINVAL;
696eb06acdcSSridhar Samudrala 		port->passthru = true;
697eb06acdcSSridhar Samudrala 		memcpy(dev->dev_addr, lowerdev->dev_addr, ETH_ALEN);
698eb06acdcSSridhar Samudrala 	}
699eb06acdcSSridhar Samudrala 
700b863ceb7SPatrick McHardy 	err = register_netdevice(dev);
701b863ceb7SPatrick McHardy 	if (err < 0)
702f16d3d57SJiri Pirko 		goto destroy_port;
703b863ceb7SPatrick McHardy 
704b863ceb7SPatrick McHardy 	list_add_tail(&vlan->list, &port->vlans);
705fc4a7489SPatrick Mullaney 	netif_stacked_transfer_operstate(lowerdev, dev);
706f16d3d57SJiri Pirko 
707b863ceb7SPatrick McHardy 	return 0;
708f16d3d57SJiri Pirko 
709f16d3d57SJiri Pirko destroy_port:
710f16d3d57SJiri Pirko 	if (list_empty(&port->vlans))
711f16d3d57SJiri Pirko 		macvlan_port_destroy(lowerdev);
712f16d3d57SJiri Pirko 
713f16d3d57SJiri Pirko 	return err;
714b863ceb7SPatrick McHardy }
715fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_common_newlink);
716b863ceb7SPatrick McHardy 
717fc0663d6SArnd Bergmann static int macvlan_newlink(struct net *src_net, struct net_device *dev,
718fc0663d6SArnd Bergmann 			   struct nlattr *tb[], struct nlattr *data[])
719fc0663d6SArnd Bergmann {
720fc0663d6SArnd Bergmann 	return macvlan_common_newlink(src_net, dev, tb, data,
721fc0663d6SArnd Bergmann 				      netif_rx,
722fc0663d6SArnd Bergmann 				      dev_forward_skb);
723fc0663d6SArnd Bergmann }
724fc0663d6SArnd Bergmann 
725fc0663d6SArnd Bergmann void macvlan_dellink(struct net_device *dev, struct list_head *head)
726b863ceb7SPatrick McHardy {
727b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
728b863ceb7SPatrick McHardy 	struct macvlan_port *port = vlan->port;
729b863ceb7SPatrick McHardy 
730b863ceb7SPatrick McHardy 	list_del(&vlan->list);
73123289a37SEric Dumazet 	unregister_netdevice_queue(dev, head);
732b863ceb7SPatrick McHardy 
733b863ceb7SPatrick McHardy 	if (list_empty(&port->vlans))
73473120964SPatrick McHardy 		macvlan_port_destroy(port->dev);
735b863ceb7SPatrick McHardy }
736fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_dellink);
737b863ceb7SPatrick McHardy 
73827c0b1a8SArnd Bergmann static int macvlan_changelink(struct net_device *dev,
73927c0b1a8SArnd Bergmann 		struct nlattr *tb[], struct nlattr *data[])
74027c0b1a8SArnd Bergmann {
74127c0b1a8SArnd Bergmann 	struct macvlan_dev *vlan = netdev_priv(dev);
74227c0b1a8SArnd Bergmann 	if (data && data[IFLA_MACVLAN_MODE])
74327c0b1a8SArnd Bergmann 		vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
74427c0b1a8SArnd Bergmann 	return 0;
74527c0b1a8SArnd Bergmann }
74627c0b1a8SArnd Bergmann 
74727c0b1a8SArnd Bergmann static size_t macvlan_get_size(const struct net_device *dev)
74827c0b1a8SArnd Bergmann {
74927c0b1a8SArnd Bergmann 	return nla_total_size(4);
75027c0b1a8SArnd Bergmann }
75127c0b1a8SArnd Bergmann 
75227c0b1a8SArnd Bergmann static int macvlan_fill_info(struct sk_buff *skb,
75327c0b1a8SArnd Bergmann 				const struct net_device *dev)
75427c0b1a8SArnd Bergmann {
75527c0b1a8SArnd Bergmann 	struct macvlan_dev *vlan = netdev_priv(dev);
75627c0b1a8SArnd Bergmann 
75727c0b1a8SArnd Bergmann 	NLA_PUT_U32(skb, IFLA_MACVLAN_MODE, vlan->mode);
75827c0b1a8SArnd Bergmann 	return 0;
75927c0b1a8SArnd Bergmann 
76027c0b1a8SArnd Bergmann nla_put_failure:
76127c0b1a8SArnd Bergmann 	return -EMSGSIZE;
76227c0b1a8SArnd Bergmann }
76327c0b1a8SArnd Bergmann 
76427c0b1a8SArnd Bergmann static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = {
76527c0b1a8SArnd Bergmann 	[IFLA_MACVLAN_MODE] = { .type = NLA_U32 },
76627c0b1a8SArnd Bergmann };
76727c0b1a8SArnd Bergmann 
768fc0663d6SArnd Bergmann int macvlan_link_register(struct rtnl_link_ops *ops)
769fc0663d6SArnd Bergmann {
770fc0663d6SArnd Bergmann 	/* common fields */
771fc0663d6SArnd Bergmann 	ops->priv_size		= sizeof(struct macvlan_dev);
772fc0663d6SArnd Bergmann 	ops->validate		= macvlan_validate;
773fc0663d6SArnd Bergmann 	ops->maxtype		= IFLA_MACVLAN_MAX;
774fc0663d6SArnd Bergmann 	ops->policy		= macvlan_policy;
775fc0663d6SArnd Bergmann 	ops->changelink		= macvlan_changelink;
776fc0663d6SArnd Bergmann 	ops->get_size		= macvlan_get_size;
777fc0663d6SArnd Bergmann 	ops->fill_info		= macvlan_fill_info;
778fc0663d6SArnd Bergmann 
779fc0663d6SArnd Bergmann 	return rtnl_link_register(ops);
780fc0663d6SArnd Bergmann };
781fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_link_register);
782fc0663d6SArnd Bergmann 
783fc0663d6SArnd Bergmann static struct rtnl_link_ops macvlan_link_ops = {
784b863ceb7SPatrick McHardy 	.kind		= "macvlan",
7858a35747aSHerbert Xu 	.setup		= macvlan_setup,
786b863ceb7SPatrick McHardy 	.newlink	= macvlan_newlink,
787b863ceb7SPatrick McHardy 	.dellink	= macvlan_dellink,
788b863ceb7SPatrick McHardy };
789b863ceb7SPatrick McHardy 
790b863ceb7SPatrick McHardy static int macvlan_device_event(struct notifier_block *unused,
791b863ceb7SPatrick McHardy 				unsigned long event, void *ptr)
792b863ceb7SPatrick McHardy {
793b863ceb7SPatrick McHardy 	struct net_device *dev = ptr;
794b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan, *next;
795b863ceb7SPatrick McHardy 	struct macvlan_port *port;
796b863ceb7SPatrick McHardy 
797a35e2c1bSJiri Pirko 	if (!macvlan_port_exists(dev))
798b863ceb7SPatrick McHardy 		return NOTIFY_DONE;
799b863ceb7SPatrick McHardy 
800a35e2c1bSJiri Pirko 	port = macvlan_port_get(dev);
801a35e2c1bSJiri Pirko 
802b863ceb7SPatrick McHardy 	switch (event) {
803b863ceb7SPatrick McHardy 	case NETDEV_CHANGE:
804b863ceb7SPatrick McHardy 		list_for_each_entry(vlan, &port->vlans, list)
805fc4a7489SPatrick Mullaney 			netif_stacked_transfer_operstate(vlan->lowerdev,
806fc4a7489SPatrick Mullaney 							 vlan->dev);
807b863ceb7SPatrick McHardy 		break;
808b863ceb7SPatrick McHardy 	case NETDEV_FEAT_CHANGE:
809b863ceb7SPatrick McHardy 		list_for_each_entry(vlan, &port->vlans, list) {
810b863ceb7SPatrick McHardy 			vlan->dev->features = dev->features & MACVLAN_FEATURES;
8118c2acc53SPatrick McHardy 			vlan->dev->gso_max_size = dev->gso_max_size;
812b863ceb7SPatrick McHardy 			netdev_features_change(vlan->dev);
813b863ceb7SPatrick McHardy 		}
814b863ceb7SPatrick McHardy 		break;
815b863ceb7SPatrick McHardy 	case NETDEV_UNREGISTER:
8163b27e105SDavid Lamparter 		/* twiddle thumbs on netns device moves */
8173b27e105SDavid Lamparter 		if (dev->reg_state != NETREG_UNREGISTERING)
8183b27e105SDavid Lamparter 			break;
8193b27e105SDavid Lamparter 
820b863ceb7SPatrick McHardy 		list_for_each_entry_safe(vlan, next, &port->vlans, list)
821fc0663d6SArnd Bergmann 			vlan->dev->rtnl_link_ops->dellink(vlan->dev, NULL);
822b863ceb7SPatrick McHardy 		break;
8231c01fe14SJiri Pirko 	case NETDEV_PRE_TYPE_CHANGE:
8241c01fe14SJiri Pirko 		/* Forbid underlaying device to change its type. */
8251c01fe14SJiri Pirko 		return NOTIFY_BAD;
826b863ceb7SPatrick McHardy 	}
827b863ceb7SPatrick McHardy 	return NOTIFY_DONE;
828b863ceb7SPatrick McHardy }
829b863ceb7SPatrick McHardy 
830b863ceb7SPatrick McHardy static struct notifier_block macvlan_notifier_block __read_mostly = {
831b863ceb7SPatrick McHardy 	.notifier_call	= macvlan_device_event,
832b863ceb7SPatrick McHardy };
833b863ceb7SPatrick McHardy 
834b863ceb7SPatrick McHardy static int __init macvlan_init_module(void)
835b863ceb7SPatrick McHardy {
836b863ceb7SPatrick McHardy 	int err;
837b863ceb7SPatrick McHardy 
838b863ceb7SPatrick McHardy 	register_netdevice_notifier(&macvlan_notifier_block);
839b863ceb7SPatrick McHardy 
840fc0663d6SArnd Bergmann 	err = macvlan_link_register(&macvlan_link_ops);
841b863ceb7SPatrick McHardy 	if (err < 0)
842b863ceb7SPatrick McHardy 		goto err1;
843b863ceb7SPatrick McHardy 	return 0;
844b863ceb7SPatrick McHardy err1:
845b863ceb7SPatrick McHardy 	unregister_netdevice_notifier(&macvlan_notifier_block);
846b863ceb7SPatrick McHardy 	return err;
847b863ceb7SPatrick McHardy }
848b863ceb7SPatrick McHardy 
849b863ceb7SPatrick McHardy static void __exit macvlan_cleanup_module(void)
850b863ceb7SPatrick McHardy {
851b863ceb7SPatrick McHardy 	rtnl_link_unregister(&macvlan_link_ops);
852b863ceb7SPatrick McHardy 	unregister_netdevice_notifier(&macvlan_notifier_block);
853b863ceb7SPatrick McHardy }
854b863ceb7SPatrick McHardy 
855b863ceb7SPatrick McHardy module_init(macvlan_init_module);
856b863ceb7SPatrick McHardy module_exit(macvlan_cleanup_module);
857b863ceb7SPatrick McHardy 
858b863ceb7SPatrick McHardy MODULE_LICENSE("GPL");
859b863ceb7SPatrick McHardy MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
860b863ceb7SPatrick McHardy MODULE_DESCRIPTION("Driver for MAC address based VLANs");
861b863ceb7SPatrick McHardy MODULE_ALIAS_RTNL_LINK("macvlan");
862