xref: /linux/drivers/net/macvlan.c (revision 8b4703e9bd1172a5f8244276ebb94302e6153e26)
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;
26612a2856bSDaniel Lezcano 	__u8 ip_summed = skb->ip_summed;
267618e1b74SArnd Bergmann 
268618e1b74SArnd Bergmann 	if (vlan->mode == MACVLAN_MODE_BRIDGE) {
269618e1b74SArnd Bergmann 		const struct ethhdr *eth = (void *)skb->data;
27012a2856bSDaniel Lezcano 		skb->ip_summed = CHECKSUM_UNNECESSARY;
271618e1b74SArnd Bergmann 
272618e1b74SArnd Bergmann 		/* send to other bridge ports directly */
273618e1b74SArnd Bergmann 		if (is_multicast_ether_addr(eth->h_dest)) {
274618e1b74SArnd Bergmann 			macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE);
275618e1b74SArnd Bergmann 			goto xmit_world;
276618e1b74SArnd Bergmann 		}
277618e1b74SArnd Bergmann 
278618e1b74SArnd Bergmann 		dest = macvlan_hash_lookup(port, eth->h_dest);
279618e1b74SArnd Bergmann 		if (dest && dest->mode == MACVLAN_MODE_BRIDGE) {
280a37dd333SDavid Ward 			/* send to lowerdev first for its network taps */
281cb2d0f3eSDavid Ward 			dev_forward_skb(vlan->lowerdev, skb);
282618e1b74SArnd Bergmann 
283618e1b74SArnd Bergmann 			return NET_XMIT_SUCCESS;
284618e1b74SArnd Bergmann 		}
285618e1b74SArnd Bergmann 	}
286618e1b74SArnd Bergmann 
287618e1b74SArnd Bergmann xmit_world:
28812a2856bSDaniel Lezcano 	skb->ip_summed = ip_summed;
28959b9997bSDavid S. Miller 	skb->dev = vlan->lowerdev;
290618e1b74SArnd Bergmann 	return dev_queue_xmit(skb);
291618e1b74SArnd Bergmann }
292618e1b74SArnd Bergmann 
2930db901bdSstephen hemminger static netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
294424efe9cSStephen Hemminger 				      struct net_device *dev)
295b863ceb7SPatrick McHardy {
296b863ceb7SPatrick McHardy 	unsigned int len = skb->len;
297b863ceb7SPatrick McHardy 	int ret;
2988ffab51bSEric Dumazet 	const struct macvlan_dev *vlan = netdev_priv(dev);
299b863ceb7SPatrick McHardy 
300a6cc0cfaSJohn Fastabend 	if (vlan->fwd_priv) {
301a6cc0cfaSJohn Fastabend 		skb->dev = vlan->lowerdev;
302f663dd9aSJason Wang 		ret = dev_queue_xmit_accel(skb, vlan->fwd_priv);
303a6cc0cfaSJohn Fastabend 	} else {
304618e1b74SArnd Bergmann 		ret = macvlan_queue_xmit(skb, dev);
305a6cc0cfaSJohn Fastabend 	}
306a6cc0cfaSJohn Fastabend 
3072d6c9ffcSEric Dumazet 	if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
308cdf3e274SLi RongQing 		struct vlan_pcpu_stats *pcpu_stats;
3092c114553SEric Dumazet 
3108ffab51bSEric Dumazet 		pcpu_stats = this_cpu_ptr(vlan->pcpu_stats);
3118ffab51bSEric Dumazet 		u64_stats_update_begin(&pcpu_stats->syncp);
3128ffab51bSEric Dumazet 		pcpu_stats->tx_packets++;
3138ffab51bSEric Dumazet 		pcpu_stats->tx_bytes += len;
3148ffab51bSEric Dumazet 		u64_stats_update_end(&pcpu_stats->syncp);
3158ffab51bSEric Dumazet 	} else {
3168ffab51bSEric Dumazet 		this_cpu_inc(vlan->pcpu_stats->tx_dropped);
3178ffab51bSEric Dumazet 	}
318cbbef5e1SPatrick McHardy 	return ret;
319b863ceb7SPatrick McHardy }
320b863ceb7SPatrick McHardy 
321b863ceb7SPatrick McHardy static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
3223b04dddeSStephen Hemminger 			       unsigned short type, const void *daddr,
3233b04dddeSStephen Hemminger 			       const void *saddr, unsigned len)
324b863ceb7SPatrick McHardy {
325b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan = netdev_priv(dev);
326b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
327b863ceb7SPatrick McHardy 
3280c4e8581SStephen Hemminger 	return dev_hard_header(skb, lowerdev, type, daddr,
329b863ceb7SPatrick McHardy 			       saddr ? : dev->dev_addr, len);
330b863ceb7SPatrick McHardy }
331b863ceb7SPatrick McHardy 
3323b04dddeSStephen Hemminger static const struct header_ops macvlan_hard_header_ops = {
3333b04dddeSStephen Hemminger 	.create  	= macvlan_hard_header,
3343b04dddeSStephen Hemminger 	.rebuild	= eth_rebuild_header,
3353b04dddeSStephen Hemminger 	.parse		= eth_header_parse,
3363b04dddeSStephen Hemminger 	.cache		= eth_header_cache,
3373b04dddeSStephen Hemminger 	.cache_update	= eth_header_cache_update,
3383b04dddeSStephen Hemminger };
3393b04dddeSStephen Hemminger 
340b13ba1b8SJason Wang static struct rtnl_link_ops macvlan_link_ops;
341b13ba1b8SJason Wang 
342b863ceb7SPatrick McHardy static int macvlan_open(struct net_device *dev)
343b863ceb7SPatrick McHardy {
344b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
345b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
346b863ceb7SPatrick McHardy 	int err;
347b863ceb7SPatrick McHardy 
348eb06acdcSSridhar Samudrala 	if (vlan->port->passthru) {
34978738141SMichael S. Tsirkin 		if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) {
35078738141SMichael S. Tsirkin 			err = dev_set_promiscuity(lowerdev, 1);
35178738141SMichael S. Tsirkin 			if (err < 0)
35278738141SMichael S. Tsirkin 				goto out;
35378738141SMichael S. Tsirkin 		}
354eb06acdcSSridhar Samudrala 		goto hash_add;
355eb06acdcSSridhar Samudrala 	}
356eb06acdcSSridhar Samudrala 
357b13ba1b8SJason Wang 	if (lowerdev->features & NETIF_F_HW_L2FW_DOFFLOAD &&
358b13ba1b8SJason Wang 	    dev->rtnl_link_ops == &macvlan_link_ops) {
359a6cc0cfaSJohn Fastabend 		vlan->fwd_priv =
360a6cc0cfaSJohn Fastabend 		      lowerdev->netdev_ops->ndo_dfwd_add_station(lowerdev, dev);
361a6cc0cfaSJohn Fastabend 
362a6cc0cfaSJohn Fastabend 		/* If we get a NULL pointer back, or if we get an error
363a6cc0cfaSJohn Fastabend 		 * then we should just fall through to the non accelerated path
364a6cc0cfaSJohn Fastabend 		 */
365a6cc0cfaSJohn Fastabend 		if (IS_ERR_OR_NULL(vlan->fwd_priv)) {
366a6cc0cfaSJohn Fastabend 			vlan->fwd_priv = NULL;
367f663dd9aSJason Wang 		} else
368a6cc0cfaSJohn Fastabend 			return 0;
369a6cc0cfaSJohn Fastabend 	}
370a6cc0cfaSJohn Fastabend 
371f9ac30f0SEric Biederman 	err = -EBUSY;
372f9ac30f0SEric Biederman 	if (macvlan_addr_busy(vlan->port, dev->dev_addr))
373f9ac30f0SEric Biederman 		goto out;
374f9ac30f0SEric Biederman 
375a748ee24SJiri Pirko 	err = dev_uc_add(lowerdev, dev->dev_addr);
376b863ceb7SPatrick McHardy 	if (err < 0)
377b89fb7daSWang Chen 		goto out;
378b89fb7daSWang Chen 	if (dev->flags & IFF_ALLMULTI) {
379b89fb7daSWang Chen 		err = dev_set_allmulti(lowerdev, 1);
380b89fb7daSWang Chen 		if (err < 0)
381b89fb7daSWang Chen 			goto del_unicast;
382b89fb7daSWang Chen 	}
383eb06acdcSSridhar Samudrala 
384eb06acdcSSridhar Samudrala hash_add:
385f9ac30f0SEric Biederman 	macvlan_hash_add(vlan);
386b863ceb7SPatrick McHardy 	return 0;
387b89fb7daSWang Chen 
388b89fb7daSWang Chen del_unicast:
389a748ee24SJiri Pirko 	dev_uc_del(lowerdev, dev->dev_addr);
390b89fb7daSWang Chen out:
391a6cc0cfaSJohn Fastabend 	if (vlan->fwd_priv) {
392a6cc0cfaSJohn Fastabend 		lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev,
393a6cc0cfaSJohn Fastabend 							   vlan->fwd_priv);
394a6cc0cfaSJohn Fastabend 		vlan->fwd_priv = NULL;
395a6cc0cfaSJohn Fastabend 	}
396b89fb7daSWang Chen 	return err;
397b863ceb7SPatrick McHardy }
398b863ceb7SPatrick McHardy 
399b863ceb7SPatrick McHardy static int macvlan_stop(struct net_device *dev)
400b863ceb7SPatrick McHardy {
401b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
402b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
403b863ceb7SPatrick McHardy 
404a6cc0cfaSJohn Fastabend 	if (vlan->fwd_priv) {
405a6cc0cfaSJohn Fastabend 		lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev,
406a6cc0cfaSJohn Fastabend 							   vlan->fwd_priv);
407a6cc0cfaSJohn Fastabend 		vlan->fwd_priv = NULL;
408a6cc0cfaSJohn Fastabend 		return 0;
409a6cc0cfaSJohn Fastabend 	}
410a6cc0cfaSJohn Fastabend 
411df8ef8f3SJohn Fastabend 	dev_uc_unsync(lowerdev, dev);
412df8ef8f3SJohn Fastabend 	dev_mc_unsync(lowerdev, dev);
413df8ef8f3SJohn Fastabend 
414eb06acdcSSridhar Samudrala 	if (vlan->port->passthru) {
415df8ef8f3SJohn Fastabend 		if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC))
416eb06acdcSSridhar Samudrala 			dev_set_promiscuity(lowerdev, -1);
417eb06acdcSSridhar Samudrala 		goto hash_del;
418eb06acdcSSridhar Samudrala 	}
419eb06acdcSSridhar Samudrala 
420b863ceb7SPatrick McHardy 	if (dev->flags & IFF_ALLMULTI)
421b863ceb7SPatrick McHardy 		dev_set_allmulti(lowerdev, -1);
422b863ceb7SPatrick McHardy 
423a748ee24SJiri Pirko 	dev_uc_del(lowerdev, dev->dev_addr);
424b863ceb7SPatrick McHardy 
425eb06acdcSSridhar Samudrala hash_del:
426449f4544SEric Dumazet 	macvlan_hash_del(vlan, !dev->dismantle);
427b863ceb7SPatrick McHardy 	return 0;
428b863ceb7SPatrick McHardy }
429b863ceb7SPatrick McHardy 
430ad5d20a6SPatrick McHardy static int macvlan_set_mac_address(struct net_device *dev, void *p)
431ad5d20a6SPatrick McHardy {
432ad5d20a6SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
433ad5d20a6SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
434ad5d20a6SPatrick McHardy 	struct sockaddr *addr = p;
435ad5d20a6SPatrick McHardy 	int err;
436ad5d20a6SPatrick McHardy 
437ad5d20a6SPatrick McHardy 	if (!is_valid_ether_addr(addr->sa_data))
438ad5d20a6SPatrick McHardy 		return -EADDRNOTAVAIL;
439ad5d20a6SPatrick McHardy 
440f9ac30f0SEric Biederman 	if (!(dev->flags & IFF_UP)) {
441f9ac30f0SEric Biederman 		/* Just copy in the new address */
442f9ac30f0SEric Biederman 		memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
443f9ac30f0SEric Biederman 	} else {
444f9ac30f0SEric Biederman 		/* Rehash and update the device filters */
445f9ac30f0SEric Biederman 		if (macvlan_addr_busy(vlan->port, addr->sa_data))
446f9ac30f0SEric Biederman 			return -EBUSY;
447ad5d20a6SPatrick McHardy 
448a748ee24SJiri Pirko 		err = dev_uc_add(lowerdev, addr->sa_data);
449ccffad25SJiri Pirko 		if (err)
450ad5d20a6SPatrick McHardy 			return err;
451f9ac30f0SEric Biederman 
452a748ee24SJiri Pirko 		dev_uc_del(lowerdev, dev->dev_addr);
453ad5d20a6SPatrick McHardy 
454f9ac30f0SEric Biederman 		macvlan_hash_change_addr(vlan, addr->sa_data);
455f9ac30f0SEric Biederman 	}
456ad5d20a6SPatrick McHardy 	return 0;
457ad5d20a6SPatrick McHardy }
458ad5d20a6SPatrick McHardy 
459b863ceb7SPatrick McHardy static void macvlan_change_rx_flags(struct net_device *dev, int change)
460b863ceb7SPatrick McHardy {
461b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
462b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
463b863ceb7SPatrick McHardy 
464b863ceb7SPatrick McHardy 	if (change & IFF_ALLMULTI)
465b863ceb7SPatrick McHardy 		dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
466b863ceb7SPatrick McHardy }
467b863ceb7SPatrick McHardy 
468df8ef8f3SJohn Fastabend static void macvlan_set_mac_lists(struct net_device *dev)
469b863ceb7SPatrick McHardy {
470b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
471b863ceb7SPatrick McHardy 
472cd431e73SEric Dumazet 	if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
473cd431e73SEric Dumazet 		bitmap_fill(vlan->mc_filter, MACVLAN_MC_FILTER_SZ);
474cd431e73SEric Dumazet 	} else {
475cd431e73SEric Dumazet 		struct netdev_hw_addr *ha;
476cd431e73SEric Dumazet 		DECLARE_BITMAP(filter, MACVLAN_MC_FILTER_SZ);
477cd431e73SEric Dumazet 
478cd431e73SEric Dumazet 		bitmap_zero(filter, MACVLAN_MC_FILTER_SZ);
479cd431e73SEric Dumazet 		netdev_for_each_mc_addr(ha, dev) {
4803807ff58SEric Dumazet 			__set_bit(mc_hash(vlan, ha->addr), filter);
481cd431e73SEric Dumazet 		}
482d5270430SEric Dumazet 
4833807ff58SEric Dumazet 		__set_bit(mc_hash(vlan, dev->broadcast), filter);
484d5270430SEric Dumazet 
485cd431e73SEric Dumazet 		bitmap_copy(vlan->mc_filter, filter, MACVLAN_MC_FILTER_SZ);
486cd431e73SEric Dumazet 	}
487df8ef8f3SJohn Fastabend 	dev_uc_sync(vlan->lowerdev, dev);
488b863ceb7SPatrick McHardy 	dev_mc_sync(vlan->lowerdev, dev);
489b863ceb7SPatrick McHardy }
490b863ceb7SPatrick McHardy 
491b863ceb7SPatrick McHardy static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
492b863ceb7SPatrick McHardy {
493b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
494b863ceb7SPatrick McHardy 
495b863ceb7SPatrick McHardy 	if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
496b863ceb7SPatrick McHardy 		return -EINVAL;
497b863ceb7SPatrick McHardy 	dev->mtu = new_mtu;
498b863ceb7SPatrick McHardy 	return 0;
499b863ceb7SPatrick McHardy }
500b863ceb7SPatrick McHardy 
501b863ceb7SPatrick McHardy /*
502b863ceb7SPatrick McHardy  * macvlan network devices have devices nesting below it and are a special
503b863ceb7SPatrick McHardy  * "super class" of normal network devices; split their locks off into a
504b863ceb7SPatrick McHardy  * separate class since they always nest.
505b863ceb7SPatrick McHardy  */
506b863ceb7SPatrick McHardy static struct lock_class_key macvlan_netdev_xmit_lock_key;
507cf508b12SDavid S. Miller static struct lock_class_key macvlan_netdev_addr_lock_key;
508b863ceb7SPatrick McHardy 
509*8b4703e9SVlad Yasevich #define ALWAYS_ON_FEATURES \
510*8b4703e9SVlad Yasevich 	(NETIF_F_SG | NETIF_F_GEN_CSUM | NETIF_F_GSO_SOFTWARE | NETIF_F_LLTX)
511*8b4703e9SVlad Yasevich 
512b863ceb7SPatrick McHardy #define MACVLAN_FEATURES \
513b863ceb7SPatrick McHardy 	(NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
514b863ceb7SPatrick McHardy 	 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
5158d13e670SJohn Fastabend 	 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
51628d2b136SPatrick McHardy 	 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
517b863ceb7SPatrick McHardy 
518b863ceb7SPatrick McHardy #define MACVLAN_STATE_MASK \
519b863ceb7SPatrick McHardy 	((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
520b863ceb7SPatrick McHardy 
521e8a0464cSDavid S. Miller static void macvlan_set_lockdep_class_one(struct net_device *dev,
522e8a0464cSDavid S. Miller 					  struct netdev_queue *txq,
523e8a0464cSDavid S. Miller 					  void *_unused)
524c773e847SDavid S. Miller {
525c773e847SDavid S. Miller 	lockdep_set_class(&txq->_xmit_lock,
526c773e847SDavid S. Miller 			  &macvlan_netdev_xmit_lock_key);
527c773e847SDavid S. Miller }
528c773e847SDavid S. Miller 
529c773e847SDavid S. Miller static void macvlan_set_lockdep_class(struct net_device *dev)
530c773e847SDavid S. Miller {
531cf508b12SDavid S. Miller 	lockdep_set_class(&dev->addr_list_lock,
532cf508b12SDavid S. Miller 			  &macvlan_netdev_addr_lock_key);
533e8a0464cSDavid S. Miller 	netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL);
534c773e847SDavid S. Miller }
535c773e847SDavid S. Miller 
536b863ceb7SPatrick McHardy static int macvlan_init(struct net_device *dev)
537b863ceb7SPatrick McHardy {
538b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
539b863ceb7SPatrick McHardy 	const struct net_device *lowerdev = vlan->lowerdev;
540827da44cSJohn Stultz 	int i;
541b863ceb7SPatrick McHardy 
542b863ceb7SPatrick McHardy 	dev->state		= (dev->state & ~MACVLAN_STATE_MASK) |
543b863ceb7SPatrick McHardy 				  (lowerdev->state & MACVLAN_STATE_MASK);
544b863ceb7SPatrick McHardy 	dev->features 		= lowerdev->features & MACVLAN_FEATURES;
545*8b4703e9SVlad Yasevich 	dev->features		|= ALWAYS_ON_FEATURES;
5468c2acc53SPatrick McHardy 	dev->gso_max_size	= lowerdev->gso_max_size;
547b863ceb7SPatrick McHardy 	dev->iflink		= lowerdev->ifindex;
548ef5c8996Ssg.tweak@gmail.com 	dev->hard_header_len	= lowerdev->hard_header_len;
549b863ceb7SPatrick McHardy 
550c773e847SDavid S. Miller 	macvlan_set_lockdep_class(dev);
551c773e847SDavid S. Miller 
552cdf3e274SLi RongQing 	vlan->pcpu_stats = alloc_percpu(struct vlan_pcpu_stats);
5538ffab51bSEric Dumazet 	if (!vlan->pcpu_stats)
554fccaf710SEric Dumazet 		return -ENOMEM;
555fccaf710SEric Dumazet 
556827da44cSJohn Stultz 	for_each_possible_cpu(i) {
557cdf3e274SLi RongQing 		struct vlan_pcpu_stats *mvlstats;
558827da44cSJohn Stultz 		mvlstats = per_cpu_ptr(vlan->pcpu_stats, i);
559827da44cSJohn Stultz 		u64_stats_init(&mvlstats->syncp);
560827da44cSJohn Stultz 	}
561827da44cSJohn Stultz 
562b863ceb7SPatrick McHardy 	return 0;
563b863ceb7SPatrick McHardy }
564b863ceb7SPatrick McHardy 
565fccaf710SEric Dumazet static void macvlan_uninit(struct net_device *dev)
566fccaf710SEric Dumazet {
567fccaf710SEric Dumazet 	struct macvlan_dev *vlan = netdev_priv(dev);
568d5cd9244SEric W. Biederman 	struct macvlan_port *port = vlan->port;
569fccaf710SEric Dumazet 
5708ffab51bSEric Dumazet 	free_percpu(vlan->pcpu_stats);
571d5cd9244SEric W. Biederman 
572d5cd9244SEric W. Biederman 	port->count -= 1;
573d5cd9244SEric W. Biederman 	if (!port->count)
574d5cd9244SEric W. Biederman 		macvlan_port_destroy(port->dev);
575fccaf710SEric Dumazet }
576fccaf710SEric Dumazet 
57728172739SEric Dumazet static struct rtnl_link_stats64 *macvlan_dev_get_stats64(struct net_device *dev,
57828172739SEric Dumazet 							 struct rtnl_link_stats64 *stats)
579fccaf710SEric Dumazet {
580fccaf710SEric Dumazet 	struct macvlan_dev *vlan = netdev_priv(dev);
581fccaf710SEric Dumazet 
5828ffab51bSEric Dumazet 	if (vlan->pcpu_stats) {
583cdf3e274SLi RongQing 		struct vlan_pcpu_stats *p;
5848ffab51bSEric Dumazet 		u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
5858ffab51bSEric Dumazet 		u32 rx_errors = 0, tx_dropped = 0;
586bc66154eSEric Dumazet 		unsigned int start;
587fccaf710SEric Dumazet 		int i;
588fccaf710SEric Dumazet 
589fccaf710SEric Dumazet 		for_each_possible_cpu(i) {
5908ffab51bSEric Dumazet 			p = per_cpu_ptr(vlan->pcpu_stats, i);
591bc66154eSEric Dumazet 			do {
592bc66154eSEric Dumazet 				start = u64_stats_fetch_begin_bh(&p->syncp);
593bc66154eSEric Dumazet 				rx_packets	= p->rx_packets;
594bc66154eSEric Dumazet 				rx_bytes	= p->rx_bytes;
595bc66154eSEric Dumazet 				rx_multicast	= p->rx_multicast;
5968ffab51bSEric Dumazet 				tx_packets	= p->tx_packets;
5978ffab51bSEric Dumazet 				tx_bytes	= p->tx_bytes;
598bc66154eSEric Dumazet 			} while (u64_stats_fetch_retry_bh(&p->syncp, start));
5998ffab51bSEric Dumazet 
6008ffab51bSEric Dumazet 			stats->rx_packets	+= rx_packets;
6018ffab51bSEric Dumazet 			stats->rx_bytes		+= rx_bytes;
6028ffab51bSEric Dumazet 			stats->multicast	+= rx_multicast;
6038ffab51bSEric Dumazet 			stats->tx_packets	+= tx_packets;
6048ffab51bSEric Dumazet 			stats->tx_bytes		+= tx_bytes;
6058ffab51bSEric Dumazet 			/* rx_errors & tx_dropped are u32, updated
6068ffab51bSEric Dumazet 			 * without syncp protection.
6078ffab51bSEric Dumazet 			 */
6088ffab51bSEric Dumazet 			rx_errors	+= p->rx_errors;
6098ffab51bSEric Dumazet 			tx_dropped	+= p->tx_dropped;
610fccaf710SEric Dumazet 		}
6118ffab51bSEric Dumazet 		stats->rx_errors	= rx_errors;
6128ffab51bSEric Dumazet 		stats->rx_dropped	= rx_errors;
6138ffab51bSEric Dumazet 		stats->tx_dropped	= tx_dropped;
614fccaf710SEric Dumazet 	}
615fccaf710SEric Dumazet 	return stats;
616fccaf710SEric Dumazet }
617fccaf710SEric Dumazet 
6188e586137SJiri Pirko static int macvlan_vlan_rx_add_vid(struct net_device *dev,
61980d5c368SPatrick McHardy 				   __be16 proto, u16 vid)
6208d13e670SJohn Fastabend {
6218d13e670SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
6228d13e670SJohn Fastabend 	struct net_device *lowerdev = vlan->lowerdev;
6238d13e670SJohn Fastabend 
62480d5c368SPatrick McHardy 	return vlan_vid_add(lowerdev, proto, vid);
6258d13e670SJohn Fastabend }
6268d13e670SJohn Fastabend 
6278e586137SJiri Pirko static int macvlan_vlan_rx_kill_vid(struct net_device *dev,
62880d5c368SPatrick McHardy 				    __be16 proto, u16 vid)
6298d13e670SJohn Fastabend {
6308d13e670SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
6318d13e670SJohn Fastabend 	struct net_device *lowerdev = vlan->lowerdev;
6328d13e670SJohn Fastabend 
63380d5c368SPatrick McHardy 	vlan_vid_del(lowerdev, proto, vid);
6348e586137SJiri Pirko 	return 0;
6358d13e670SJohn Fastabend }
6368d13e670SJohn Fastabend 
637edc7d573Sstephen hemminger static int macvlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
638df8ef8f3SJohn Fastabend 			   struct net_device *dev,
6396b6e2725Sstephen hemminger 			   const unsigned char *addr,
640df8ef8f3SJohn Fastabend 			   u16 flags)
641df8ef8f3SJohn Fastabend {
642df8ef8f3SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
643df8ef8f3SJohn Fastabend 	int err = -EINVAL;
644df8ef8f3SJohn Fastabend 
645df8ef8f3SJohn Fastabend 	if (!vlan->port->passthru)
646df8ef8f3SJohn Fastabend 		return -EOPNOTSUPP;
647df8ef8f3SJohn Fastabend 
648ab2cfbb2SThomas Richter 	if (flags & NLM_F_REPLACE)
649ab2cfbb2SThomas Richter 		return -EOPNOTSUPP;
650ab2cfbb2SThomas Richter 
651df8ef8f3SJohn Fastabend 	if (is_unicast_ether_addr(addr))
652df8ef8f3SJohn Fastabend 		err = dev_uc_add_excl(dev, addr);
653df8ef8f3SJohn Fastabend 	else if (is_multicast_ether_addr(addr))
654df8ef8f3SJohn Fastabend 		err = dev_mc_add_excl(dev, addr);
655df8ef8f3SJohn Fastabend 
656df8ef8f3SJohn Fastabend 	return err;
657df8ef8f3SJohn Fastabend }
658df8ef8f3SJohn Fastabend 
6591690be63SVlad Yasevich static int macvlan_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
660df8ef8f3SJohn Fastabend 			   struct net_device *dev,
6616b6e2725Sstephen hemminger 			   const unsigned char *addr)
662df8ef8f3SJohn Fastabend {
663df8ef8f3SJohn Fastabend 	struct macvlan_dev *vlan = netdev_priv(dev);
664df8ef8f3SJohn Fastabend 	int err = -EINVAL;
665df8ef8f3SJohn Fastabend 
666df8ef8f3SJohn Fastabend 	if (!vlan->port->passthru)
667df8ef8f3SJohn Fastabend 		return -EOPNOTSUPP;
668df8ef8f3SJohn Fastabend 
669df8ef8f3SJohn Fastabend 	if (is_unicast_ether_addr(addr))
670df8ef8f3SJohn Fastabend 		err = dev_uc_del(dev, addr);
671df8ef8f3SJohn Fastabend 	else if (is_multicast_ether_addr(addr))
672df8ef8f3SJohn Fastabend 		err = dev_mc_del(dev, addr);
673df8ef8f3SJohn Fastabend 
674df8ef8f3SJohn Fastabend 	return err;
675df8ef8f3SJohn Fastabend }
676df8ef8f3SJohn Fastabend 
677b863ceb7SPatrick McHardy static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
678b863ceb7SPatrick McHardy 					struct ethtool_drvinfo *drvinfo)
679b863ceb7SPatrick McHardy {
6807826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "macvlan", sizeof(drvinfo->driver));
6817826d43fSJiri Pirko 	strlcpy(drvinfo->version, "0.1", sizeof(drvinfo->version));
682b863ceb7SPatrick McHardy }
683b863ceb7SPatrick McHardy 
6849edb8bb6SStephen Hemminger static int macvlan_ethtool_get_settings(struct net_device *dev,
6859edb8bb6SStephen Hemminger 					struct ethtool_cmd *cmd)
6869edb8bb6SStephen Hemminger {
6879edb8bb6SStephen Hemminger 	const struct macvlan_dev *vlan = netdev_priv(dev);
6884bc71cb9SJiri Pirko 
6894bc71cb9SJiri Pirko 	return __ethtool_get_settings(vlan->lowerdev, cmd);
6909edb8bb6SStephen Hemminger }
6919edb8bb6SStephen Hemminger 
6922be5c767SVlad Yasevich static netdev_features_t macvlan_fix_features(struct net_device *dev,
6932be5c767SVlad Yasevich 					      netdev_features_t features)
6942be5c767SVlad Yasevich {
6952be5c767SVlad Yasevich 	struct macvlan_dev *vlan = netdev_priv(dev);
696797f87f8SFlorian Westphal 	netdev_features_t mask;
6972be5c767SVlad Yasevich 
698797f87f8SFlorian Westphal 	features |= NETIF_F_ALL_FOR_ALL;
699797f87f8SFlorian Westphal 	features &= (vlan->set_features | ~MACVLAN_FEATURES);
700797f87f8SFlorian Westphal 	mask = features;
701797f87f8SFlorian Westphal 
702797f87f8SFlorian Westphal 	features = netdev_increment_features(vlan->lowerdev->features,
703797f87f8SFlorian Westphal 					     features,
704797f87f8SFlorian Westphal 					     mask);
705*8b4703e9SVlad Yasevich 	features |= ALWAYS_ON_FEATURES;
706797f87f8SFlorian Westphal 
707797f87f8SFlorian Westphal 	return features;
7082be5c767SVlad Yasevich }
7092be5c767SVlad Yasevich 
710b863ceb7SPatrick McHardy static const struct ethtool_ops macvlan_ethtool_ops = {
711b863ceb7SPatrick McHardy 	.get_link		= ethtool_op_get_link,
7129edb8bb6SStephen Hemminger 	.get_settings		= macvlan_ethtool_get_settings,
713b863ceb7SPatrick McHardy 	.get_drvinfo		= macvlan_ethtool_get_drvinfo,
714b863ceb7SPatrick McHardy };
715b863ceb7SPatrick McHardy 
71654a30c97SStephen Hemminger static const struct net_device_ops macvlan_netdev_ops = {
71754a30c97SStephen Hemminger 	.ndo_init		= macvlan_init,
718fccaf710SEric Dumazet 	.ndo_uninit		= macvlan_uninit,
71954a30c97SStephen Hemminger 	.ndo_open		= macvlan_open,
72054a30c97SStephen Hemminger 	.ndo_stop		= macvlan_stop,
72100829823SStephen Hemminger 	.ndo_start_xmit		= macvlan_start_xmit,
72254a30c97SStephen Hemminger 	.ndo_change_mtu		= macvlan_change_mtu,
7232be5c767SVlad Yasevich 	.ndo_fix_features	= macvlan_fix_features,
72454a30c97SStephen Hemminger 	.ndo_change_rx_flags	= macvlan_change_rx_flags,
72554a30c97SStephen Hemminger 	.ndo_set_mac_address	= macvlan_set_mac_address,
726df8ef8f3SJohn Fastabend 	.ndo_set_rx_mode	= macvlan_set_mac_lists,
727bc66154eSEric Dumazet 	.ndo_get_stats64	= macvlan_dev_get_stats64,
72854a30c97SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
7298d13e670SJohn Fastabend 	.ndo_vlan_rx_add_vid	= macvlan_vlan_rx_add_vid,
7308d13e670SJohn Fastabend 	.ndo_vlan_rx_kill_vid	= macvlan_vlan_rx_kill_vid,
731df8ef8f3SJohn Fastabend 	.ndo_fdb_add		= macvlan_fdb_add,
732df8ef8f3SJohn Fastabend 	.ndo_fdb_del		= macvlan_fdb_del,
733df8ef8f3SJohn Fastabend 	.ndo_fdb_dump		= ndo_dflt_fdb_dump,
73454a30c97SStephen Hemminger };
73554a30c97SStephen Hemminger 
7368a35747aSHerbert Xu void macvlan_common_setup(struct net_device *dev)
737b863ceb7SPatrick McHardy {
738b863ceb7SPatrick McHardy 	ether_setup(dev);
739b863ceb7SPatrick McHardy 
740550fd08cSNeil Horman 	dev->priv_flags	       &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
74187ab7f6fSVlad Yasevich 	dev->priv_flags	       |= IFF_UNICAST_FLT;
74254a30c97SStephen Hemminger 	dev->netdev_ops		= &macvlan_netdev_ops;
743b863ceb7SPatrick McHardy 	dev->destructor		= free_netdev;
74471740129SLutz Jaenicke 	dev->header_ops		= &macvlan_hard_header_ops;
745b863ceb7SPatrick McHardy 	dev->ethtool_ops	= &macvlan_ethtool_ops;
7468a35747aSHerbert Xu }
7478a35747aSHerbert Xu EXPORT_SYMBOL_GPL(macvlan_common_setup);
7488a35747aSHerbert Xu 
7498a35747aSHerbert Xu static void macvlan_setup(struct net_device *dev)
7508a35747aSHerbert Xu {
7518a35747aSHerbert Xu 	macvlan_common_setup(dev);
752b863ceb7SPatrick McHardy 	dev->tx_queue_len	= 0;
753b863ceb7SPatrick McHardy }
754b863ceb7SPatrick McHardy 
755b863ceb7SPatrick McHardy static int macvlan_port_create(struct net_device *dev)
756b863ceb7SPatrick McHardy {
757b863ceb7SPatrick McHardy 	struct macvlan_port *port;
758b863ceb7SPatrick McHardy 	unsigned int i;
759ab95bfe0SJiri Pirko 	int err;
760b863ceb7SPatrick McHardy 
761b863ceb7SPatrick McHardy 	if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
762b863ceb7SPatrick McHardy 		return -EINVAL;
763b863ceb7SPatrick McHardy 
764b863ceb7SPatrick McHardy 	port = kzalloc(sizeof(*port), GFP_KERNEL);
765b863ceb7SPatrick McHardy 	if (port == NULL)
766b863ceb7SPatrick McHardy 		return -ENOMEM;
767b863ceb7SPatrick McHardy 
768eb06acdcSSridhar Samudrala 	port->passthru = false;
769b863ceb7SPatrick McHardy 	port->dev = dev;
770b863ceb7SPatrick McHardy 	INIT_LIST_HEAD(&port->vlans);
771b863ceb7SPatrick McHardy 	for (i = 0; i < MACVLAN_HASH_SIZE; i++)
772b863ceb7SPatrick McHardy 		INIT_HLIST_HEAD(&port->vlan_hash[i]);
773ab95bfe0SJiri Pirko 
774a35e2c1bSJiri Pirko 	err = netdev_rx_handler_register(dev, macvlan_handle_frame, port);
775a35e2c1bSJiri Pirko 	if (err)
776ab95bfe0SJiri Pirko 		kfree(port);
777d9351561SEric Dumazet 	else
778a35e2c1bSJiri Pirko 		dev->priv_flags |= IFF_MACVLAN_PORT;
779ab95bfe0SJiri Pirko 	return err;
780b863ceb7SPatrick McHardy }
781b863ceb7SPatrick McHardy 
782b863ceb7SPatrick McHardy static void macvlan_port_destroy(struct net_device *dev)
783b863ceb7SPatrick McHardy {
784e052f7e6SEric Dumazet 	struct macvlan_port *port = macvlan_port_get_rtnl(dev);
785b863ceb7SPatrick McHardy 
786a35e2c1bSJiri Pirko 	dev->priv_flags &= ~IFF_MACVLAN_PORT;
787ab95bfe0SJiri Pirko 	netdev_rx_handler_unregister(dev);
7886e070aecSLai Jiangshan 	kfree_rcu(port, rcu);
789b863ceb7SPatrick McHardy }
790b863ceb7SPatrick McHardy 
791b863ceb7SPatrick McHardy static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
792b863ceb7SPatrick McHardy {
793b863ceb7SPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
794b863ceb7SPatrick McHardy 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
795b863ceb7SPatrick McHardy 			return -EINVAL;
796b863ceb7SPatrick McHardy 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
797b863ceb7SPatrick McHardy 			return -EADDRNOTAVAIL;
798b863ceb7SPatrick McHardy 	}
79927c0b1a8SArnd Bergmann 
80015127478SMichael S. Tsirkin 	if (data && data[IFLA_MACVLAN_FLAGS] &&
80115127478SMichael S. Tsirkin 	    nla_get_u16(data[IFLA_MACVLAN_FLAGS]) & ~MACVLAN_FLAG_NOPROMISC)
80215127478SMichael S. Tsirkin 		return -EINVAL;
80315127478SMichael S. Tsirkin 
80427c0b1a8SArnd Bergmann 	if (data && data[IFLA_MACVLAN_MODE]) {
80527c0b1a8SArnd Bergmann 		switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) {
80627c0b1a8SArnd Bergmann 		case MACVLAN_MODE_PRIVATE:
80727c0b1a8SArnd Bergmann 		case MACVLAN_MODE_VEPA:
80827c0b1a8SArnd Bergmann 		case MACVLAN_MODE_BRIDGE:
809eb06acdcSSridhar Samudrala 		case MACVLAN_MODE_PASSTHRU:
81027c0b1a8SArnd Bergmann 			break;
81127c0b1a8SArnd Bergmann 		default:
81227c0b1a8SArnd Bergmann 			return -EINVAL;
81327c0b1a8SArnd Bergmann 		}
81427c0b1a8SArnd Bergmann 	}
815b863ceb7SPatrick McHardy 	return 0;
816b863ceb7SPatrick McHardy }
817b863ceb7SPatrick McHardy 
818fc0663d6SArnd Bergmann int macvlan_common_newlink(struct net *src_net, struct net_device *dev,
8192f6a1b66SVlad Yasevich 			   struct nlattr *tb[], struct nlattr *data[])
820b863ceb7SPatrick McHardy {
821b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
822b863ceb7SPatrick McHardy 	struct macvlan_port *port;
823b863ceb7SPatrick McHardy 	struct net_device *lowerdev;
824b863ceb7SPatrick McHardy 	int err;
825b863ceb7SPatrick McHardy 
826b863ceb7SPatrick McHardy 	if (!tb[IFLA_LINK])
827b863ceb7SPatrick McHardy 		return -EINVAL;
828b863ceb7SPatrick McHardy 
82981adee47SEric W. Biederman 	lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
830b863ceb7SPatrick McHardy 	if (lowerdev == NULL)
831b863ceb7SPatrick McHardy 		return -ENODEV;
832b863ceb7SPatrick McHardy 
833d70f2cf5SKevin Wallace 	/* When creating macvlans or macvtaps on top of other macvlans - use
834b0832a29SEric Biederman 	 * the real device as the lowerdev.
835a6ca5f1dSPatrick McHardy 	 */
836d70f2cf5SKevin Wallace 	if (netif_is_macvlan(lowerdev))
837d70f2cf5SKevin Wallace 		lowerdev = macvlan_dev_real_dev(lowerdev);
838a6ca5f1dSPatrick McHardy 
839b863ceb7SPatrick McHardy 	if (!tb[IFLA_MTU])
840b863ceb7SPatrick McHardy 		dev->mtu = lowerdev->mtu;
841b863ceb7SPatrick McHardy 	else if (dev->mtu > lowerdev->mtu)
842b863ceb7SPatrick McHardy 		return -EINVAL;
843b863ceb7SPatrick McHardy 
844b863ceb7SPatrick McHardy 	if (!tb[IFLA_ADDRESS])
8457ce5d222SDanny Kukawka 		eth_hw_addr_random(dev);
846b863ceb7SPatrick McHardy 
847a35e2c1bSJiri Pirko 	if (!macvlan_port_exists(lowerdev)) {
848b863ceb7SPatrick McHardy 		err = macvlan_port_create(lowerdev);
849b863ceb7SPatrick McHardy 		if (err < 0)
850b863ceb7SPatrick McHardy 			return err;
851b863ceb7SPatrick McHardy 	}
852e052f7e6SEric Dumazet 	port = macvlan_port_get_rtnl(lowerdev);
853b863ceb7SPatrick McHardy 
854eb06acdcSSridhar Samudrala 	/* Only 1 macvlan device can be created in passthru mode */
855eb06acdcSSridhar Samudrala 	if (port->passthru)
856eb06acdcSSridhar Samudrala 		return -EINVAL;
857eb06acdcSSridhar Samudrala 
858b863ceb7SPatrick McHardy 	vlan->lowerdev = lowerdev;
859b863ceb7SPatrick McHardy 	vlan->dev      = dev;
860b863ceb7SPatrick McHardy 	vlan->port     = port;
8612be5c767SVlad Yasevich 	vlan->set_features = MACVLAN_FEATURES;
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