xref: /linux/drivers/net/macvlan.c (revision c674ac30c549596295eb0a5af7f4714c0b905b6f)
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 
122618e1b74SArnd Bergmann 	if (local)
1232f6a1b66SVlad Yasevich 		return dev_forward_skb(dev, skb);
124618e1b74SArnd Bergmann 
125a1e514c5SArnd Bergmann 	skb->dev = dev;
126a6700db1SJoe Perches 	if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast))
127a1e514c5SArnd Bergmann 		skb->pkt_type = PACKET_BROADCAST;
128a1e514c5SArnd Bergmann 	else
129a1e514c5SArnd Bergmann 		skb->pkt_type = PACKET_MULTICAST;
130a1e514c5SArnd Bergmann 
1312f6a1b66SVlad Yasevich 	return netif_rx(skb);
132a1e514c5SArnd Bergmann }
133a1e514c5SArnd Bergmann 
1343807ff58SEric Dumazet static u32 macvlan_hash_mix(const struct macvlan_dev *vlan)
1353807ff58SEric Dumazet {
1363807ff58SEric Dumazet 	return (u32)(((unsigned long)vlan) >> L1_CACHE_SHIFT);
1373807ff58SEric Dumazet }
1383807ff58SEric Dumazet 
1393807ff58SEric Dumazet 
1403807ff58SEric Dumazet static unsigned int mc_hash(const struct macvlan_dev *vlan,
1413807ff58SEric Dumazet 			    const unsigned char *addr)
142cd431e73SEric Dumazet {
143cd431e73SEric Dumazet 	u32 val = __get_unaligned_cpu32(addr + 2);
144cd431e73SEric Dumazet 
1453807ff58SEric Dumazet 	val ^= macvlan_hash_mix(vlan);
146cd431e73SEric Dumazet 	return hash_32(val, MACVLAN_MC_FILTER_BITS);
147cd431e73SEric Dumazet }
148cd431e73SEric Dumazet 
149b863ceb7SPatrick McHardy static void macvlan_broadcast(struct sk_buff *skb,
150618e1b74SArnd Bergmann 			      const struct macvlan_port *port,
151618e1b74SArnd Bergmann 			      struct net_device *src,
152618e1b74SArnd Bergmann 			      enum macvlan_mode mode)
153b863ceb7SPatrick McHardy {
154b863ceb7SPatrick McHardy 	const struct ethhdr *eth = eth_hdr(skb);
155b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan;
156b863ceb7SPatrick McHardy 	struct sk_buff *nskb;
157b863ceb7SPatrick McHardy 	unsigned int i;
158a1e514c5SArnd Bergmann 	int err;
1593807ff58SEric Dumazet 	unsigned int hash;
160b863ceb7SPatrick McHardy 
161efbbced3SPatrick McHardy 	if (skb->protocol == htons(ETH_P_PAUSE))
162efbbced3SPatrick McHardy 		return;
163efbbced3SPatrick McHardy 
164b863ceb7SPatrick McHardy 	for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
165b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(vlan, &port->vlan_hash[i], hlist) {
166618e1b74SArnd Bergmann 			if (vlan->dev == src || !(vlan->mode & mode))
167618e1b74SArnd Bergmann 				continue;
168618e1b74SArnd Bergmann 
1693807ff58SEric Dumazet 			hash = mc_hash(vlan, eth->h_dest);
170cd431e73SEric Dumazet 			if (!test_bit(hash, vlan->mc_filter))
171cd431e73SEric Dumazet 				continue;
172de9e8f3fSHerbert Xu 
173de9e8f3fSHerbert Xu 			err = NET_RX_DROP;
174b863ceb7SPatrick McHardy 			nskb = skb_clone(skb, GFP_ATOMIC);
175de9e8f3fSHerbert Xu 			if (likely(nskb))
176de9e8f3fSHerbert Xu 				err = macvlan_broadcast_one(
177de9e8f3fSHerbert Xu 					nskb, vlan, eth,
178618e1b74SArnd Bergmann 					mode == MACVLAN_MODE_BRIDGE);
179a1e514c5SArnd Bergmann 			macvlan_count_rx(vlan, skb->len + ETH_HLEN,
180a1e514c5SArnd Bergmann 					 err == NET_RX_SUCCESS, 1);
181b863ceb7SPatrick McHardy 		}
182b863ceb7SPatrick McHardy 	}
183b863ceb7SPatrick McHardy }
184b863ceb7SPatrick McHardy 
185b863ceb7SPatrick McHardy /* called under rcu_read_lock() from netif_receive_skb */
1868a4eb573SJiri Pirko static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb)
187b863ceb7SPatrick McHardy {
188ab95bfe0SJiri Pirko 	struct macvlan_port *port;
1898a4eb573SJiri Pirko 	struct sk_buff *skb = *pskb;
190b863ceb7SPatrick McHardy 	const struct ethhdr *eth = eth_hdr(skb);
191b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan;
192618e1b74SArnd Bergmann 	const struct macvlan_dev *src;
193b863ceb7SPatrick McHardy 	struct net_device *dev;
194ba01877fSSridhar Samudrala 	unsigned int len = 0;
195ba01877fSSridhar Samudrala 	int ret = NET_RX_DROP;
196b863ceb7SPatrick McHardy 
197a35e2c1bSJiri Pirko 	port = macvlan_port_get_rcu(skb->dev);
198b863ceb7SPatrick McHardy 	if (is_multicast_ether_addr(eth->h_dest)) {
199bc416d97SEric Dumazet 		skb = ip_check_defrag(skb, IP_DEFRAG_MACVLAN);
200bc416d97SEric Dumazet 		if (!skb)
201bc416d97SEric Dumazet 			return RX_HANDLER_CONSUMED;
2024ec7ac12SEric Dumazet 		eth = eth_hdr(skb);
203618e1b74SArnd Bergmann 		src = macvlan_hash_lookup(port, eth->h_source);
204618e1b74SArnd Bergmann 		if (!src)
205618e1b74SArnd Bergmann 			/* frame comes from an external address */
206618e1b74SArnd Bergmann 			macvlan_broadcast(skb, port, NULL,
207618e1b74SArnd Bergmann 					  MACVLAN_MODE_PRIVATE |
208618e1b74SArnd Bergmann 					  MACVLAN_MODE_VEPA    |
209eb06acdcSSridhar Samudrala 					  MACVLAN_MODE_PASSTHRU|
210618e1b74SArnd Bergmann 					  MACVLAN_MODE_BRIDGE);
211618e1b74SArnd Bergmann 		else if (src->mode == MACVLAN_MODE_VEPA)
212618e1b74SArnd Bergmann 			/* flood to everyone except source */
213618e1b74SArnd Bergmann 			macvlan_broadcast(skb, port, src->dev,
214618e1b74SArnd Bergmann 					  MACVLAN_MODE_VEPA |
215618e1b74SArnd Bergmann 					  MACVLAN_MODE_BRIDGE);
216618e1b74SArnd Bergmann 		else if (src->mode == MACVLAN_MODE_BRIDGE)
217618e1b74SArnd Bergmann 			/*
218618e1b74SArnd Bergmann 			 * flood only to VEPA ports, bridge ports
219618e1b74SArnd Bergmann 			 * already saw the frame on the way out.
220618e1b74SArnd Bergmann 			 */
221618e1b74SArnd Bergmann 			macvlan_broadcast(skb, port, src->dev,
222618e1b74SArnd Bergmann 					  MACVLAN_MODE_VEPA);
223729e72a1Sstephen hemminger 		else {
224729e72a1Sstephen hemminger 			/* forward to original port. */
225729e72a1Sstephen hemminger 			vlan = src;
226729e72a1Sstephen hemminger 			ret = macvlan_broadcast_one(skb, vlan, eth, 0);
227729e72a1Sstephen hemminger 			goto out;
228729e72a1Sstephen hemminger 		}
229729e72a1Sstephen hemminger 
2308a4eb573SJiri Pirko 		return RX_HANDLER_PASS;
231b863ceb7SPatrick McHardy 	}
232b863ceb7SPatrick McHardy 
233eb06acdcSSridhar Samudrala 	if (port->passthru)
234233c7df0SJiri Pirko 		vlan = list_first_or_null_rcu(&port->vlans,
235233c7df0SJiri Pirko 					      struct macvlan_dev, list);
236eb06acdcSSridhar Samudrala 	else
237b863ceb7SPatrick McHardy 		vlan = macvlan_hash_lookup(port, eth->h_dest);
238b863ceb7SPatrick McHardy 	if (vlan == NULL)
2398a4eb573SJiri Pirko 		return RX_HANDLER_PASS;
240b863ceb7SPatrick McHardy 
241b863ceb7SPatrick McHardy 	dev = vlan->dev;
242b863ceb7SPatrick McHardy 	if (unlikely(!(dev->flags & IFF_UP))) {
243b863ceb7SPatrick McHardy 		kfree_skb(skb);
2448a4eb573SJiri Pirko 		return RX_HANDLER_CONSUMED;
245b863ceb7SPatrick McHardy 	}
246a1e514c5SArnd Bergmann 	len = skb->len + ETH_HLEN;
247b863ceb7SPatrick McHardy 	skb = skb_share_check(skb, GFP_ATOMIC);
248a1e514c5SArnd Bergmann 	if (!skb)
249ba01877fSSridhar Samudrala 		goto out;
250b863ceb7SPatrick McHardy 
251b863ceb7SPatrick McHardy 	skb->dev = dev;
252b863ceb7SPatrick McHardy 	skb->pkt_type = PACKET_HOST;
253b863ceb7SPatrick McHardy 
2542f6a1b66SVlad Yasevich 	ret = netif_rx(skb);
255ba01877fSSridhar Samudrala 
256ba01877fSSridhar Samudrala out:
257ba01877fSSridhar Samudrala 	macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, 0);
2588a4eb573SJiri Pirko 	return RX_HANDLER_CONSUMED;
259b863ceb7SPatrick McHardy }
260b863ceb7SPatrick McHardy 
261618e1b74SArnd Bergmann static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
262618e1b74SArnd Bergmann {
263618e1b74SArnd Bergmann 	const struct macvlan_dev *vlan = netdev_priv(dev);
264618e1b74SArnd Bergmann 	const struct macvlan_port *port = vlan->port;
265618e1b74SArnd Bergmann 	const struct macvlan_dev *dest;
266618e1b74SArnd Bergmann 
267618e1b74SArnd Bergmann 	if (vlan->mode == MACVLAN_MODE_BRIDGE) {
268618e1b74SArnd Bergmann 		const struct ethhdr *eth = (void *)skb->data;
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:
28659b9997bSDavid S. Miller 	skb->dev = vlan->lowerdev;
287618e1b74SArnd Bergmann 	return dev_queue_xmit(skb);
288618e1b74SArnd Bergmann }
289618e1b74SArnd Bergmann 
2900db901bdSstephen hemminger static netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
291424efe9cSStephen Hemminger 				      struct net_device *dev)
292b863ceb7SPatrick McHardy {
293b863ceb7SPatrick McHardy 	unsigned int len = skb->len;
294b863ceb7SPatrick McHardy 	int ret;
2958ffab51bSEric Dumazet 	const struct macvlan_dev *vlan = netdev_priv(dev);
296b863ceb7SPatrick McHardy 
297a6cc0cfaSJohn Fastabend 	if (vlan->fwd_priv) {
298a6cc0cfaSJohn Fastabend 		skb->dev = vlan->lowerdev;
299f663dd9aSJason Wang 		ret = dev_queue_xmit_accel(skb, vlan->fwd_priv);
300a6cc0cfaSJohn Fastabend 	} else {
301618e1b74SArnd Bergmann 		ret = macvlan_queue_xmit(skb, dev);
302a6cc0cfaSJohn Fastabend 	}
303a6cc0cfaSJohn Fastabend 
3042d6c9ffcSEric Dumazet 	if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
305cdf3e274SLi RongQing 		struct vlan_pcpu_stats *pcpu_stats;
3062c114553SEric Dumazet 
3078ffab51bSEric Dumazet 		pcpu_stats = this_cpu_ptr(vlan->pcpu_stats);
3088ffab51bSEric Dumazet 		u64_stats_update_begin(&pcpu_stats->syncp);
3098ffab51bSEric Dumazet 		pcpu_stats->tx_packets++;
3108ffab51bSEric Dumazet 		pcpu_stats->tx_bytes += len;
3118ffab51bSEric Dumazet 		u64_stats_update_end(&pcpu_stats->syncp);
3128ffab51bSEric Dumazet 	} else {
3138ffab51bSEric Dumazet 		this_cpu_inc(vlan->pcpu_stats->tx_dropped);
3148ffab51bSEric Dumazet 	}
315cbbef5e1SPatrick McHardy 	return ret;
316b863ceb7SPatrick McHardy }
317b863ceb7SPatrick McHardy 
318b863ceb7SPatrick McHardy static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
3193b04dddeSStephen Hemminger 			       unsigned short type, const void *daddr,
3203b04dddeSStephen Hemminger 			       const void *saddr, unsigned len)
321b863ceb7SPatrick McHardy {
322b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan = netdev_priv(dev);
323b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
324b863ceb7SPatrick McHardy 
3250c4e8581SStephen Hemminger 	return dev_hard_header(skb, lowerdev, type, daddr,
326b863ceb7SPatrick McHardy 			       saddr ? : dev->dev_addr, len);
327b863ceb7SPatrick McHardy }
328b863ceb7SPatrick McHardy 
3293b04dddeSStephen Hemminger static const struct header_ops macvlan_hard_header_ops = {
3303b04dddeSStephen Hemminger 	.create  	= macvlan_hard_header,
3313b04dddeSStephen Hemminger 	.rebuild	= eth_rebuild_header,
3323b04dddeSStephen Hemminger 	.parse		= eth_header_parse,
3333b04dddeSStephen Hemminger 	.cache		= eth_header_cache,
3343b04dddeSStephen Hemminger 	.cache_update	= eth_header_cache_update,
3353b04dddeSStephen Hemminger };
3363b04dddeSStephen Hemminger 
337b13ba1b8SJason Wang static struct rtnl_link_ops macvlan_link_ops;
338b13ba1b8SJason Wang 
339b863ceb7SPatrick McHardy static int macvlan_open(struct net_device *dev)
340b863ceb7SPatrick McHardy {
341b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
342b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
343b863ceb7SPatrick McHardy 	int err;
344b863ceb7SPatrick McHardy 
345eb06acdcSSridhar Samudrala 	if (vlan->port->passthru) {
34678738141SMichael S. Tsirkin 		if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) {
34778738141SMichael S. Tsirkin 			err = dev_set_promiscuity(lowerdev, 1);
34878738141SMichael S. Tsirkin 			if (err < 0)
34978738141SMichael S. Tsirkin 				goto out;
35078738141SMichael S. Tsirkin 		}
351eb06acdcSSridhar Samudrala 		goto hash_add;
352eb06acdcSSridhar Samudrala 	}
353eb06acdcSSridhar Samudrala 
354b13ba1b8SJason Wang 	if (lowerdev->features & NETIF_F_HW_L2FW_DOFFLOAD &&
355b13ba1b8SJason Wang 	    dev->rtnl_link_ops == &macvlan_link_ops) {
356a6cc0cfaSJohn Fastabend 		vlan->fwd_priv =
357a6cc0cfaSJohn Fastabend 		      lowerdev->netdev_ops->ndo_dfwd_add_station(lowerdev, dev);
358a6cc0cfaSJohn Fastabend 
359a6cc0cfaSJohn Fastabend 		/* If we get a NULL pointer back, or if we get an error
360a6cc0cfaSJohn Fastabend 		 * then we should just fall through to the non accelerated path
361a6cc0cfaSJohn Fastabend 		 */
362a6cc0cfaSJohn Fastabend 		if (IS_ERR_OR_NULL(vlan->fwd_priv)) {
363a6cc0cfaSJohn Fastabend 			vlan->fwd_priv = NULL;
364f663dd9aSJason Wang 		} else
365a6cc0cfaSJohn Fastabend 			return 0;
366a6cc0cfaSJohn Fastabend 	}
367a6cc0cfaSJohn Fastabend 
368f9ac30f0SEric Biederman 	err = -EBUSY;
369f9ac30f0SEric Biederman 	if (macvlan_addr_busy(vlan->port, dev->dev_addr))
370f9ac30f0SEric Biederman 		goto out;
371f9ac30f0SEric Biederman 
372a748ee24SJiri Pirko 	err = dev_uc_add(lowerdev, dev->dev_addr);
373b863ceb7SPatrick McHardy 	if (err < 0)
374b89fb7daSWang Chen 		goto out;
375b89fb7daSWang Chen 	if (dev->flags & IFF_ALLMULTI) {
376b89fb7daSWang Chen 		err = dev_set_allmulti(lowerdev, 1);
377b89fb7daSWang Chen 		if (err < 0)
378b89fb7daSWang Chen 			goto del_unicast;
379b89fb7daSWang Chen 	}
380eb06acdcSSridhar Samudrala 
381eb06acdcSSridhar Samudrala hash_add:
382f9ac30f0SEric Biederman 	macvlan_hash_add(vlan);
383b863ceb7SPatrick McHardy 	return 0;
384b89fb7daSWang Chen 
385b89fb7daSWang Chen del_unicast:
386a748ee24SJiri Pirko 	dev_uc_del(lowerdev, dev->dev_addr);
387b89fb7daSWang Chen out:
388a6cc0cfaSJohn Fastabend 	if (vlan->fwd_priv) {
389a6cc0cfaSJohn Fastabend 		lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev,
390a6cc0cfaSJohn Fastabend 							   vlan->fwd_priv);
391a6cc0cfaSJohn Fastabend 		vlan->fwd_priv = NULL;
392a6cc0cfaSJohn Fastabend 	}
393b89fb7daSWang Chen 	return err;
394b863ceb7SPatrick McHardy }
395b863ceb7SPatrick McHardy 
396b863ceb7SPatrick McHardy static int macvlan_stop(struct net_device *dev)
397b863ceb7SPatrick McHardy {
398b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
399b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
400b863ceb7SPatrick McHardy 
401a6cc0cfaSJohn Fastabend 	if (vlan->fwd_priv) {
402a6cc0cfaSJohn Fastabend 		lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev,
403a6cc0cfaSJohn Fastabend 							   vlan->fwd_priv);
404a6cc0cfaSJohn Fastabend 		vlan->fwd_priv = NULL;
405a6cc0cfaSJohn Fastabend 		return 0;
406a6cc0cfaSJohn Fastabend 	}
407a6cc0cfaSJohn Fastabend 
408df8ef8f3SJohn Fastabend 	dev_uc_unsync(lowerdev, dev);
409df8ef8f3SJohn Fastabend 	dev_mc_unsync(lowerdev, dev);
410df8ef8f3SJohn Fastabend 
411eb06acdcSSridhar Samudrala 	if (vlan->port->passthru) {
412df8ef8f3SJohn Fastabend 		if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC))
413eb06acdcSSridhar Samudrala 			dev_set_promiscuity(lowerdev, -1);
414eb06acdcSSridhar Samudrala 		goto hash_del;
415eb06acdcSSridhar Samudrala 	}
416eb06acdcSSridhar Samudrala 
417b863ceb7SPatrick McHardy 	if (dev->flags & IFF_ALLMULTI)
418b863ceb7SPatrick McHardy 		dev_set_allmulti(lowerdev, -1);
419b863ceb7SPatrick McHardy 
420a748ee24SJiri Pirko 	dev_uc_del(lowerdev, dev->dev_addr);
421b863ceb7SPatrick McHardy 
422eb06acdcSSridhar Samudrala hash_del:
423449f4544SEric Dumazet 	macvlan_hash_del(vlan, !dev->dismantle);
424b863ceb7SPatrick McHardy 	return 0;
425b863ceb7SPatrick McHardy }
426b863ceb7SPatrick McHardy 
427ad5d20a6SPatrick McHardy static int macvlan_set_mac_address(struct net_device *dev, void *p)
428ad5d20a6SPatrick McHardy {
429ad5d20a6SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
430ad5d20a6SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
431ad5d20a6SPatrick McHardy 	struct sockaddr *addr = p;
432ad5d20a6SPatrick McHardy 	int err;
433ad5d20a6SPatrick McHardy 
434ad5d20a6SPatrick McHardy 	if (!is_valid_ether_addr(addr->sa_data))
435ad5d20a6SPatrick McHardy 		return -EADDRNOTAVAIL;
436ad5d20a6SPatrick McHardy 
437f9ac30f0SEric Biederman 	if (!(dev->flags & IFF_UP)) {
438f9ac30f0SEric Biederman 		/* Just copy in the new address */
439f9ac30f0SEric Biederman 		memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
440f9ac30f0SEric Biederman 	} else {
441f9ac30f0SEric Biederman 		/* Rehash and update the device filters */
442f9ac30f0SEric Biederman 		if (macvlan_addr_busy(vlan->port, addr->sa_data))
443f9ac30f0SEric Biederman 			return -EBUSY;
444ad5d20a6SPatrick McHardy 
445a748ee24SJiri Pirko 		err = dev_uc_add(lowerdev, addr->sa_data);
446ccffad25SJiri Pirko 		if (err)
447ad5d20a6SPatrick McHardy 			return err;
448f9ac30f0SEric Biederman 
449a748ee24SJiri Pirko 		dev_uc_del(lowerdev, dev->dev_addr);
450ad5d20a6SPatrick McHardy 
451f9ac30f0SEric Biederman 		macvlan_hash_change_addr(vlan, addr->sa_data);
452f9ac30f0SEric Biederman 	}
453ad5d20a6SPatrick McHardy 	return 0;
454ad5d20a6SPatrick McHardy }
455ad5d20a6SPatrick McHardy 
456b863ceb7SPatrick McHardy static void macvlan_change_rx_flags(struct net_device *dev, int change)
457b863ceb7SPatrick McHardy {
458b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
459b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
460b863ceb7SPatrick McHardy 
461bbeb0eadSPeter Christensen 	if (dev->flags & IFF_UP) {
462b863ceb7SPatrick McHardy 		if (change & IFF_ALLMULTI)
463b863ceb7SPatrick McHardy 			dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
464b863ceb7SPatrick McHardy 	}
465bbeb0eadSPeter Christensen }
466b863ceb7SPatrick McHardy 
467df8ef8f3SJohn Fastabend static void macvlan_set_mac_lists(struct net_device *dev)
468b863ceb7SPatrick McHardy {
469b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
470b863ceb7SPatrick McHardy 
471cd431e73SEric Dumazet 	if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
472cd431e73SEric Dumazet 		bitmap_fill(vlan->mc_filter, MACVLAN_MC_FILTER_SZ);
473cd431e73SEric Dumazet 	} else {
474cd431e73SEric Dumazet 		struct netdev_hw_addr *ha;
475cd431e73SEric Dumazet 		DECLARE_BITMAP(filter, MACVLAN_MC_FILTER_SZ);
476cd431e73SEric Dumazet 
477cd431e73SEric Dumazet 		bitmap_zero(filter, MACVLAN_MC_FILTER_SZ);
478cd431e73SEric Dumazet 		netdev_for_each_mc_addr(ha, dev) {
4793807ff58SEric Dumazet 			__set_bit(mc_hash(vlan, ha->addr), filter);
480cd431e73SEric Dumazet 		}
481d5270430SEric Dumazet 
4823807ff58SEric Dumazet 		__set_bit(mc_hash(vlan, dev->broadcast), filter);
483d5270430SEric Dumazet 
484cd431e73SEric Dumazet 		bitmap_copy(vlan->mc_filter, filter, MACVLAN_MC_FILTER_SZ);
485cd431e73SEric Dumazet 	}
486df8ef8f3SJohn Fastabend 	dev_uc_sync(vlan->lowerdev, dev);
487b863ceb7SPatrick McHardy 	dev_mc_sync(vlan->lowerdev, dev);
488b863ceb7SPatrick McHardy }
489b863ceb7SPatrick McHardy 
490b863ceb7SPatrick McHardy static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
491b863ceb7SPatrick McHardy {
492b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
493b863ceb7SPatrick McHardy 
494b863ceb7SPatrick McHardy 	if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
495b863ceb7SPatrick McHardy 		return -EINVAL;
496b863ceb7SPatrick McHardy 	dev->mtu = new_mtu;
497b863ceb7SPatrick McHardy 	return 0;
498b863ceb7SPatrick McHardy }
499b863ceb7SPatrick McHardy 
500b863ceb7SPatrick McHardy /*
501b863ceb7SPatrick McHardy  * macvlan network devices have devices nesting below it and are a special
502b863ceb7SPatrick McHardy  * "super class" of normal network devices; split their locks off into a
503b863ceb7SPatrick McHardy  * separate class since they always nest.
504b863ceb7SPatrick McHardy  */
505b863ceb7SPatrick McHardy static struct lock_class_key macvlan_netdev_xmit_lock_key;
506cf508b12SDavid S. Miller static struct lock_class_key macvlan_netdev_addr_lock_key;
507b863ceb7SPatrick McHardy 
5088b4703e9SVlad Yasevich #define ALWAYS_ON_FEATURES \
5098b4703e9SVlad Yasevich 	(NETIF_F_SG | NETIF_F_GEN_CSUM | NETIF_F_GSO_SOFTWARE | NETIF_F_LLTX)
5108b4703e9SVlad Yasevich 
511b863ceb7SPatrick McHardy #define MACVLAN_FEATURES \
512b863ceb7SPatrick McHardy 	(NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
513b863ceb7SPatrick McHardy 	 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
5148d13e670SJohn Fastabend 	 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
51528d2b136SPatrick McHardy 	 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
516b863ceb7SPatrick McHardy 
517b863ceb7SPatrick McHardy #define MACVLAN_STATE_MASK \
518b863ceb7SPatrick McHardy 	((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
519b863ceb7SPatrick McHardy 
520*c674ac30SVlad Yasevich static int macvlan_get_nest_level(struct net_device *dev)
521*c674ac30SVlad Yasevich {
522*c674ac30SVlad Yasevich 	return ((struct macvlan_dev *)netdev_priv(dev))->nest_level;
523*c674ac30SVlad Yasevich }
524*c674ac30SVlad Yasevich 
525e8a0464cSDavid S. Miller static void macvlan_set_lockdep_class_one(struct net_device *dev,
526e8a0464cSDavid S. Miller 					  struct netdev_queue *txq,
527e8a0464cSDavid S. Miller 					  void *_unused)
528c773e847SDavid S. Miller {
529c773e847SDavid S. Miller 	lockdep_set_class(&txq->_xmit_lock,
530c773e847SDavid S. Miller 			  &macvlan_netdev_xmit_lock_key);
531c773e847SDavid S. Miller }
532c773e847SDavid S. Miller 
533c773e847SDavid S. Miller static void macvlan_set_lockdep_class(struct net_device *dev)
534c773e847SDavid S. Miller {
535*c674ac30SVlad Yasevich 	lockdep_set_class_and_subclass(&dev->addr_list_lock,
536*c674ac30SVlad Yasevich 				       &macvlan_netdev_addr_lock_key,
537*c674ac30SVlad Yasevich 				       macvlan_get_nest_level(dev));
538e8a0464cSDavid S. Miller 	netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL);
539c773e847SDavid S. Miller }
540c773e847SDavid S. Miller 
541b863ceb7SPatrick McHardy static int macvlan_init(struct net_device *dev)
542b863ceb7SPatrick McHardy {
543b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
544b863ceb7SPatrick McHardy 	const struct net_device *lowerdev = vlan->lowerdev;
545b863ceb7SPatrick McHardy 
546b863ceb7SPatrick McHardy 	dev->state		= (dev->state & ~MACVLAN_STATE_MASK) |
547b863ceb7SPatrick McHardy 				  (lowerdev->state & MACVLAN_STATE_MASK);
548b863ceb7SPatrick McHardy 	dev->features 		= lowerdev->features & MACVLAN_FEATURES;
5498b4703e9SVlad Yasevich 	dev->features		|= ALWAYS_ON_FEATURES;
5508c2acc53SPatrick McHardy 	dev->gso_max_size	= lowerdev->gso_max_size;
551b863ceb7SPatrick McHardy 	dev->iflink		= lowerdev->ifindex;
552ef5c8996Ssg.tweak@gmail.com 	dev->hard_header_len	= lowerdev->hard_header_len;
553b863ceb7SPatrick McHardy 
554c773e847SDavid S. Miller 	macvlan_set_lockdep_class(dev);
555c773e847SDavid S. Miller 
5561c213bd2SWANG Cong 	vlan->pcpu_stats = netdev_alloc_pcpu_stats(struct vlan_pcpu_stats);
5578ffab51bSEric Dumazet 	if (!vlan->pcpu_stats)
558fccaf710SEric Dumazet 		return -ENOMEM;
559fccaf710SEric Dumazet 
560b863ceb7SPatrick McHardy 	return 0;
561b863ceb7SPatrick McHardy }
562b863ceb7SPatrick McHardy 
563fccaf710SEric Dumazet static void macvlan_uninit(struct net_device *dev)
564fccaf710SEric Dumazet {
565fccaf710SEric Dumazet 	struct macvlan_dev *vlan = netdev_priv(dev);
566d5cd9244SEric W. Biederman 	struct macvlan_port *port = vlan->port;
567fccaf710SEric Dumazet 
5688ffab51bSEric Dumazet 	free_percpu(vlan->pcpu_stats);
569d5cd9244SEric W. Biederman 
570d5cd9244SEric W. Biederman 	port->count -= 1;
571d5cd9244SEric W. Biederman 	if (!port->count)
572d5cd9244SEric W. Biederman 		macvlan_port_destroy(port->dev);
573fccaf710SEric Dumazet }
574fccaf710SEric Dumazet 
57528172739SEric Dumazet static struct rtnl_link_stats64 *macvlan_dev_get_stats64(struct net_device *dev,
57628172739SEric Dumazet 							 struct rtnl_link_stats64 *stats)
577fccaf710SEric Dumazet {
578fccaf710SEric Dumazet 	struct macvlan_dev *vlan = netdev_priv(dev);
579fccaf710SEric Dumazet 
5808ffab51bSEric Dumazet 	if (vlan->pcpu_stats) {
581cdf3e274SLi RongQing 		struct vlan_pcpu_stats *p;
5828ffab51bSEric Dumazet 		u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
5838ffab51bSEric Dumazet 		u32 rx_errors = 0, tx_dropped = 0;
584bc66154eSEric Dumazet 		unsigned int start;
585fccaf710SEric Dumazet 		int i;
586fccaf710SEric Dumazet 
587fccaf710SEric Dumazet 		for_each_possible_cpu(i) {
5888ffab51bSEric Dumazet 			p = per_cpu_ptr(vlan->pcpu_stats, i);
589bc66154eSEric Dumazet 			do {
59057a7744eSEric W. Biederman 				start = u64_stats_fetch_begin_irq(&p->syncp);
591bc66154eSEric Dumazet 				rx_packets	= p->rx_packets;
592bc66154eSEric Dumazet 				rx_bytes	= p->rx_bytes;
593bc66154eSEric Dumazet 				rx_multicast	= p->rx_multicast;
5948ffab51bSEric Dumazet 				tx_packets	= p->tx_packets;
5958ffab51bSEric Dumazet 				tx_bytes	= p->tx_bytes;
59657a7744eSEric W. Biederman 			} while (u64_stats_fetch_retry_irq(&p->syncp, start));
5978ffab51bSEric Dumazet 
5988ffab51bSEric Dumazet 			stats->rx_packets	+= rx_packets;
5998ffab51bSEric Dumazet 			stats->rx_bytes		+= rx_bytes;
6008ffab51bSEric Dumazet 			stats->multicast	+= rx_multicast;
6018ffab51bSEric Dumazet 			stats->tx_packets	+= tx_packets;
6028ffab51bSEric Dumazet 			stats->tx_bytes		+= tx_bytes;
6038ffab51bSEric Dumazet 			/* rx_errors & tx_dropped are u32, updated
6048ffab51bSEric Dumazet 			 * without syncp protection.
6058ffab51bSEric Dumazet 			 */
6068ffab51bSEric Dumazet 			rx_errors	+= p->rx_errors;
6078ffab51bSEric Dumazet 			tx_dropped	+= p->tx_dropped;
608fccaf710SEric Dumazet 		}
6098ffab51bSEric Dumazet 		stats->rx_errors	= rx_errors;
6108ffab51bSEric Dumazet 		stats->rx_dropped	= rx_errors;
6118ffab51bSEric Dumazet 		stats->tx_dropped	= tx_dropped;
612fccaf710SEric Dumazet 	}
613fccaf710SEric Dumazet 	return stats;
614fccaf710SEric Dumazet }
615fccaf710SEric Dumazet 
6168e586137SJiri Pirko static int macvlan_vlan_rx_add_vid(struct net_device *dev,
61780d5c368SPatrick McHardy 				   __be16 proto, u16 vid)
6188d13e670SJohn Fastabend {
6198d13e670SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
6208d13e670SJohn Fastabend 	struct net_device *lowerdev = vlan->lowerdev;
6218d13e670SJohn Fastabend 
62280d5c368SPatrick McHardy 	return vlan_vid_add(lowerdev, proto, vid);
6238d13e670SJohn Fastabend }
6248d13e670SJohn Fastabend 
6258e586137SJiri Pirko static int macvlan_vlan_rx_kill_vid(struct net_device *dev,
62680d5c368SPatrick McHardy 				    __be16 proto, u16 vid)
6278d13e670SJohn Fastabend {
6288d13e670SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
6298d13e670SJohn Fastabend 	struct net_device *lowerdev = vlan->lowerdev;
6308d13e670SJohn Fastabend 
63180d5c368SPatrick McHardy 	vlan_vid_del(lowerdev, proto, vid);
6328e586137SJiri Pirko 	return 0;
6338d13e670SJohn Fastabend }
6348d13e670SJohn Fastabend 
635edc7d573Sstephen hemminger static int macvlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
636df8ef8f3SJohn Fastabend 			   struct net_device *dev,
6376b6e2725Sstephen hemminger 			   const unsigned char *addr,
638df8ef8f3SJohn Fastabend 			   u16 flags)
639df8ef8f3SJohn Fastabend {
640df8ef8f3SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
641df8ef8f3SJohn Fastabend 	int err = -EINVAL;
642df8ef8f3SJohn Fastabend 
643df8ef8f3SJohn Fastabend 	if (!vlan->port->passthru)
644df8ef8f3SJohn Fastabend 		return -EOPNOTSUPP;
645df8ef8f3SJohn Fastabend 
646ab2cfbb2SThomas Richter 	if (flags & NLM_F_REPLACE)
647ab2cfbb2SThomas Richter 		return -EOPNOTSUPP;
648ab2cfbb2SThomas Richter 
649df8ef8f3SJohn Fastabend 	if (is_unicast_ether_addr(addr))
650df8ef8f3SJohn Fastabend 		err = dev_uc_add_excl(dev, addr);
651df8ef8f3SJohn Fastabend 	else if (is_multicast_ether_addr(addr))
652df8ef8f3SJohn Fastabend 		err = dev_mc_add_excl(dev, addr);
653df8ef8f3SJohn Fastabend 
654df8ef8f3SJohn Fastabend 	return err;
655df8ef8f3SJohn Fastabend }
656df8ef8f3SJohn Fastabend 
6571690be63SVlad Yasevich static int macvlan_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
658df8ef8f3SJohn Fastabend 			   struct net_device *dev,
6596b6e2725Sstephen hemminger 			   const unsigned char *addr)
660df8ef8f3SJohn Fastabend {
661df8ef8f3SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
662df8ef8f3SJohn Fastabend 	int err = -EINVAL;
663df8ef8f3SJohn Fastabend 
664df8ef8f3SJohn Fastabend 	if (!vlan->port->passthru)
665df8ef8f3SJohn Fastabend 		return -EOPNOTSUPP;
666df8ef8f3SJohn Fastabend 
667df8ef8f3SJohn Fastabend 	if (is_unicast_ether_addr(addr))
668df8ef8f3SJohn Fastabend 		err = dev_uc_del(dev, addr);
669df8ef8f3SJohn Fastabend 	else if (is_multicast_ether_addr(addr))
670df8ef8f3SJohn Fastabend 		err = dev_mc_del(dev, addr);
671df8ef8f3SJohn Fastabend 
672df8ef8f3SJohn Fastabend 	return err;
673df8ef8f3SJohn Fastabend }
674df8ef8f3SJohn Fastabend 
675b863ceb7SPatrick McHardy static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
676b863ceb7SPatrick McHardy 					struct ethtool_drvinfo *drvinfo)
677b863ceb7SPatrick McHardy {
6787826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "macvlan", sizeof(drvinfo->driver));
6797826d43fSJiri Pirko 	strlcpy(drvinfo->version, "0.1", sizeof(drvinfo->version));
680b863ceb7SPatrick McHardy }
681b863ceb7SPatrick McHardy 
6829edb8bb6SStephen Hemminger static int macvlan_ethtool_get_settings(struct net_device *dev,
6839edb8bb6SStephen Hemminger 					struct ethtool_cmd *cmd)
6849edb8bb6SStephen Hemminger {
6859edb8bb6SStephen Hemminger 	const struct macvlan_dev *vlan = netdev_priv(dev);
6864bc71cb9SJiri Pirko 
6874bc71cb9SJiri Pirko 	return __ethtool_get_settings(vlan->lowerdev, cmd);
6889edb8bb6SStephen Hemminger }
6899edb8bb6SStephen Hemminger 
6902be5c767SVlad Yasevich static netdev_features_t macvlan_fix_features(struct net_device *dev,
6912be5c767SVlad Yasevich 					      netdev_features_t features)
6922be5c767SVlad Yasevich {
6932be5c767SVlad Yasevich 	struct macvlan_dev *vlan = netdev_priv(dev);
694797f87f8SFlorian Westphal 	netdev_features_t mask;
6952be5c767SVlad Yasevich 
696797f87f8SFlorian Westphal 	features |= NETIF_F_ALL_FOR_ALL;
697797f87f8SFlorian Westphal 	features &= (vlan->set_features | ~MACVLAN_FEATURES);
698797f87f8SFlorian Westphal 	mask = features;
699797f87f8SFlorian Westphal 
700797f87f8SFlorian Westphal 	features = netdev_increment_features(vlan->lowerdev->features,
701797f87f8SFlorian Westphal 					     features,
702797f87f8SFlorian Westphal 					     mask);
7038b4703e9SVlad Yasevich 	features |= ALWAYS_ON_FEATURES;
704797f87f8SFlorian Westphal 
705797f87f8SFlorian Westphal 	return features;
7062be5c767SVlad Yasevich }
7072be5c767SVlad Yasevich 
708b863ceb7SPatrick McHardy static const struct ethtool_ops macvlan_ethtool_ops = {
709b863ceb7SPatrick McHardy 	.get_link		= ethtool_op_get_link,
7109edb8bb6SStephen Hemminger 	.get_settings		= macvlan_ethtool_get_settings,
711b863ceb7SPatrick McHardy 	.get_drvinfo		= macvlan_ethtool_get_drvinfo,
712b863ceb7SPatrick McHardy };
713b863ceb7SPatrick McHardy 
71454a30c97SStephen Hemminger static const struct net_device_ops macvlan_netdev_ops = {
71554a30c97SStephen Hemminger 	.ndo_init		= macvlan_init,
716fccaf710SEric Dumazet 	.ndo_uninit		= macvlan_uninit,
71754a30c97SStephen Hemminger 	.ndo_open		= macvlan_open,
71854a30c97SStephen Hemminger 	.ndo_stop		= macvlan_stop,
71900829823SStephen Hemminger 	.ndo_start_xmit		= macvlan_start_xmit,
72054a30c97SStephen Hemminger 	.ndo_change_mtu		= macvlan_change_mtu,
7212be5c767SVlad Yasevich 	.ndo_fix_features	= macvlan_fix_features,
72254a30c97SStephen Hemminger 	.ndo_change_rx_flags	= macvlan_change_rx_flags,
72354a30c97SStephen Hemminger 	.ndo_set_mac_address	= macvlan_set_mac_address,
724df8ef8f3SJohn Fastabend 	.ndo_set_rx_mode	= macvlan_set_mac_lists,
725bc66154eSEric Dumazet 	.ndo_get_stats64	= macvlan_dev_get_stats64,
72654a30c97SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
7278d13e670SJohn Fastabend 	.ndo_vlan_rx_add_vid	= macvlan_vlan_rx_add_vid,
7288d13e670SJohn Fastabend 	.ndo_vlan_rx_kill_vid	= macvlan_vlan_rx_kill_vid,
729df8ef8f3SJohn Fastabend 	.ndo_fdb_add		= macvlan_fdb_add,
730df8ef8f3SJohn Fastabend 	.ndo_fdb_del		= macvlan_fdb_del,
731df8ef8f3SJohn Fastabend 	.ndo_fdb_dump		= ndo_dflt_fdb_dump,
732*c674ac30SVlad Yasevich 	.ndo_get_lock_subclass  = macvlan_get_nest_level,
73354a30c97SStephen Hemminger };
73454a30c97SStephen Hemminger 
7358a35747aSHerbert Xu void macvlan_common_setup(struct net_device *dev)
736b863ceb7SPatrick McHardy {
737b863ceb7SPatrick McHardy 	ether_setup(dev);
738b863ceb7SPatrick McHardy 
739550fd08cSNeil Horman 	dev->priv_flags	       &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
74087ab7f6fSVlad Yasevich 	dev->priv_flags	       |= IFF_UNICAST_FLT;
74154a30c97SStephen Hemminger 	dev->netdev_ops		= &macvlan_netdev_ops;
742b863ceb7SPatrick McHardy 	dev->destructor		= free_netdev;
74371740129SLutz Jaenicke 	dev->header_ops		= &macvlan_hard_header_ops;
744b863ceb7SPatrick McHardy 	dev->ethtool_ops	= &macvlan_ethtool_ops;
7458a35747aSHerbert Xu }
7468a35747aSHerbert Xu EXPORT_SYMBOL_GPL(macvlan_common_setup);
7478a35747aSHerbert Xu 
7488a35747aSHerbert Xu static void macvlan_setup(struct net_device *dev)
7498a35747aSHerbert Xu {
7508a35747aSHerbert Xu 	macvlan_common_setup(dev);
751b863ceb7SPatrick McHardy 	dev->tx_queue_len	= 0;
752b863ceb7SPatrick McHardy }
753b863ceb7SPatrick McHardy 
754b863ceb7SPatrick McHardy static int macvlan_port_create(struct net_device *dev)
755b863ceb7SPatrick McHardy {
756b863ceb7SPatrick McHardy 	struct macvlan_port *port;
757b863ceb7SPatrick McHardy 	unsigned int i;
758ab95bfe0SJiri Pirko 	int err;
759b863ceb7SPatrick McHardy 
760b863ceb7SPatrick McHardy 	if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
761b863ceb7SPatrick McHardy 		return -EINVAL;
762b863ceb7SPatrick McHardy 
763b863ceb7SPatrick McHardy 	port = kzalloc(sizeof(*port), GFP_KERNEL);
764b863ceb7SPatrick McHardy 	if (port == NULL)
765b863ceb7SPatrick McHardy 		return -ENOMEM;
766b863ceb7SPatrick McHardy 
767eb06acdcSSridhar Samudrala 	port->passthru = false;
768b863ceb7SPatrick McHardy 	port->dev = dev;
769b863ceb7SPatrick McHardy 	INIT_LIST_HEAD(&port->vlans);
770b863ceb7SPatrick McHardy 	for (i = 0; i < MACVLAN_HASH_SIZE; i++)
771b863ceb7SPatrick McHardy 		INIT_HLIST_HEAD(&port->vlan_hash[i]);
772ab95bfe0SJiri Pirko 
773a35e2c1bSJiri Pirko 	err = netdev_rx_handler_register(dev, macvlan_handle_frame, port);
774a35e2c1bSJiri Pirko 	if (err)
775ab95bfe0SJiri Pirko 		kfree(port);
776d9351561SEric Dumazet 	else
777a35e2c1bSJiri Pirko 		dev->priv_flags |= IFF_MACVLAN_PORT;
778ab95bfe0SJiri Pirko 	return err;
779b863ceb7SPatrick McHardy }
780b863ceb7SPatrick McHardy 
781b863ceb7SPatrick McHardy static void macvlan_port_destroy(struct net_device *dev)
782b863ceb7SPatrick McHardy {
783e052f7e6SEric Dumazet 	struct macvlan_port *port = macvlan_port_get_rtnl(dev);
784b863ceb7SPatrick McHardy 
785a35e2c1bSJiri Pirko 	dev->priv_flags &= ~IFF_MACVLAN_PORT;
786ab95bfe0SJiri Pirko 	netdev_rx_handler_unregister(dev);
7876e070aecSLai Jiangshan 	kfree_rcu(port, rcu);
788b863ceb7SPatrick McHardy }
789b863ceb7SPatrick McHardy 
790b863ceb7SPatrick McHardy static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
791b863ceb7SPatrick McHardy {
792b863ceb7SPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
793b863ceb7SPatrick McHardy 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
794b863ceb7SPatrick McHardy 			return -EINVAL;
795b863ceb7SPatrick McHardy 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
796b863ceb7SPatrick McHardy 			return -EADDRNOTAVAIL;
797b863ceb7SPatrick McHardy 	}
79827c0b1a8SArnd Bergmann 
79915127478SMichael S. Tsirkin 	if (data && data[IFLA_MACVLAN_FLAGS] &&
80015127478SMichael S. Tsirkin 	    nla_get_u16(data[IFLA_MACVLAN_FLAGS]) & ~MACVLAN_FLAG_NOPROMISC)
80115127478SMichael S. Tsirkin 		return -EINVAL;
80215127478SMichael S. Tsirkin 
80327c0b1a8SArnd Bergmann 	if (data && data[IFLA_MACVLAN_MODE]) {
80427c0b1a8SArnd Bergmann 		switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) {
80527c0b1a8SArnd Bergmann 		case MACVLAN_MODE_PRIVATE:
80627c0b1a8SArnd Bergmann 		case MACVLAN_MODE_VEPA:
80727c0b1a8SArnd Bergmann 		case MACVLAN_MODE_BRIDGE:
808eb06acdcSSridhar Samudrala 		case MACVLAN_MODE_PASSTHRU:
80927c0b1a8SArnd Bergmann 			break;
81027c0b1a8SArnd Bergmann 		default:
81127c0b1a8SArnd Bergmann 			return -EINVAL;
81227c0b1a8SArnd Bergmann 		}
81327c0b1a8SArnd Bergmann 	}
814b863ceb7SPatrick McHardy 	return 0;
815b863ceb7SPatrick McHardy }
816b863ceb7SPatrick McHardy 
817fc0663d6SArnd Bergmann int macvlan_common_newlink(struct net *src_net, struct net_device *dev,
8182f6a1b66SVlad Yasevich 			   struct nlattr *tb[], struct nlattr *data[])
819b863ceb7SPatrick McHardy {
820b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
821b863ceb7SPatrick McHardy 	struct macvlan_port *port;
822b863ceb7SPatrick McHardy 	struct net_device *lowerdev;
823b863ceb7SPatrick McHardy 	int err;
824b863ceb7SPatrick McHardy 
825b863ceb7SPatrick McHardy 	if (!tb[IFLA_LINK])
826b863ceb7SPatrick McHardy 		return -EINVAL;
827b863ceb7SPatrick McHardy 
82881adee47SEric W. Biederman 	lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
829b863ceb7SPatrick McHardy 	if (lowerdev == NULL)
830b863ceb7SPatrick McHardy 		return -ENODEV;
831b863ceb7SPatrick McHardy 
832d70f2cf5SKevin Wallace 	/* When creating macvlans or macvtaps on top of other macvlans - use
833b0832a29SEric Biederman 	 * the real device as the lowerdev.
834a6ca5f1dSPatrick McHardy 	 */
835d70f2cf5SKevin Wallace 	if (netif_is_macvlan(lowerdev))
836d70f2cf5SKevin Wallace 		lowerdev = macvlan_dev_real_dev(lowerdev);
837a6ca5f1dSPatrick McHardy 
838b863ceb7SPatrick McHardy 	if (!tb[IFLA_MTU])
839b863ceb7SPatrick McHardy 		dev->mtu = lowerdev->mtu;
840b863ceb7SPatrick McHardy 	else if (dev->mtu > lowerdev->mtu)
841b863ceb7SPatrick McHardy 		return -EINVAL;
842b863ceb7SPatrick McHardy 
843b863ceb7SPatrick McHardy 	if (!tb[IFLA_ADDRESS])
8447ce5d222SDanny Kukawka 		eth_hw_addr_random(dev);
845b863ceb7SPatrick McHardy 
846a35e2c1bSJiri Pirko 	if (!macvlan_port_exists(lowerdev)) {
847b863ceb7SPatrick McHardy 		err = macvlan_port_create(lowerdev);
848b863ceb7SPatrick McHardy 		if (err < 0)
849b863ceb7SPatrick McHardy 			return err;
850b863ceb7SPatrick McHardy 	}
851e052f7e6SEric Dumazet 	port = macvlan_port_get_rtnl(lowerdev);
852b863ceb7SPatrick McHardy 
853eb06acdcSSridhar Samudrala 	/* Only 1 macvlan device can be created in passthru mode */
854eb06acdcSSridhar Samudrala 	if (port->passthru)
855eb06acdcSSridhar Samudrala 		return -EINVAL;
856eb06acdcSSridhar Samudrala 
857b863ceb7SPatrick McHardy 	vlan->lowerdev = lowerdev;
858b863ceb7SPatrick McHardy 	vlan->dev      = dev;
859b863ceb7SPatrick McHardy 	vlan->port     = port;
8602be5c767SVlad Yasevich 	vlan->set_features = MACVLAN_FEATURES;
861*c674ac30SVlad Yasevich 	vlan->nest_level = dev_get_nest_level(lowerdev, netif_is_macvlan) + 1;
862b863ceb7SPatrick McHardy 
86327c0b1a8SArnd Bergmann 	vlan->mode     = MACVLAN_MODE_VEPA;
86427c0b1a8SArnd Bergmann 	if (data && data[IFLA_MACVLAN_MODE])
86527c0b1a8SArnd Bergmann 		vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
86627c0b1a8SArnd Bergmann 
867df8ef8f3SJohn Fastabend 	if (data && data[IFLA_MACVLAN_FLAGS])
868df8ef8f3SJohn Fastabend 		vlan->flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
869df8ef8f3SJohn Fastabend 
870eb06acdcSSridhar Samudrala 	if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
871d5cd9244SEric W. Biederman 		if (port->count)
872eb06acdcSSridhar Samudrala 			return -EINVAL;
873eb06acdcSSridhar Samudrala 		port->passthru = true;
8748b98604eSBjørn Mork 		eth_hw_addr_inherit(dev, lowerdev);
875eb06acdcSSridhar Samudrala 	}
876eb06acdcSSridhar Samudrala 
87747d4ab91SJohn Fastabend 	port->count += 1;
87847d4ab91SJohn Fastabend 	err = register_netdevice(dev);
87947d4ab91SJohn Fastabend 	if (err < 0)
88047d4ab91SJohn Fastabend 		goto destroy_port;
88147d4ab91SJohn Fastabend 
882a6cc0cfaSJohn Fastabend 	dev->priv_flags |= IFF_MACVLAN;
8837cd43db7SJiri Pirko 	err = netdev_upper_dev_link(lowerdev, dev);
8847cd43db7SJiri Pirko 	if (err)
885da37705cSCong Wang 		goto unregister_netdev;
886b863ceb7SPatrick McHardy 
887233c7df0SJiri Pirko 	list_add_tail_rcu(&vlan->list, &port->vlans);
888fc4a7489SPatrick Mullaney 	netif_stacked_transfer_operstate(lowerdev, dev);
889f16d3d57SJiri Pirko 
890b863ceb7SPatrick McHardy 	return 0;
891f16d3d57SJiri Pirko 
892da37705cSCong Wang unregister_netdev:
893da37705cSCong Wang 	unregister_netdevice(dev);
894f16d3d57SJiri Pirko destroy_port:
895d5cd9244SEric W. Biederman 	port->count -= 1;
896d5cd9244SEric W. Biederman 	if (!port->count)
897f16d3d57SJiri Pirko 		macvlan_port_destroy(lowerdev);
898f16d3d57SJiri Pirko 
899f16d3d57SJiri Pirko 	return err;
900b863ceb7SPatrick McHardy }
901fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_common_newlink);
902b863ceb7SPatrick McHardy 
903fc0663d6SArnd Bergmann static int macvlan_newlink(struct net *src_net, struct net_device *dev,
904fc0663d6SArnd Bergmann 			   struct nlattr *tb[], struct nlattr *data[])
905fc0663d6SArnd Bergmann {
9062f6a1b66SVlad Yasevich 	return macvlan_common_newlink(src_net, dev, tb, data);
907fc0663d6SArnd Bergmann }
908fc0663d6SArnd Bergmann 
909fc0663d6SArnd Bergmann void macvlan_dellink(struct net_device *dev, struct list_head *head)
910b863ceb7SPatrick McHardy {
911b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
912b863ceb7SPatrick McHardy 
913233c7df0SJiri Pirko 	list_del_rcu(&vlan->list);
91423289a37SEric Dumazet 	unregister_netdevice_queue(dev, head);
9157cd43db7SJiri Pirko 	netdev_upper_dev_unlink(vlan->lowerdev, dev);
916b863ceb7SPatrick McHardy }
917fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_dellink);
918b863ceb7SPatrick McHardy 
91927c0b1a8SArnd Bergmann static int macvlan_changelink(struct net_device *dev,
92027c0b1a8SArnd Bergmann 		struct nlattr *tb[], struct nlattr *data[])
92127c0b1a8SArnd Bergmann {
92227c0b1a8SArnd Bergmann 	struct macvlan_dev *vlan = netdev_priv(dev);
923266e8347SMichael S. Tsirkin 	enum macvlan_mode mode;
924266e8347SMichael S. Tsirkin 	bool set_mode = false;
925266e8347SMichael S. Tsirkin 
926266e8347SMichael S. Tsirkin 	/* Validate mode, but don't set yet: setting flags may fail. */
927266e8347SMichael S. Tsirkin 	if (data && data[IFLA_MACVLAN_MODE]) {
928266e8347SMichael S. Tsirkin 		set_mode = true;
929266e8347SMichael S. Tsirkin 		mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
930266e8347SMichael S. Tsirkin 		/* Passthrough mode can't be set or cleared dynamically */
931266e8347SMichael S. Tsirkin 		if ((mode == MACVLAN_MODE_PASSTHRU) !=
932266e8347SMichael S. Tsirkin 		    (vlan->mode == MACVLAN_MODE_PASSTHRU))
933266e8347SMichael S. Tsirkin 			return -EINVAL;
934266e8347SMichael S. Tsirkin 	}
93599ffc3e7SMichael S. Tsirkin 
936df8ef8f3SJohn Fastabend 	if (data && data[IFLA_MACVLAN_FLAGS]) {
937df8ef8f3SJohn Fastabend 		__u16 flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
938df8ef8f3SJohn Fastabend 		bool promisc = (flags ^ vlan->flags) & MACVLAN_FLAG_NOPROMISC;
93999ffc3e7SMichael S. Tsirkin 		if (vlan->port->passthru && promisc) {
94099ffc3e7SMichael S. Tsirkin 			int err;
941df8ef8f3SJohn Fastabend 
94299ffc3e7SMichael S. Tsirkin 			if (flags & MACVLAN_FLAG_NOPROMISC)
94399ffc3e7SMichael S. Tsirkin 				err = dev_set_promiscuity(vlan->lowerdev, -1);
94499ffc3e7SMichael S. Tsirkin 			else
94599ffc3e7SMichael S. Tsirkin 				err = dev_set_promiscuity(vlan->lowerdev, 1);
94699ffc3e7SMichael S. Tsirkin 			if (err < 0)
94799ffc3e7SMichael S. Tsirkin 				return err;
94899ffc3e7SMichael S. Tsirkin 		}
949df8ef8f3SJohn Fastabend 		vlan->flags = flags;
950df8ef8f3SJohn Fastabend 	}
951266e8347SMichael S. Tsirkin 	if (set_mode)
952266e8347SMichael S. Tsirkin 		vlan->mode = mode;
95327c0b1a8SArnd Bergmann 	return 0;
95427c0b1a8SArnd Bergmann }
95527c0b1a8SArnd Bergmann 
95627c0b1a8SArnd Bergmann static size_t macvlan_get_size(const struct net_device *dev)
95727c0b1a8SArnd Bergmann {
95801fe944fSEric Dumazet 	return (0
95901fe944fSEric Dumazet 		+ nla_total_size(4) /* IFLA_MACVLAN_MODE */
96001fe944fSEric Dumazet 		+ nla_total_size(2) /* IFLA_MACVLAN_FLAGS */
96101fe944fSEric Dumazet 		);
96227c0b1a8SArnd Bergmann }
96327c0b1a8SArnd Bergmann 
96427c0b1a8SArnd Bergmann static int macvlan_fill_info(struct sk_buff *skb,
96527c0b1a8SArnd Bergmann 				const struct net_device *dev)
96627c0b1a8SArnd Bergmann {
96727c0b1a8SArnd Bergmann 	struct macvlan_dev *vlan = netdev_priv(dev);
96827c0b1a8SArnd Bergmann 
969ead9a76cSDavid S. Miller 	if (nla_put_u32(skb, IFLA_MACVLAN_MODE, vlan->mode))
970ead9a76cSDavid S. Miller 		goto nla_put_failure;
971df8ef8f3SJohn Fastabend 	if (nla_put_u16(skb, IFLA_MACVLAN_FLAGS, vlan->flags))
972df8ef8f3SJohn Fastabend 		goto nla_put_failure;
97327c0b1a8SArnd Bergmann 	return 0;
97427c0b1a8SArnd Bergmann 
97527c0b1a8SArnd Bergmann nla_put_failure:
97627c0b1a8SArnd Bergmann 	return -EMSGSIZE;
97727c0b1a8SArnd Bergmann }
97827c0b1a8SArnd Bergmann 
97927c0b1a8SArnd Bergmann static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = {
98027c0b1a8SArnd Bergmann 	[IFLA_MACVLAN_MODE]  = { .type = NLA_U32 },
981df8ef8f3SJohn Fastabend 	[IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 },
98227c0b1a8SArnd Bergmann };
98327c0b1a8SArnd Bergmann 
984fc0663d6SArnd Bergmann int macvlan_link_register(struct rtnl_link_ops *ops)
985fc0663d6SArnd Bergmann {
986fc0663d6SArnd Bergmann 	/* common fields */
987fc0663d6SArnd Bergmann 	ops->priv_size		= sizeof(struct macvlan_dev);
988fc0663d6SArnd Bergmann 	ops->validate		= macvlan_validate;
989fc0663d6SArnd Bergmann 	ops->maxtype		= IFLA_MACVLAN_MAX;
990fc0663d6SArnd Bergmann 	ops->policy		= macvlan_policy;
991fc0663d6SArnd Bergmann 	ops->changelink		= macvlan_changelink;
992fc0663d6SArnd Bergmann 	ops->get_size		= macvlan_get_size;
993fc0663d6SArnd Bergmann 	ops->fill_info		= macvlan_fill_info;
994fc0663d6SArnd Bergmann 
995fc0663d6SArnd Bergmann 	return rtnl_link_register(ops);
996fc0663d6SArnd Bergmann };
997fc0663d6SArnd Bergmann EXPORT_SYMBOL_GPL(macvlan_link_register);
998fc0663d6SArnd Bergmann 
999fc0663d6SArnd Bergmann static struct rtnl_link_ops macvlan_link_ops = {
1000b863ceb7SPatrick McHardy 	.kind		= "macvlan",
10018a35747aSHerbert Xu 	.setup		= macvlan_setup,
1002b863ceb7SPatrick McHardy 	.newlink	= macvlan_newlink,
1003b863ceb7SPatrick McHardy 	.dellink	= macvlan_dellink,
1004b863ceb7SPatrick McHardy };
1005b863ceb7SPatrick McHardy 
1006b863ceb7SPatrick McHardy static int macvlan_device_event(struct notifier_block *unused,
1007b863ceb7SPatrick McHardy 				unsigned long event, void *ptr)
1008b863ceb7SPatrick McHardy {
1009351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1010b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan, *next;
1011b863ceb7SPatrick McHardy 	struct macvlan_port *port;
1012226bd341SEric Dumazet 	LIST_HEAD(list_kill);
1013b863ceb7SPatrick McHardy 
1014a35e2c1bSJiri Pirko 	if (!macvlan_port_exists(dev))
1015b863ceb7SPatrick McHardy 		return NOTIFY_DONE;
1016b863ceb7SPatrick McHardy 
1017e052f7e6SEric Dumazet 	port = macvlan_port_get_rtnl(dev);
1018a35e2c1bSJiri Pirko 
1019b863ceb7SPatrick McHardy 	switch (event) {
1020b863ceb7SPatrick McHardy 	case NETDEV_CHANGE:
1021b863ceb7SPatrick McHardy 		list_for_each_entry(vlan, &port->vlans, list)
1022fc4a7489SPatrick Mullaney 			netif_stacked_transfer_operstate(vlan->lowerdev,
1023fc4a7489SPatrick Mullaney 							 vlan->dev);
1024b863ceb7SPatrick McHardy 		break;
1025b863ceb7SPatrick McHardy 	case NETDEV_FEAT_CHANGE:
1026b863ceb7SPatrick McHardy 		list_for_each_entry(vlan, &port->vlans, list) {
10278c2acc53SPatrick McHardy 			vlan->dev->gso_max_size = dev->gso_max_size;
1028797f87f8SFlorian Westphal 			netdev_update_features(vlan->dev);
1029b863ceb7SPatrick McHardy 		}
1030b863ceb7SPatrick McHardy 		break;
1031b863ceb7SPatrick McHardy 	case NETDEV_UNREGISTER:
10323b27e105SDavid Lamparter 		/* twiddle thumbs on netns device moves */
10333b27e105SDavid Lamparter 		if (dev->reg_state != NETREG_UNREGISTERING)
10343b27e105SDavid Lamparter 			break;
10353b27e105SDavid Lamparter 
1036b863ceb7SPatrick McHardy 		list_for_each_entry_safe(vlan, next, &port->vlans, list)
1037226bd341SEric Dumazet 			vlan->dev->rtnl_link_ops->dellink(vlan->dev, &list_kill);
1038226bd341SEric Dumazet 		unregister_netdevice_many(&list_kill);
1039226bd341SEric Dumazet 		list_del(&list_kill);
1040b863ceb7SPatrick McHardy 		break;
10411c01fe14SJiri Pirko 	case NETDEV_PRE_TYPE_CHANGE:
10421c01fe14SJiri Pirko 		/* Forbid underlaying device to change its type. */
10431c01fe14SJiri Pirko 		return NOTIFY_BAD;
1044b863ceb7SPatrick McHardy 	}
1045b863ceb7SPatrick McHardy 	return NOTIFY_DONE;
1046b863ceb7SPatrick McHardy }
1047b863ceb7SPatrick McHardy 
1048b863ceb7SPatrick McHardy static struct notifier_block macvlan_notifier_block __read_mostly = {
1049b863ceb7SPatrick McHardy 	.notifier_call	= macvlan_device_event,
1050b863ceb7SPatrick McHardy };
1051b863ceb7SPatrick McHardy 
1052b863ceb7SPatrick McHardy static int __init macvlan_init_module(void)
1053b863ceb7SPatrick McHardy {
1054b863ceb7SPatrick McHardy 	int err;
1055b863ceb7SPatrick McHardy 
1056b863ceb7SPatrick McHardy 	register_netdevice_notifier(&macvlan_notifier_block);
1057b863ceb7SPatrick McHardy 
1058fc0663d6SArnd Bergmann 	err = macvlan_link_register(&macvlan_link_ops);
1059b863ceb7SPatrick McHardy 	if (err < 0)
1060b863ceb7SPatrick McHardy 		goto err1;
1061b863ceb7SPatrick McHardy 	return 0;
1062b863ceb7SPatrick McHardy err1:
1063b863ceb7SPatrick McHardy 	unregister_netdevice_notifier(&macvlan_notifier_block);
1064b863ceb7SPatrick McHardy 	return err;
1065b863ceb7SPatrick McHardy }
1066b863ceb7SPatrick McHardy 
1067b863ceb7SPatrick McHardy static void __exit macvlan_cleanup_module(void)
1068b863ceb7SPatrick McHardy {
1069b863ceb7SPatrick McHardy 	rtnl_link_unregister(&macvlan_link_ops);
1070b863ceb7SPatrick McHardy 	unregister_netdevice_notifier(&macvlan_notifier_block);
1071b863ceb7SPatrick McHardy }
1072b863ceb7SPatrick McHardy 
1073b863ceb7SPatrick McHardy module_init(macvlan_init_module);
1074b863ceb7SPatrick McHardy module_exit(macvlan_cleanup_module);
1075b863ceb7SPatrick McHardy 
1076b863ceb7SPatrick McHardy MODULE_LICENSE("GPL");
1077b863ceb7SPatrick McHardy MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
1078b863ceb7SPatrick McHardy MODULE_DESCRIPTION("Driver for MAC address based VLANs");
1079b863ceb7SPatrick McHardy MODULE_ALIAS_RTNL_LINK("macvlan");
1080