xref: /linux/drivers/net/macvlan.c (revision 233c7df0821c4190e2d3f4be0f2ca0ab40a5ed8c)
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>
33b863ceb7SPatrick McHardy #include <net/rtnetlink.h>
34618e1b74SArnd Bergmann #include <net/xfrm.h>
35b863ceb7SPatrick McHardy 
36b863ceb7SPatrick McHardy #define MACVLAN_HASH_SIZE	(1 << BITS_PER_BYTE)
37b863ceb7SPatrick McHardy 
38b863ceb7SPatrick McHardy struct macvlan_port {
39b863ceb7SPatrick McHardy 	struct net_device	*dev;
40b863ceb7SPatrick McHardy 	struct hlist_head	vlan_hash[MACVLAN_HASH_SIZE];
41b863ceb7SPatrick McHardy 	struct list_head	vlans;
428b37ef0aSJiri Pirko 	struct rcu_head		rcu;
43eb06acdcSSridhar Samudrala 	bool 			passthru;
44d5cd9244SEric W. Biederman 	int			count;
45b863ceb7SPatrick McHardy };
46b863ceb7SPatrick McHardy 
47d5cd9244SEric W. Biederman static void macvlan_port_destroy(struct net_device *dev);
48d5cd9244SEric W. Biederman 
49e052f7e6SEric Dumazet static struct macvlan_port *macvlan_port_get_rcu(const struct net_device *dev)
50e052f7e6SEric Dumazet {
51e052f7e6SEric Dumazet 	return rcu_dereference(dev->rx_handler_data);
52e052f7e6SEric Dumazet }
53e052f7e6SEric Dumazet 
54e052f7e6SEric Dumazet static struct macvlan_port *macvlan_port_get_rtnl(const struct net_device *dev)
55e052f7e6SEric Dumazet {
56e052f7e6SEric Dumazet 	return rtnl_dereference(dev->rx_handler_data);
57e052f7e6SEric Dumazet }
58e052f7e6SEric Dumazet 
59a35e2c1bSJiri Pirko #define macvlan_port_exists(dev) (dev->priv_flags & IFF_MACVLAN_PORT)
60a35e2c1bSJiri Pirko 
61b863ceb7SPatrick McHardy static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
62b863ceb7SPatrick McHardy 					       const unsigned char *addr)
63b863ceb7SPatrick McHardy {
64b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan;
65b863ceb7SPatrick McHardy 
66b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(vlan, &port->vlan_hash[addr[5]], hlist) {
67a6700db1SJoe Perches 		if (ether_addr_equal_64bits(vlan->dev->dev_addr, addr))
68b863ceb7SPatrick McHardy 			return vlan;
69b863ceb7SPatrick McHardy 	}
70b863ceb7SPatrick McHardy 	return NULL;
71b863ceb7SPatrick McHardy }
72b863ceb7SPatrick McHardy 
73f9ac30f0SEric Biederman static void macvlan_hash_add(struct macvlan_dev *vlan)
74f9ac30f0SEric Biederman {
75f9ac30f0SEric Biederman 	struct macvlan_port *port = vlan->port;
76f9ac30f0SEric Biederman 	const unsigned char *addr = vlan->dev->dev_addr;
77f9ac30f0SEric Biederman 
78f9ac30f0SEric Biederman 	hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]);
79f9ac30f0SEric Biederman }
80f9ac30f0SEric Biederman 
81449f4544SEric Dumazet static void macvlan_hash_del(struct macvlan_dev *vlan, bool sync)
82f9ac30f0SEric Biederman {
83f9ac30f0SEric Biederman 	hlist_del_rcu(&vlan->hlist);
84449f4544SEric Dumazet 	if (sync)
85f9ac30f0SEric Biederman 		synchronize_rcu();
86f9ac30f0SEric Biederman }
87f9ac30f0SEric Biederman 
88f9ac30f0SEric Biederman static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
89f9ac30f0SEric Biederman 					const unsigned char *addr)
90f9ac30f0SEric Biederman {
91449f4544SEric Dumazet 	macvlan_hash_del(vlan, true);
92f9ac30f0SEric Biederman 	/* Now that we are unhashed it is safe to change the device
93f9ac30f0SEric Biederman 	 * address without confusing packet delivery.
94f9ac30f0SEric Biederman 	 */
95f9ac30f0SEric Biederman 	memcpy(vlan->dev->dev_addr, addr, ETH_ALEN);
96f9ac30f0SEric Biederman 	macvlan_hash_add(vlan);
97f9ac30f0SEric Biederman }
98f9ac30f0SEric Biederman 
99f9ac30f0SEric Biederman static int macvlan_addr_busy(const struct macvlan_port *port,
100f9ac30f0SEric Biederman 				const unsigned char *addr)
101f9ac30f0SEric Biederman {
102f9ac30f0SEric Biederman 	/* Test to see if the specified multicast address is
103f9ac30f0SEric Biederman 	 * currently in use by the underlying device or
104f9ac30f0SEric Biederman 	 * another macvlan.
105f9ac30f0SEric Biederman 	 */
106a6700db1SJoe Perches 	if (ether_addr_equal_64bits(port->dev->dev_addr, addr))
107f9ac30f0SEric Biederman 		return 1;
108f9ac30f0SEric Biederman 
109f9ac30f0SEric Biederman 	if (macvlan_hash_lookup(port, addr))
110f9ac30f0SEric Biederman 		return 1;
111f9ac30f0SEric Biederman 
112f9ac30f0SEric Biederman 	return 0;
113f9ac30f0SEric Biederman }
114f9ac30f0SEric Biederman 
115a1e514c5SArnd Bergmann 
116fc0663d6SArnd Bergmann static int macvlan_broadcast_one(struct sk_buff *skb,
117fc0663d6SArnd Bergmann 				 const struct macvlan_dev *vlan,
118618e1b74SArnd Bergmann 				 const struct ethhdr *eth, bool local)
119a1e514c5SArnd Bergmann {
120fc0663d6SArnd Bergmann 	struct net_device *dev = vlan->dev;
121a1e514c5SArnd Bergmann 	if (!skb)
122a1e514c5SArnd Bergmann 		return NET_RX_DROP;
123a1e514c5SArnd Bergmann 
124618e1b74SArnd Bergmann 	if (local)
125fc0663d6SArnd Bergmann 		return vlan->forward(dev, skb);
126618e1b74SArnd Bergmann 
127a1e514c5SArnd Bergmann 	skb->dev = dev;
128a6700db1SJoe Perches 	if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast))
129a1e514c5SArnd Bergmann 		skb->pkt_type = PACKET_BROADCAST;
130a1e514c5SArnd Bergmann 	else
131a1e514c5SArnd Bergmann 		skb->pkt_type = PACKET_MULTICAST;
132a1e514c5SArnd Bergmann 
133fc0663d6SArnd Bergmann 	return vlan->receive(skb);
134a1e514c5SArnd Bergmann }
135a1e514c5SArnd Bergmann 
1363807ff58SEric Dumazet static u32 macvlan_hash_mix(const struct macvlan_dev *vlan)
1373807ff58SEric Dumazet {
1383807ff58SEric Dumazet 	return (u32)(((unsigned long)vlan) >> L1_CACHE_SHIFT);
1393807ff58SEric Dumazet }
1403807ff58SEric Dumazet 
1413807ff58SEric Dumazet 
1423807ff58SEric Dumazet static unsigned int mc_hash(const struct macvlan_dev *vlan,
1433807ff58SEric Dumazet 			    const unsigned char *addr)
144cd431e73SEric Dumazet {
145cd431e73SEric Dumazet 	u32 val = __get_unaligned_cpu32(addr + 2);
146cd431e73SEric Dumazet 
1473807ff58SEric Dumazet 	val ^= macvlan_hash_mix(vlan);
148cd431e73SEric Dumazet 	return hash_32(val, MACVLAN_MC_FILTER_BITS);
149cd431e73SEric Dumazet }
150cd431e73SEric Dumazet 
151b863ceb7SPatrick McHardy static void macvlan_broadcast(struct sk_buff *skb,
152618e1b74SArnd Bergmann 			      const struct macvlan_port *port,
153618e1b74SArnd Bergmann 			      struct net_device *src,
154618e1b74SArnd Bergmann 			      enum macvlan_mode mode)
155b863ceb7SPatrick McHardy {
156b863ceb7SPatrick McHardy 	const struct ethhdr *eth = eth_hdr(skb);
157b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan;
158b863ceb7SPatrick McHardy 	struct sk_buff *nskb;
159b863ceb7SPatrick McHardy 	unsigned int i;
160a1e514c5SArnd Bergmann 	int err;
1613807ff58SEric Dumazet 	unsigned int hash;
162b863ceb7SPatrick McHardy 
163efbbced3SPatrick McHardy 	if (skb->protocol == htons(ETH_P_PAUSE))
164efbbced3SPatrick McHardy 		return;
165efbbced3SPatrick McHardy 
166b863ceb7SPatrick McHardy 	for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
167b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(vlan, &port->vlan_hash[i], hlist) {
168618e1b74SArnd Bergmann 			if (vlan->dev == src || !(vlan->mode & mode))
169618e1b74SArnd Bergmann 				continue;
170618e1b74SArnd Bergmann 
1713807ff58SEric Dumazet 			hash = mc_hash(vlan, eth->h_dest);
172cd431e73SEric Dumazet 			if (!test_bit(hash, vlan->mc_filter))
173cd431e73SEric Dumazet 				continue;
174b863ceb7SPatrick McHardy 			nskb = skb_clone(skb, GFP_ATOMIC);
175fc0663d6SArnd Bergmann 			err = macvlan_broadcast_one(nskb, vlan, eth,
176618e1b74SArnd Bergmann 					 mode == MACVLAN_MODE_BRIDGE);
177a1e514c5SArnd Bergmann 			macvlan_count_rx(vlan, skb->len + ETH_HLEN,
178a1e514c5SArnd Bergmann 					 err == NET_RX_SUCCESS, 1);
179b863ceb7SPatrick McHardy 		}
180b863ceb7SPatrick McHardy 	}
181b863ceb7SPatrick McHardy }
182b863ceb7SPatrick McHardy 
183b863ceb7SPatrick McHardy /* called under rcu_read_lock() from netif_receive_skb */
1848a4eb573SJiri Pirko static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb)
185b863ceb7SPatrick McHardy {
186ab95bfe0SJiri Pirko 	struct macvlan_port *port;
1878a4eb573SJiri Pirko 	struct sk_buff *skb = *pskb;
188b863ceb7SPatrick McHardy 	const struct ethhdr *eth = eth_hdr(skb);
189b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan;
190618e1b74SArnd Bergmann 	const struct macvlan_dev *src;
191b863ceb7SPatrick McHardy 	struct net_device *dev;
192ba01877fSSridhar Samudrala 	unsigned int len = 0;
193ba01877fSSridhar Samudrala 	int ret = NET_RX_DROP;
194b863ceb7SPatrick McHardy 
195a35e2c1bSJiri Pirko 	port = macvlan_port_get_rcu(skb->dev);
196b863ceb7SPatrick McHardy 	if (is_multicast_ether_addr(eth->h_dest)) {
197bc416d97SEric Dumazet 		skb = ip_check_defrag(skb, IP_DEFRAG_MACVLAN);
198bc416d97SEric Dumazet 		if (!skb)
199bc416d97SEric Dumazet 			return RX_HANDLER_CONSUMED;
2004ec7ac12SEric Dumazet 		eth = eth_hdr(skb);
201618e1b74SArnd Bergmann 		src = macvlan_hash_lookup(port, eth->h_source);
202618e1b74SArnd Bergmann 		if (!src)
203618e1b74SArnd Bergmann 			/* frame comes from an external address */
204618e1b74SArnd Bergmann 			macvlan_broadcast(skb, port, NULL,
205618e1b74SArnd Bergmann 					  MACVLAN_MODE_PRIVATE |
206618e1b74SArnd Bergmann 					  MACVLAN_MODE_VEPA    |
207eb06acdcSSridhar Samudrala 					  MACVLAN_MODE_PASSTHRU|
208618e1b74SArnd Bergmann 					  MACVLAN_MODE_BRIDGE);
209618e1b74SArnd Bergmann 		else if (src->mode == MACVLAN_MODE_VEPA)
210618e1b74SArnd Bergmann 			/* flood to everyone except source */
211618e1b74SArnd Bergmann 			macvlan_broadcast(skb, port, src->dev,
212618e1b74SArnd Bergmann 					  MACVLAN_MODE_VEPA |
213618e1b74SArnd Bergmann 					  MACVLAN_MODE_BRIDGE);
214618e1b74SArnd Bergmann 		else if (src->mode == MACVLAN_MODE_BRIDGE)
215618e1b74SArnd Bergmann 			/*
216618e1b74SArnd Bergmann 			 * flood only to VEPA ports, bridge ports
217618e1b74SArnd Bergmann 			 * already saw the frame on the way out.
218618e1b74SArnd Bergmann 			 */
219618e1b74SArnd Bergmann 			macvlan_broadcast(skb, port, src->dev,
220618e1b74SArnd Bergmann 					  MACVLAN_MODE_VEPA);
221729e72a1Sstephen hemminger 		else {
222729e72a1Sstephen hemminger 			/* forward to original port. */
223729e72a1Sstephen hemminger 			vlan = src;
224729e72a1Sstephen hemminger 			ret = macvlan_broadcast_one(skb, vlan, eth, 0);
225729e72a1Sstephen hemminger 			goto out;
226729e72a1Sstephen hemminger 		}
227729e72a1Sstephen hemminger 
2288a4eb573SJiri Pirko 		return RX_HANDLER_PASS;
229b863ceb7SPatrick McHardy 	}
230b863ceb7SPatrick McHardy 
231eb06acdcSSridhar Samudrala 	if (port->passthru)
232*233c7df0SJiri Pirko 		vlan = list_first_or_null_rcu(&port->vlans,
233*233c7df0SJiri Pirko 					      struct macvlan_dev, list);
234eb06acdcSSridhar Samudrala 	else
235b863ceb7SPatrick McHardy 		vlan = macvlan_hash_lookup(port, eth->h_dest);
236b863ceb7SPatrick McHardy 	if (vlan == NULL)
2378a4eb573SJiri Pirko 		return RX_HANDLER_PASS;
238b863ceb7SPatrick McHardy 
239b863ceb7SPatrick McHardy 	dev = vlan->dev;
240b863ceb7SPatrick McHardy 	if (unlikely(!(dev->flags & IFF_UP))) {
241b863ceb7SPatrick McHardy 		kfree_skb(skb);
2428a4eb573SJiri Pirko 		return RX_HANDLER_CONSUMED;
243b863ceb7SPatrick McHardy 	}
244a1e514c5SArnd Bergmann 	len = skb->len + ETH_HLEN;
245b863ceb7SPatrick McHardy 	skb = skb_share_check(skb, GFP_ATOMIC);
246a1e514c5SArnd Bergmann 	if (!skb)
247ba01877fSSridhar Samudrala 		goto out;
248b863ceb7SPatrick McHardy 
249b863ceb7SPatrick McHardy 	skb->dev = dev;
250b863ceb7SPatrick McHardy 	skb->pkt_type = PACKET_HOST;
251b863ceb7SPatrick McHardy 
252ba01877fSSridhar Samudrala 	ret = vlan->receive(skb);
253ba01877fSSridhar Samudrala 
254ba01877fSSridhar Samudrala out:
255ba01877fSSridhar Samudrala 	macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, 0);
2568a4eb573SJiri Pirko 	return RX_HANDLER_CONSUMED;
257b863ceb7SPatrick McHardy }
258b863ceb7SPatrick McHardy 
259618e1b74SArnd Bergmann static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
260618e1b74SArnd Bergmann {
261618e1b74SArnd Bergmann 	const struct macvlan_dev *vlan = netdev_priv(dev);
262618e1b74SArnd Bergmann 	const struct macvlan_port *port = vlan->port;
263618e1b74SArnd Bergmann 	const struct macvlan_dev *dest;
26412a2856bSDaniel Lezcano 	__u8 ip_summed = skb->ip_summed;
265618e1b74SArnd Bergmann 
266618e1b74SArnd Bergmann 	if (vlan->mode == MACVLAN_MODE_BRIDGE) {
267618e1b74SArnd Bergmann 		const struct ethhdr *eth = (void *)skb->data;
26812a2856bSDaniel Lezcano 		skb->ip_summed = CHECKSUM_UNNECESSARY;
269618e1b74SArnd Bergmann 
270618e1b74SArnd Bergmann 		/* send to other bridge ports directly */
271618e1b74SArnd Bergmann 		if (is_multicast_ether_addr(eth->h_dest)) {
272618e1b74SArnd Bergmann 			macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE);
273618e1b74SArnd Bergmann 			goto xmit_world;
274618e1b74SArnd Bergmann 		}
275618e1b74SArnd Bergmann 
276618e1b74SArnd Bergmann 		dest = macvlan_hash_lookup(port, eth->h_dest);
277618e1b74SArnd Bergmann 		if (dest && dest->mode == MACVLAN_MODE_BRIDGE) {
278a37dd333SDavid Ward 			/* send to lowerdev first for its network taps */
279cb2d0f3eSDavid Ward 			dev_forward_skb(vlan->lowerdev, skb);
280618e1b74SArnd Bergmann 
281618e1b74SArnd Bergmann 			return NET_XMIT_SUCCESS;
282618e1b74SArnd Bergmann 		}
283618e1b74SArnd Bergmann 	}
284618e1b74SArnd Bergmann 
285618e1b74SArnd Bergmann xmit_world:
28612a2856bSDaniel Lezcano 	skb->ip_summed = ip_summed;
28759b9997bSDavid S. Miller 	skb->dev = vlan->lowerdev;
288618e1b74SArnd Bergmann 	return dev_queue_xmit(skb);
289618e1b74SArnd Bergmann }
290618e1b74SArnd Bergmann 
291fc0663d6SArnd Bergmann netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
292424efe9cSStephen Hemminger 			       struct net_device *dev)
293b863ceb7SPatrick McHardy {
294b863ceb7SPatrick McHardy 	unsigned int len = skb->len;
295b863ceb7SPatrick McHardy 	int ret;
2968ffab51bSEric Dumazet 	const struct macvlan_dev *vlan = netdev_priv(dev);
297b863ceb7SPatrick McHardy 
298618e1b74SArnd Bergmann 	ret = macvlan_queue_xmit(skb, dev);
2992d6c9ffcSEric Dumazet 	if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
3008ffab51bSEric Dumazet 		struct macvlan_pcpu_stats *pcpu_stats;
3012c114553SEric Dumazet 
3028ffab51bSEric Dumazet 		pcpu_stats = this_cpu_ptr(vlan->pcpu_stats);
3038ffab51bSEric Dumazet 		u64_stats_update_begin(&pcpu_stats->syncp);
3048ffab51bSEric Dumazet 		pcpu_stats->tx_packets++;
3058ffab51bSEric Dumazet 		pcpu_stats->tx_bytes += len;
3068ffab51bSEric Dumazet 		u64_stats_update_end(&pcpu_stats->syncp);
3078ffab51bSEric Dumazet 	} else {
3088ffab51bSEric Dumazet 		this_cpu_inc(vlan->pcpu_stats->tx_dropped);
3098ffab51bSEric Dumazet 	}
310cbbef5e1SPatrick McHardy 	return ret;
311b863ceb7SPatrick McHardy }
312fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_start_xmit);
313b863ceb7SPatrick McHardy 
314b863ceb7SPatrick McHardy static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
3153b04dddeSStephen Hemminger 			       unsigned short type, const void *daddr,
3163b04dddeSStephen Hemminger 			       const void *saddr, unsigned len)
317b863ceb7SPatrick McHardy {
318b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan = netdev_priv(dev);
319b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
320b863ceb7SPatrick McHardy 
3210c4e8581SStephen Hemminger 	return dev_hard_header(skb, lowerdev, type, daddr,
322b863ceb7SPatrick McHardy 			       saddr ? : dev->dev_addr, len);
323b863ceb7SPatrick McHardy }
324b863ceb7SPatrick McHardy 
3253b04dddeSStephen Hemminger static const struct header_ops macvlan_hard_header_ops = {
3263b04dddeSStephen Hemminger 	.create  	= macvlan_hard_header,
3273b04dddeSStephen Hemminger 	.rebuild	= eth_rebuild_header,
3283b04dddeSStephen Hemminger 	.parse		= eth_header_parse,
3293b04dddeSStephen Hemminger 	.cache		= eth_header_cache,
3303b04dddeSStephen Hemminger 	.cache_update	= eth_header_cache_update,
3313b04dddeSStephen Hemminger };
3323b04dddeSStephen Hemminger 
333b863ceb7SPatrick McHardy static int macvlan_open(struct net_device *dev)
334b863ceb7SPatrick McHardy {
335b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
336b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
337b863ceb7SPatrick McHardy 	int err;
338b863ceb7SPatrick McHardy 
339eb06acdcSSridhar Samudrala 	if (vlan->port->passthru) {
340df8ef8f3SJohn Fastabend 		if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC))
341eb06acdcSSridhar Samudrala 			dev_set_promiscuity(lowerdev, 1);
342eb06acdcSSridhar Samudrala 		goto hash_add;
343eb06acdcSSridhar Samudrala 	}
344eb06acdcSSridhar Samudrala 
345f9ac30f0SEric Biederman 	err = -EBUSY;
346f9ac30f0SEric Biederman 	if (macvlan_addr_busy(vlan->port, dev->dev_addr))
347f9ac30f0SEric Biederman 		goto out;
348f9ac30f0SEric Biederman 
349a748ee24SJiri Pirko 	err = dev_uc_add(lowerdev, dev->dev_addr);
350b863ceb7SPatrick McHardy 	if (err < 0)
351b89fb7daSWang Chen 		goto out;
352b89fb7daSWang Chen 	if (dev->flags & IFF_ALLMULTI) {
353b89fb7daSWang Chen 		err = dev_set_allmulti(lowerdev, 1);
354b89fb7daSWang Chen 		if (err < 0)
355b89fb7daSWang Chen 			goto del_unicast;
356b89fb7daSWang Chen 	}
357eb06acdcSSridhar Samudrala 
358eb06acdcSSridhar Samudrala hash_add:
359f9ac30f0SEric Biederman 	macvlan_hash_add(vlan);
360b863ceb7SPatrick McHardy 	return 0;
361b89fb7daSWang Chen 
362b89fb7daSWang Chen del_unicast:
363a748ee24SJiri Pirko 	dev_uc_del(lowerdev, dev->dev_addr);
364b89fb7daSWang Chen out:
365b89fb7daSWang Chen 	return err;
366b863ceb7SPatrick McHardy }
367b863ceb7SPatrick McHardy 
368b863ceb7SPatrick McHardy static int macvlan_stop(struct net_device *dev)
369b863ceb7SPatrick McHardy {
370b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
371b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
372b863ceb7SPatrick McHardy 
373df8ef8f3SJohn Fastabend 	dev_uc_unsync(lowerdev, dev);
374df8ef8f3SJohn Fastabend 	dev_mc_unsync(lowerdev, dev);
375df8ef8f3SJohn Fastabend 
376eb06acdcSSridhar Samudrala 	if (vlan->port->passthru) {
377df8ef8f3SJohn Fastabend 		if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC))
378eb06acdcSSridhar Samudrala 			dev_set_promiscuity(lowerdev, -1);
379eb06acdcSSridhar Samudrala 		goto hash_del;
380eb06acdcSSridhar Samudrala 	}
381eb06acdcSSridhar Samudrala 
382b863ceb7SPatrick McHardy 	if (dev->flags & IFF_ALLMULTI)
383b863ceb7SPatrick McHardy 		dev_set_allmulti(lowerdev, -1);
384b863ceb7SPatrick McHardy 
385a748ee24SJiri Pirko 	dev_uc_del(lowerdev, dev->dev_addr);
386b863ceb7SPatrick McHardy 
387eb06acdcSSridhar Samudrala hash_del:
388449f4544SEric Dumazet 	macvlan_hash_del(vlan, !dev->dismantle);
389b863ceb7SPatrick McHardy 	return 0;
390b863ceb7SPatrick McHardy }
391b863ceb7SPatrick McHardy 
392ad5d20a6SPatrick McHardy static int macvlan_set_mac_address(struct net_device *dev, void *p)
393ad5d20a6SPatrick McHardy {
394ad5d20a6SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
395ad5d20a6SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
396ad5d20a6SPatrick McHardy 	struct sockaddr *addr = p;
397ad5d20a6SPatrick McHardy 	int err;
398ad5d20a6SPatrick McHardy 
399ad5d20a6SPatrick McHardy 	if (!is_valid_ether_addr(addr->sa_data))
400ad5d20a6SPatrick McHardy 		return -EADDRNOTAVAIL;
401ad5d20a6SPatrick McHardy 
402f9ac30f0SEric Biederman 	if (!(dev->flags & IFF_UP)) {
403f9ac30f0SEric Biederman 		/* Just copy in the new address */
404f9ac30f0SEric Biederman 		memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
405f9ac30f0SEric Biederman 	} else {
406f9ac30f0SEric Biederman 		/* Rehash and update the device filters */
407f9ac30f0SEric Biederman 		if (macvlan_addr_busy(vlan->port, addr->sa_data))
408f9ac30f0SEric Biederman 			return -EBUSY;
409ad5d20a6SPatrick McHardy 
410a748ee24SJiri Pirko 		err = dev_uc_add(lowerdev, addr->sa_data);
411ccffad25SJiri Pirko 		if (err)
412ad5d20a6SPatrick McHardy 			return err;
413f9ac30f0SEric Biederman 
414a748ee24SJiri Pirko 		dev_uc_del(lowerdev, dev->dev_addr);
415ad5d20a6SPatrick McHardy 
416f9ac30f0SEric Biederman 		macvlan_hash_change_addr(vlan, addr->sa_data);
417f9ac30f0SEric Biederman 	}
418ad5d20a6SPatrick McHardy 	return 0;
419ad5d20a6SPatrick McHardy }
420ad5d20a6SPatrick McHardy 
421b863ceb7SPatrick McHardy static void macvlan_change_rx_flags(struct net_device *dev, int change)
422b863ceb7SPatrick McHardy {
423b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
424b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
425b863ceb7SPatrick McHardy 
426b863ceb7SPatrick McHardy 	if (change & IFF_ALLMULTI)
427b863ceb7SPatrick McHardy 		dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
428b863ceb7SPatrick McHardy }
429b863ceb7SPatrick McHardy 
430df8ef8f3SJohn Fastabend static void macvlan_set_mac_lists(struct net_device *dev)
431b863ceb7SPatrick McHardy {
432b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
433b863ceb7SPatrick McHardy 
434cd431e73SEric Dumazet 	if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
435cd431e73SEric Dumazet 		bitmap_fill(vlan->mc_filter, MACVLAN_MC_FILTER_SZ);
436cd431e73SEric Dumazet 	} else {
437cd431e73SEric Dumazet 		struct netdev_hw_addr *ha;
438cd431e73SEric Dumazet 		DECLARE_BITMAP(filter, MACVLAN_MC_FILTER_SZ);
439cd431e73SEric Dumazet 
440cd431e73SEric Dumazet 		bitmap_zero(filter, MACVLAN_MC_FILTER_SZ);
441cd431e73SEric Dumazet 		netdev_for_each_mc_addr(ha, dev) {
4423807ff58SEric Dumazet 			__set_bit(mc_hash(vlan, ha->addr), filter);
443cd431e73SEric Dumazet 		}
444d5270430SEric Dumazet 
4453807ff58SEric Dumazet 		__set_bit(mc_hash(vlan, dev->broadcast), filter);
446d5270430SEric Dumazet 
447cd431e73SEric Dumazet 		bitmap_copy(vlan->mc_filter, filter, MACVLAN_MC_FILTER_SZ);
448cd431e73SEric Dumazet 	}
449df8ef8f3SJohn Fastabend 	dev_uc_sync(vlan->lowerdev, dev);
450b863ceb7SPatrick McHardy 	dev_mc_sync(vlan->lowerdev, dev);
451b863ceb7SPatrick McHardy }
452b863ceb7SPatrick McHardy 
453b863ceb7SPatrick McHardy static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
454b863ceb7SPatrick McHardy {
455b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
456b863ceb7SPatrick McHardy 
457b863ceb7SPatrick McHardy 	if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
458b863ceb7SPatrick McHardy 		return -EINVAL;
459b863ceb7SPatrick McHardy 	dev->mtu = new_mtu;
460b863ceb7SPatrick McHardy 	return 0;
461b863ceb7SPatrick McHardy }
462b863ceb7SPatrick McHardy 
463b863ceb7SPatrick McHardy /*
464b863ceb7SPatrick McHardy  * macvlan network devices have devices nesting below it and are a special
465b863ceb7SPatrick McHardy  * "super class" of normal network devices; split their locks off into a
466b863ceb7SPatrick McHardy  * separate class since they always nest.
467b863ceb7SPatrick McHardy  */
468b863ceb7SPatrick McHardy static struct lock_class_key macvlan_netdev_xmit_lock_key;
469cf508b12SDavid S. Miller static struct lock_class_key macvlan_netdev_addr_lock_key;
470b863ceb7SPatrick McHardy 
471b863ceb7SPatrick McHardy #define MACVLAN_FEATURES \
472b863ceb7SPatrick McHardy 	(NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
473b863ceb7SPatrick McHardy 	 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
4748d13e670SJohn Fastabend 	 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
47528d2b136SPatrick McHardy 	 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
476b863ceb7SPatrick McHardy 
477b863ceb7SPatrick McHardy #define MACVLAN_STATE_MASK \
478b863ceb7SPatrick McHardy 	((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
479b863ceb7SPatrick McHardy 
480e8a0464cSDavid S. Miller static void macvlan_set_lockdep_class_one(struct net_device *dev,
481e8a0464cSDavid S. Miller 					  struct netdev_queue *txq,
482e8a0464cSDavid S. Miller 					  void *_unused)
483c773e847SDavid S. Miller {
484c773e847SDavid S. Miller 	lockdep_set_class(&txq->_xmit_lock,
485c773e847SDavid S. Miller 			  &macvlan_netdev_xmit_lock_key);
486c773e847SDavid S. Miller }
487c773e847SDavid S. Miller 
488c773e847SDavid S. Miller static void macvlan_set_lockdep_class(struct net_device *dev)
489c773e847SDavid S. Miller {
490cf508b12SDavid S. Miller 	lockdep_set_class(&dev->addr_list_lock,
491cf508b12SDavid S. Miller 			  &macvlan_netdev_addr_lock_key);
492e8a0464cSDavid S. Miller 	netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL);
493c773e847SDavid S. Miller }
494c773e847SDavid S. Miller 
495b863ceb7SPatrick McHardy static int macvlan_init(struct net_device *dev)
496b863ceb7SPatrick McHardy {
497b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
498b863ceb7SPatrick McHardy 	const struct net_device *lowerdev = vlan->lowerdev;
499b863ceb7SPatrick McHardy 
500b863ceb7SPatrick McHardy 	dev->state		= (dev->state & ~MACVLAN_STATE_MASK) |
501b863ceb7SPatrick McHardy 				  (lowerdev->state & MACVLAN_STATE_MASK);
502b863ceb7SPatrick McHardy 	dev->features 		= lowerdev->features & MACVLAN_FEATURES;
5038ffab51bSEric Dumazet 	dev->features		|= NETIF_F_LLTX;
5048c2acc53SPatrick McHardy 	dev->gso_max_size	= lowerdev->gso_max_size;
505b863ceb7SPatrick McHardy 	dev->iflink		= lowerdev->ifindex;
506ef5c8996Ssg.tweak@gmail.com 	dev->hard_header_len	= lowerdev->hard_header_len;
507b863ceb7SPatrick McHardy 
508c773e847SDavid S. Miller 	macvlan_set_lockdep_class(dev);
509c773e847SDavid S. Miller 
5108ffab51bSEric Dumazet 	vlan->pcpu_stats = alloc_percpu(struct macvlan_pcpu_stats);
5118ffab51bSEric Dumazet 	if (!vlan->pcpu_stats)
512fccaf710SEric Dumazet 		return -ENOMEM;
513fccaf710SEric Dumazet 
514b863ceb7SPatrick McHardy 	return 0;
515b863ceb7SPatrick McHardy }
516b863ceb7SPatrick McHardy 
517fccaf710SEric Dumazet static void macvlan_uninit(struct net_device *dev)
518fccaf710SEric Dumazet {
519fccaf710SEric Dumazet 	struct macvlan_dev *vlan = netdev_priv(dev);
520d5cd9244SEric W. Biederman 	struct macvlan_port *port = vlan->port;
521fccaf710SEric Dumazet 
5228ffab51bSEric Dumazet 	free_percpu(vlan->pcpu_stats);
523d5cd9244SEric W. Biederman 
524d5cd9244SEric W. Biederman 	port->count -= 1;
525d5cd9244SEric W. Biederman 	if (!port->count)
526d5cd9244SEric W. Biederman 		macvlan_port_destroy(port->dev);
527fccaf710SEric Dumazet }
528fccaf710SEric Dumazet 
52928172739SEric Dumazet static struct rtnl_link_stats64 *macvlan_dev_get_stats64(struct net_device *dev,
53028172739SEric Dumazet 							 struct rtnl_link_stats64 *stats)
531fccaf710SEric Dumazet {
532fccaf710SEric Dumazet 	struct macvlan_dev *vlan = netdev_priv(dev);
533fccaf710SEric Dumazet 
5348ffab51bSEric Dumazet 	if (vlan->pcpu_stats) {
5358ffab51bSEric Dumazet 		struct macvlan_pcpu_stats *p;
5368ffab51bSEric Dumazet 		u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
5378ffab51bSEric Dumazet 		u32 rx_errors = 0, tx_dropped = 0;
538bc66154eSEric Dumazet 		unsigned int start;
539fccaf710SEric Dumazet 		int i;
540fccaf710SEric Dumazet 
541fccaf710SEric Dumazet 		for_each_possible_cpu(i) {
5428ffab51bSEric Dumazet 			p = per_cpu_ptr(vlan->pcpu_stats, i);
543bc66154eSEric Dumazet 			do {
544bc66154eSEric Dumazet 				start = u64_stats_fetch_begin_bh(&p->syncp);
545bc66154eSEric Dumazet 				rx_packets	= p->rx_packets;
546bc66154eSEric Dumazet 				rx_bytes	= p->rx_bytes;
547bc66154eSEric Dumazet 				rx_multicast	= p->rx_multicast;
5488ffab51bSEric Dumazet 				tx_packets	= p->tx_packets;
5498ffab51bSEric Dumazet 				tx_bytes	= p->tx_bytes;
550bc66154eSEric Dumazet 			} while (u64_stats_fetch_retry_bh(&p->syncp, start));
5518ffab51bSEric Dumazet 
5528ffab51bSEric Dumazet 			stats->rx_packets	+= rx_packets;
5538ffab51bSEric Dumazet 			stats->rx_bytes		+= rx_bytes;
5548ffab51bSEric Dumazet 			stats->multicast	+= rx_multicast;
5558ffab51bSEric Dumazet 			stats->tx_packets	+= tx_packets;
5568ffab51bSEric Dumazet 			stats->tx_bytes		+= tx_bytes;
5578ffab51bSEric Dumazet 			/* rx_errors & tx_dropped are u32, updated
5588ffab51bSEric Dumazet 			 * without syncp protection.
5598ffab51bSEric Dumazet 			 */
5608ffab51bSEric Dumazet 			rx_errors	+= p->rx_errors;
5618ffab51bSEric Dumazet 			tx_dropped	+= p->tx_dropped;
562fccaf710SEric Dumazet 		}
5638ffab51bSEric Dumazet 		stats->rx_errors	= rx_errors;
5648ffab51bSEric Dumazet 		stats->rx_dropped	= rx_errors;
5658ffab51bSEric Dumazet 		stats->tx_dropped	= tx_dropped;
566fccaf710SEric Dumazet 	}
567fccaf710SEric Dumazet 	return stats;
568fccaf710SEric Dumazet }
569fccaf710SEric Dumazet 
5708e586137SJiri Pirko static int macvlan_vlan_rx_add_vid(struct net_device *dev,
57180d5c368SPatrick McHardy 				   __be16 proto, u16 vid)
5728d13e670SJohn Fastabend {
5738d13e670SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
5748d13e670SJohn Fastabend 	struct net_device *lowerdev = vlan->lowerdev;
5758d13e670SJohn Fastabend 
57680d5c368SPatrick McHardy 	return vlan_vid_add(lowerdev, proto, vid);
5778d13e670SJohn Fastabend }
5788d13e670SJohn Fastabend 
5798e586137SJiri Pirko static int macvlan_vlan_rx_kill_vid(struct net_device *dev,
58080d5c368SPatrick McHardy 				    __be16 proto, u16 vid)
5818d13e670SJohn Fastabend {
5828d13e670SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
5838d13e670SJohn Fastabend 	struct net_device *lowerdev = vlan->lowerdev;
5848d13e670SJohn Fastabend 
58580d5c368SPatrick McHardy 	vlan_vid_del(lowerdev, proto, vid);
5868e586137SJiri Pirko 	return 0;
5878d13e670SJohn Fastabend }
5888d13e670SJohn Fastabend 
589edc7d573Sstephen hemminger static int macvlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
590df8ef8f3SJohn Fastabend 			   struct net_device *dev,
5916b6e2725Sstephen hemminger 			   const unsigned char *addr,
592df8ef8f3SJohn Fastabend 			   u16 flags)
593df8ef8f3SJohn Fastabend {
594df8ef8f3SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
595df8ef8f3SJohn Fastabend 	int err = -EINVAL;
596df8ef8f3SJohn Fastabend 
597df8ef8f3SJohn Fastabend 	if (!vlan->port->passthru)
598df8ef8f3SJohn Fastabend 		return -EOPNOTSUPP;
599df8ef8f3SJohn Fastabend 
600df8ef8f3SJohn Fastabend 	if (is_unicast_ether_addr(addr))
601df8ef8f3SJohn Fastabend 		err = dev_uc_add_excl(dev, addr);
602df8ef8f3SJohn Fastabend 	else if (is_multicast_ether_addr(addr))
603df8ef8f3SJohn Fastabend 		err = dev_mc_add_excl(dev, addr);
604df8ef8f3SJohn Fastabend 
605df8ef8f3SJohn Fastabend 	return err;
606df8ef8f3SJohn Fastabend }
607df8ef8f3SJohn Fastabend 
6081690be63SVlad Yasevich static int macvlan_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
609df8ef8f3SJohn Fastabend 			   struct net_device *dev,
6106b6e2725Sstephen hemminger 			   const unsigned char *addr)
611df8ef8f3SJohn Fastabend {
612df8ef8f3SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
613df8ef8f3SJohn Fastabend 	int err = -EINVAL;
614df8ef8f3SJohn Fastabend 
615df8ef8f3SJohn Fastabend 	if (!vlan->port->passthru)
616df8ef8f3SJohn Fastabend 		return -EOPNOTSUPP;
617df8ef8f3SJohn Fastabend 
618df8ef8f3SJohn Fastabend 	if (is_unicast_ether_addr(addr))
619df8ef8f3SJohn Fastabend 		err = dev_uc_del(dev, addr);
620df8ef8f3SJohn Fastabend 	else if (is_multicast_ether_addr(addr))
621df8ef8f3SJohn Fastabend 		err = dev_mc_del(dev, addr);
622df8ef8f3SJohn Fastabend 
623df8ef8f3SJohn Fastabend 	return err;
624df8ef8f3SJohn Fastabend }
625df8ef8f3SJohn Fastabend 
626b863ceb7SPatrick McHardy static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
627b863ceb7SPatrick McHardy 					struct ethtool_drvinfo *drvinfo)
628b863ceb7SPatrick McHardy {
6297826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "macvlan", sizeof(drvinfo->driver));
6307826d43fSJiri Pirko 	strlcpy(drvinfo->version, "0.1", sizeof(drvinfo->version));
631b863ceb7SPatrick McHardy }
632b863ceb7SPatrick McHardy 
6339edb8bb6SStephen Hemminger static int macvlan_ethtool_get_settings(struct net_device *dev,
6349edb8bb6SStephen Hemminger 					struct ethtool_cmd *cmd)
6359edb8bb6SStephen Hemminger {
6369edb8bb6SStephen Hemminger 	const struct macvlan_dev *vlan = netdev_priv(dev);
6374bc71cb9SJiri Pirko 
6384bc71cb9SJiri Pirko 	return __ethtool_get_settings(vlan->lowerdev, cmd);
6399edb8bb6SStephen Hemminger }
6409edb8bb6SStephen Hemminger 
641b863ceb7SPatrick McHardy static const struct ethtool_ops macvlan_ethtool_ops = {
642b863ceb7SPatrick McHardy 	.get_link		= ethtool_op_get_link,
6439edb8bb6SStephen Hemminger 	.get_settings		= macvlan_ethtool_get_settings,
644b863ceb7SPatrick McHardy 	.get_drvinfo		= macvlan_ethtool_get_drvinfo,
645b863ceb7SPatrick McHardy };
646b863ceb7SPatrick McHardy 
64754a30c97SStephen Hemminger static const struct net_device_ops macvlan_netdev_ops = {
64854a30c97SStephen Hemminger 	.ndo_init		= macvlan_init,
649fccaf710SEric Dumazet 	.ndo_uninit		= macvlan_uninit,
65054a30c97SStephen Hemminger 	.ndo_open		= macvlan_open,
65154a30c97SStephen Hemminger 	.ndo_stop		= macvlan_stop,
65200829823SStephen Hemminger 	.ndo_start_xmit		= macvlan_start_xmit,
65354a30c97SStephen Hemminger 	.ndo_change_mtu		= macvlan_change_mtu,
65454a30c97SStephen Hemminger 	.ndo_change_rx_flags	= macvlan_change_rx_flags,
65554a30c97SStephen Hemminger 	.ndo_set_mac_address	= macvlan_set_mac_address,
656df8ef8f3SJohn Fastabend 	.ndo_set_rx_mode	= macvlan_set_mac_lists,
657bc66154eSEric Dumazet 	.ndo_get_stats64	= macvlan_dev_get_stats64,
65854a30c97SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
6598d13e670SJohn Fastabend 	.ndo_vlan_rx_add_vid	= macvlan_vlan_rx_add_vid,
6608d13e670SJohn Fastabend 	.ndo_vlan_rx_kill_vid	= macvlan_vlan_rx_kill_vid,
661df8ef8f3SJohn Fastabend 	.ndo_fdb_add		= macvlan_fdb_add,
662df8ef8f3SJohn Fastabend 	.ndo_fdb_del		= macvlan_fdb_del,
663df8ef8f3SJohn Fastabend 	.ndo_fdb_dump		= ndo_dflt_fdb_dump,
66454a30c97SStephen Hemminger };
66554a30c97SStephen Hemminger 
6668a35747aSHerbert Xu void macvlan_common_setup(struct net_device *dev)
667b863ceb7SPatrick McHardy {
668b863ceb7SPatrick McHardy 	ether_setup(dev);
669b863ceb7SPatrick McHardy 
670550fd08cSNeil Horman 	dev->priv_flags	       &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
67187ab7f6fSVlad Yasevich 	dev->priv_flags	       |= IFF_UNICAST_FLT;
67254a30c97SStephen Hemminger 	dev->netdev_ops		= &macvlan_netdev_ops;
673b863ceb7SPatrick McHardy 	dev->destructor		= free_netdev;
6743b04dddeSStephen Hemminger 	dev->header_ops		= &macvlan_hard_header_ops,
675b863ceb7SPatrick McHardy 	dev->ethtool_ops	= &macvlan_ethtool_ops;
6768a35747aSHerbert Xu }
6778a35747aSHerbert Xu EXPORT_SYMBOL_GPL(macvlan_common_setup);
6788a35747aSHerbert Xu 
6798a35747aSHerbert Xu static void macvlan_setup(struct net_device *dev)
6808a35747aSHerbert Xu {
6818a35747aSHerbert Xu 	macvlan_common_setup(dev);
682b863ceb7SPatrick McHardy 	dev->tx_queue_len	= 0;
683b863ceb7SPatrick McHardy }
684b863ceb7SPatrick McHardy 
685b863ceb7SPatrick McHardy static int macvlan_port_create(struct net_device *dev)
686b863ceb7SPatrick McHardy {
687b863ceb7SPatrick McHardy 	struct macvlan_port *port;
688b863ceb7SPatrick McHardy 	unsigned int i;
689ab95bfe0SJiri Pirko 	int err;
690b863ceb7SPatrick McHardy 
691b863ceb7SPatrick McHardy 	if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
692b863ceb7SPatrick McHardy 		return -EINVAL;
693b863ceb7SPatrick McHardy 
694b863ceb7SPatrick McHardy 	port = kzalloc(sizeof(*port), GFP_KERNEL);
695b863ceb7SPatrick McHardy 	if (port == NULL)
696b863ceb7SPatrick McHardy 		return -ENOMEM;
697b863ceb7SPatrick McHardy 
698eb06acdcSSridhar Samudrala 	port->passthru = false;
699b863ceb7SPatrick McHardy 	port->dev = dev;
700b863ceb7SPatrick McHardy 	INIT_LIST_HEAD(&port->vlans);
701b863ceb7SPatrick McHardy 	for (i = 0; i < MACVLAN_HASH_SIZE; i++)
702b863ceb7SPatrick McHardy 		INIT_HLIST_HEAD(&port->vlan_hash[i]);
703ab95bfe0SJiri Pirko 
704a35e2c1bSJiri Pirko 	err = netdev_rx_handler_register(dev, macvlan_handle_frame, port);
705a35e2c1bSJiri Pirko 	if (err)
706ab95bfe0SJiri Pirko 		kfree(port);
707d9351561SEric Dumazet 	else
708a35e2c1bSJiri Pirko 		dev->priv_flags |= IFF_MACVLAN_PORT;
709ab95bfe0SJiri Pirko 	return err;
710b863ceb7SPatrick McHardy }
711b863ceb7SPatrick McHardy 
712b863ceb7SPatrick McHardy static void macvlan_port_destroy(struct net_device *dev)
713b863ceb7SPatrick McHardy {
714e052f7e6SEric Dumazet 	struct macvlan_port *port = macvlan_port_get_rtnl(dev);
715b863ceb7SPatrick McHardy 
716a35e2c1bSJiri Pirko 	dev->priv_flags &= ~IFF_MACVLAN_PORT;
717ab95bfe0SJiri Pirko 	netdev_rx_handler_unregister(dev);
7186e070aecSLai Jiangshan 	kfree_rcu(port, rcu);
719b863ceb7SPatrick McHardy }
720b863ceb7SPatrick McHardy 
721b863ceb7SPatrick McHardy static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
722b863ceb7SPatrick McHardy {
723b863ceb7SPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
724b863ceb7SPatrick McHardy 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
725b863ceb7SPatrick McHardy 			return -EINVAL;
726b863ceb7SPatrick McHardy 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
727b863ceb7SPatrick McHardy 			return -EADDRNOTAVAIL;
728b863ceb7SPatrick McHardy 	}
72927c0b1a8SArnd Bergmann 
73027c0b1a8SArnd Bergmann 	if (data && data[IFLA_MACVLAN_MODE]) {
73127c0b1a8SArnd Bergmann 		switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) {
73227c0b1a8SArnd Bergmann 		case MACVLAN_MODE_PRIVATE:
73327c0b1a8SArnd Bergmann 		case MACVLAN_MODE_VEPA:
73427c0b1a8SArnd Bergmann 		case MACVLAN_MODE_BRIDGE:
735eb06acdcSSridhar Samudrala 		case MACVLAN_MODE_PASSTHRU:
73627c0b1a8SArnd Bergmann 			break;
73727c0b1a8SArnd Bergmann 		default:
73827c0b1a8SArnd Bergmann 			return -EINVAL;
73927c0b1a8SArnd Bergmann 		}
74027c0b1a8SArnd Bergmann 	}
741b863ceb7SPatrick McHardy 	return 0;
742b863ceb7SPatrick McHardy }
743b863ceb7SPatrick McHardy 
744fc0663d6SArnd Bergmann int macvlan_common_newlink(struct net *src_net, struct net_device *dev,
745fc0663d6SArnd Bergmann 			   struct nlattr *tb[], struct nlattr *data[],
746fc0663d6SArnd Bergmann 			   int (*receive)(struct sk_buff *skb),
747fc0663d6SArnd Bergmann 			   int (*forward)(struct net_device *dev,
748fc0663d6SArnd Bergmann 					  struct sk_buff *skb))
749b863ceb7SPatrick McHardy {
750b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
751b863ceb7SPatrick McHardy 	struct macvlan_port *port;
752b863ceb7SPatrick McHardy 	struct net_device *lowerdev;
753b863ceb7SPatrick McHardy 	int err;
754b863ceb7SPatrick McHardy 
755b863ceb7SPatrick McHardy 	if (!tb[IFLA_LINK])
756b863ceb7SPatrick McHardy 		return -EINVAL;
757b863ceb7SPatrick McHardy 
75881adee47SEric W. Biederman 	lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
759b863ceb7SPatrick McHardy 	if (lowerdev == NULL)
760b863ceb7SPatrick McHardy 		return -ENODEV;
761b863ceb7SPatrick McHardy 
762b0832a29SEric Biederman 	/* When creating macvlans on top of other macvlans - use
763b0832a29SEric Biederman 	 * the real device as the lowerdev.
764a6ca5f1dSPatrick McHardy 	 */
765b0832a29SEric Biederman 	if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) {
766b0832a29SEric Biederman 		struct macvlan_dev *lowervlan = netdev_priv(lowerdev);
767b0832a29SEric Biederman 		lowerdev = lowervlan->lowerdev;
768b0832a29SEric Biederman 	}
769a6ca5f1dSPatrick McHardy 
770b863ceb7SPatrick McHardy 	if (!tb[IFLA_MTU])
771b863ceb7SPatrick McHardy 		dev->mtu = lowerdev->mtu;
772b863ceb7SPatrick McHardy 	else if (dev->mtu > lowerdev->mtu)
773b863ceb7SPatrick McHardy 		return -EINVAL;
774b863ceb7SPatrick McHardy 
775b863ceb7SPatrick McHardy 	if (!tb[IFLA_ADDRESS])
7767ce5d222SDanny Kukawka 		eth_hw_addr_random(dev);
777b863ceb7SPatrick McHardy 
778a35e2c1bSJiri Pirko 	if (!macvlan_port_exists(lowerdev)) {
779b863ceb7SPatrick McHardy 		err = macvlan_port_create(lowerdev);
780b863ceb7SPatrick McHardy 		if (err < 0)
781b863ceb7SPatrick McHardy 			return err;
782b863ceb7SPatrick McHardy 	}
783e052f7e6SEric Dumazet 	port = macvlan_port_get_rtnl(lowerdev);
784b863ceb7SPatrick McHardy 
785eb06acdcSSridhar Samudrala 	/* Only 1 macvlan device can be created in passthru mode */
786eb06acdcSSridhar Samudrala 	if (port->passthru)
787eb06acdcSSridhar Samudrala 		return -EINVAL;
788eb06acdcSSridhar Samudrala 
789b863ceb7SPatrick McHardy 	vlan->lowerdev = lowerdev;
790b863ceb7SPatrick McHardy 	vlan->dev      = dev;
791b863ceb7SPatrick McHardy 	vlan->port     = port;
792fc0663d6SArnd Bergmann 	vlan->receive  = receive;
793fc0663d6SArnd Bergmann 	vlan->forward  = forward;
794b863ceb7SPatrick McHardy 
79527c0b1a8SArnd Bergmann 	vlan->mode     = MACVLAN_MODE_VEPA;
79627c0b1a8SArnd Bergmann 	if (data && data[IFLA_MACVLAN_MODE])
79727c0b1a8SArnd Bergmann 		vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
79827c0b1a8SArnd Bergmann 
799df8ef8f3SJohn Fastabend 	if (data && data[IFLA_MACVLAN_FLAGS])
800df8ef8f3SJohn Fastabend 		vlan->flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
801df8ef8f3SJohn Fastabend 
802eb06acdcSSridhar Samudrala 	if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
803d5cd9244SEric W. Biederman 		if (port->count)
804eb06acdcSSridhar Samudrala 			return -EINVAL;
805eb06acdcSSridhar Samudrala 		port->passthru = true;
806eb06acdcSSridhar Samudrala 		memcpy(dev->dev_addr, lowerdev->dev_addr, ETH_ALEN);
807eb06acdcSSridhar Samudrala 	}
808eb06acdcSSridhar Samudrala 
8097cd43db7SJiri Pirko 	err = netdev_upper_dev_link(lowerdev, dev);
8107cd43db7SJiri Pirko 	if (err)
8117cd43db7SJiri Pirko 		goto destroy_port;
8127cd43db7SJiri Pirko 
813d5cd9244SEric W. Biederman 	port->count += 1;
814b863ceb7SPatrick McHardy 	err = register_netdevice(dev);
815b863ceb7SPatrick McHardy 	if (err < 0)
8167cd43db7SJiri Pirko 		goto upper_dev_unlink;
817b863ceb7SPatrick McHardy 
818*233c7df0SJiri Pirko 	list_add_tail_rcu(&vlan->list, &port->vlans);
819fc4a7489SPatrick Mullaney 	netif_stacked_transfer_operstate(lowerdev, dev);
820f16d3d57SJiri Pirko 
821b863ceb7SPatrick McHardy 	return 0;
822f16d3d57SJiri Pirko 
8237cd43db7SJiri Pirko upper_dev_unlink:
8247cd43db7SJiri Pirko 	netdev_upper_dev_unlink(lowerdev, dev);
825f16d3d57SJiri Pirko destroy_port:
826d5cd9244SEric W. Biederman 	port->count -= 1;
827d5cd9244SEric W. Biederman 	if (!port->count)
828f16d3d57SJiri Pirko 		macvlan_port_destroy(lowerdev);
829f16d3d57SJiri Pirko 
830f16d3d57SJiri Pirko 	return err;
831b863ceb7SPatrick McHardy }
832fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_common_newlink);
833b863ceb7SPatrick McHardy 
834fc0663d6SArnd Bergmann static int macvlan_newlink(struct net *src_net, struct net_device *dev,
835fc0663d6SArnd Bergmann 			   struct nlattr *tb[], struct nlattr *data[])
836fc0663d6SArnd Bergmann {
837fc0663d6SArnd Bergmann 	return macvlan_common_newlink(src_net, dev, tb, data,
838fc0663d6SArnd Bergmann 				      netif_rx,
839fc0663d6SArnd Bergmann 				      dev_forward_skb);
840fc0663d6SArnd Bergmann }
841fc0663d6SArnd Bergmann 
842fc0663d6SArnd Bergmann void macvlan_dellink(struct net_device *dev, struct list_head *head)
843b863ceb7SPatrick McHardy {
844b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
845b863ceb7SPatrick McHardy 
846*233c7df0SJiri Pirko 	list_del_rcu(&vlan->list);
84723289a37SEric Dumazet 	unregister_netdevice_queue(dev, head);
8487cd43db7SJiri Pirko 	netdev_upper_dev_unlink(vlan->lowerdev, dev);
849b863ceb7SPatrick McHardy }
850fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_dellink);
851b863ceb7SPatrick McHardy 
85227c0b1a8SArnd Bergmann static int macvlan_changelink(struct net_device *dev,
85327c0b1a8SArnd Bergmann 		struct nlattr *tb[], struct nlattr *data[])
85427c0b1a8SArnd Bergmann {
85527c0b1a8SArnd Bergmann 	struct macvlan_dev *vlan = netdev_priv(dev);
85627c0b1a8SArnd Bergmann 	if (data && data[IFLA_MACVLAN_MODE])
85727c0b1a8SArnd Bergmann 		vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
858df8ef8f3SJohn Fastabend 	if (data && data[IFLA_MACVLAN_FLAGS]) {
859df8ef8f3SJohn Fastabend 		__u16 flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
860df8ef8f3SJohn Fastabend 		bool promisc = (flags ^ vlan->flags) & MACVLAN_FLAG_NOPROMISC;
861df8ef8f3SJohn Fastabend 
862df8ef8f3SJohn Fastabend 		if (promisc && (flags & MACVLAN_FLAG_NOPROMISC))
863df8ef8f3SJohn Fastabend 			dev_set_promiscuity(vlan->lowerdev, -1);
864df8ef8f3SJohn Fastabend 		else if (promisc && !(flags & MACVLAN_FLAG_NOPROMISC))
865df8ef8f3SJohn Fastabend 			dev_set_promiscuity(vlan->lowerdev, 1);
866df8ef8f3SJohn Fastabend 		vlan->flags = flags;
867df8ef8f3SJohn Fastabend 	}
86827c0b1a8SArnd Bergmann 	return 0;
86927c0b1a8SArnd Bergmann }
87027c0b1a8SArnd Bergmann 
87127c0b1a8SArnd Bergmann static size_t macvlan_get_size(const struct net_device *dev)
87227c0b1a8SArnd Bergmann {
87301fe944fSEric Dumazet 	return (0
87401fe944fSEric Dumazet 		+ nla_total_size(4) /* IFLA_MACVLAN_MODE */
87501fe944fSEric Dumazet 		+ nla_total_size(2) /* IFLA_MACVLAN_FLAGS */
87601fe944fSEric Dumazet 		);
87727c0b1a8SArnd Bergmann }
87827c0b1a8SArnd Bergmann 
87927c0b1a8SArnd Bergmann static int macvlan_fill_info(struct sk_buff *skb,
88027c0b1a8SArnd Bergmann 				const struct net_device *dev)
88127c0b1a8SArnd Bergmann {
88227c0b1a8SArnd Bergmann 	struct macvlan_dev *vlan = netdev_priv(dev);
88327c0b1a8SArnd Bergmann 
884ead9a76cSDavid S. Miller 	if (nla_put_u32(skb, IFLA_MACVLAN_MODE, vlan->mode))
885ead9a76cSDavid S. Miller 		goto nla_put_failure;
886df8ef8f3SJohn Fastabend 	if (nla_put_u16(skb, IFLA_MACVLAN_FLAGS, vlan->flags))
887df8ef8f3SJohn Fastabend 		goto nla_put_failure;
88827c0b1a8SArnd Bergmann 	return 0;
88927c0b1a8SArnd Bergmann 
89027c0b1a8SArnd Bergmann nla_put_failure:
89127c0b1a8SArnd Bergmann 	return -EMSGSIZE;
89227c0b1a8SArnd Bergmann }
89327c0b1a8SArnd Bergmann 
89427c0b1a8SArnd Bergmann static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = {
89527c0b1a8SArnd Bergmann 	[IFLA_MACVLAN_MODE]  = { .type = NLA_U32 },
896df8ef8f3SJohn Fastabend 	[IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 },
89727c0b1a8SArnd Bergmann };
89827c0b1a8SArnd Bergmann 
899fc0663d6SArnd Bergmann int macvlan_link_register(struct rtnl_link_ops *ops)
900fc0663d6SArnd Bergmann {
901fc0663d6SArnd Bergmann 	/* common fields */
902fc0663d6SArnd Bergmann 	ops->priv_size		= sizeof(struct macvlan_dev);
903fc0663d6SArnd Bergmann 	ops->validate		= macvlan_validate;
904fc0663d6SArnd Bergmann 	ops->maxtype		= IFLA_MACVLAN_MAX;
905fc0663d6SArnd Bergmann 	ops->policy		= macvlan_policy;
906fc0663d6SArnd Bergmann 	ops->changelink		= macvlan_changelink;
907fc0663d6SArnd Bergmann 	ops->get_size		= macvlan_get_size;
908fc0663d6SArnd Bergmann 	ops->fill_info		= macvlan_fill_info;
909fc0663d6SArnd Bergmann 
910fc0663d6SArnd Bergmann 	return rtnl_link_register(ops);
911fc0663d6SArnd Bergmann };
912fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_link_register);
913fc0663d6SArnd Bergmann 
914fc0663d6SArnd Bergmann static struct rtnl_link_ops macvlan_link_ops = {
915b863ceb7SPatrick McHardy 	.kind		= "macvlan",
9168a35747aSHerbert Xu 	.setup		= macvlan_setup,
917b863ceb7SPatrick McHardy 	.newlink	= macvlan_newlink,
918b863ceb7SPatrick McHardy 	.dellink	= macvlan_dellink,
919b863ceb7SPatrick McHardy };
920b863ceb7SPatrick McHardy 
921b863ceb7SPatrick McHardy static int macvlan_device_event(struct notifier_block *unused,
922b863ceb7SPatrick McHardy 				unsigned long event, void *ptr)
923b863ceb7SPatrick McHardy {
924b863ceb7SPatrick McHardy 	struct net_device *dev = ptr;
925b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan, *next;
926b863ceb7SPatrick McHardy 	struct macvlan_port *port;
927226bd341SEric Dumazet 	LIST_HEAD(list_kill);
928b863ceb7SPatrick McHardy 
929a35e2c1bSJiri Pirko 	if (!macvlan_port_exists(dev))
930b863ceb7SPatrick McHardy 		return NOTIFY_DONE;
931b863ceb7SPatrick McHardy 
932e052f7e6SEric Dumazet 	port = macvlan_port_get_rtnl(dev);
933a35e2c1bSJiri Pirko 
934b863ceb7SPatrick McHardy 	switch (event) {
935b863ceb7SPatrick McHardy 	case NETDEV_CHANGE:
936b863ceb7SPatrick McHardy 		list_for_each_entry(vlan, &port->vlans, list)
937fc4a7489SPatrick Mullaney 			netif_stacked_transfer_operstate(vlan->lowerdev,
938fc4a7489SPatrick Mullaney 							 vlan->dev);
939b863ceb7SPatrick McHardy 		break;
940b863ceb7SPatrick McHardy 	case NETDEV_FEAT_CHANGE:
941b863ceb7SPatrick McHardy 		list_for_each_entry(vlan, &port->vlans, list) {
942b863ceb7SPatrick McHardy 			vlan->dev->features = dev->features & MACVLAN_FEATURES;
9438c2acc53SPatrick McHardy 			vlan->dev->gso_max_size = dev->gso_max_size;
944b863ceb7SPatrick McHardy 			netdev_features_change(vlan->dev);
945b863ceb7SPatrick McHardy 		}
946b863ceb7SPatrick McHardy 		break;
947b863ceb7SPatrick McHardy 	case NETDEV_UNREGISTER:
9483b27e105SDavid Lamparter 		/* twiddle thumbs on netns device moves */
9493b27e105SDavid Lamparter 		if (dev->reg_state != NETREG_UNREGISTERING)
9503b27e105SDavid Lamparter 			break;
9513b27e105SDavid Lamparter 
952b863ceb7SPatrick McHardy 		list_for_each_entry_safe(vlan, next, &port->vlans, list)
953226bd341SEric Dumazet 			vlan->dev->rtnl_link_ops->dellink(vlan->dev, &list_kill);
954226bd341SEric Dumazet 		unregister_netdevice_many(&list_kill);
955226bd341SEric Dumazet 		list_del(&list_kill);
956b863ceb7SPatrick McHardy 		break;
9571c01fe14SJiri Pirko 	case NETDEV_PRE_TYPE_CHANGE:
9581c01fe14SJiri Pirko 		/* Forbid underlaying device to change its type. */
9591c01fe14SJiri Pirko 		return NOTIFY_BAD;
960b863ceb7SPatrick McHardy 	}
961b863ceb7SPatrick McHardy 	return NOTIFY_DONE;
962b863ceb7SPatrick McHardy }
963b863ceb7SPatrick McHardy 
964b863ceb7SPatrick McHardy static struct notifier_block macvlan_notifier_block __read_mostly = {
965b863ceb7SPatrick McHardy 	.notifier_call	= macvlan_device_event,
966b863ceb7SPatrick McHardy };
967b863ceb7SPatrick McHardy 
968b863ceb7SPatrick McHardy static int __init macvlan_init_module(void)
969b863ceb7SPatrick McHardy {
970b863ceb7SPatrick McHardy 	int err;
971b863ceb7SPatrick McHardy 
972b863ceb7SPatrick McHardy 	register_netdevice_notifier(&macvlan_notifier_block);
973b863ceb7SPatrick McHardy 
974fc0663d6SArnd Bergmann 	err = macvlan_link_register(&macvlan_link_ops);
975b863ceb7SPatrick McHardy 	if (err < 0)
976b863ceb7SPatrick McHardy 		goto err1;
977b863ceb7SPatrick McHardy 	return 0;
978b863ceb7SPatrick McHardy err1:
979b863ceb7SPatrick McHardy 	unregister_netdevice_notifier(&macvlan_notifier_block);
980b863ceb7SPatrick McHardy 	return err;
981b863ceb7SPatrick McHardy }
982b863ceb7SPatrick McHardy 
983b863ceb7SPatrick McHardy static void __exit macvlan_cleanup_module(void)
984b863ceb7SPatrick McHardy {
985b863ceb7SPatrick McHardy 	rtnl_link_unregister(&macvlan_link_ops);
986b863ceb7SPatrick McHardy 	unregister_netdevice_notifier(&macvlan_notifier_block);
987b863ceb7SPatrick McHardy }
988b863ceb7SPatrick McHardy 
989b863ceb7SPatrick McHardy module_init(macvlan_init_module);
990b863ceb7SPatrick McHardy module_exit(macvlan_cleanup_module);
991b863ceb7SPatrick McHardy 
992b863ceb7SPatrick McHardy MODULE_LICENSE("GPL");
993b863ceb7SPatrick McHardy MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
994b863ceb7SPatrick McHardy MODULE_DESCRIPTION("Driver for MAC address based VLANs");
995b863ceb7SPatrick McHardy MODULE_ALIAS_RTNL_LINK("macvlan");
996