xref: /openbmc/linux/drivers/net/macvlan.c (revision 8a83a00b0735190384a348156837918271034144)
1b863ceb7SPatrick McHardy /*
2b863ceb7SPatrick McHardy  * Copyright (c) 2007 Patrick McHardy <kaber@trash.net>
3b863ceb7SPatrick McHardy  *
4b863ceb7SPatrick McHardy  * This program is free software; you can redistribute it and/or
5b863ceb7SPatrick McHardy  * modify it under the terms of the GNU General Public License as
6b863ceb7SPatrick McHardy  * published by the Free Software Foundation; either version 2 of
7b863ceb7SPatrick McHardy  * the License, or (at your option) any later version.
8b863ceb7SPatrick McHardy  *
9b863ceb7SPatrick McHardy  * The code this is based on carried the following copyright notice:
10b863ceb7SPatrick McHardy  * ---
11b863ceb7SPatrick McHardy  * (C) Copyright 2001-2006
12b863ceb7SPatrick McHardy  * Alex Zeffertt, Cambridge Broadband Ltd, ajz@cambridgebroadband.com
13b863ceb7SPatrick McHardy  * Re-worked by Ben Greear <greearb@candelatech.com>
14b863ceb7SPatrick McHardy  * ---
15b863ceb7SPatrick McHardy  */
16b863ceb7SPatrick McHardy #include <linux/kernel.h>
17b863ceb7SPatrick McHardy #include <linux/types.h>
18b863ceb7SPatrick McHardy #include <linux/module.h>
19b863ceb7SPatrick McHardy #include <linux/init.h>
20b863ceb7SPatrick McHardy #include <linux/errno.h>
21b863ceb7SPatrick McHardy #include <linux/slab.h>
22b863ceb7SPatrick McHardy #include <linux/string.h>
2382524746SFranck Bui-Huu #include <linux/rculist.h>
24b863ceb7SPatrick McHardy #include <linux/notifier.h>
25b863ceb7SPatrick McHardy #include <linux/netdevice.h>
26b863ceb7SPatrick McHardy #include <linux/etherdevice.h>
27b863ceb7SPatrick McHardy #include <linux/ethtool.h>
28b863ceb7SPatrick McHardy #include <linux/if_arp.h>
29b863ceb7SPatrick McHardy #include <linux/if_link.h>
30b863ceb7SPatrick McHardy #include <linux/if_macvlan.h>
31b863ceb7SPatrick McHardy #include <net/rtnetlink.h>
32618e1b74SArnd Bergmann #include <net/xfrm.h>
33b863ceb7SPatrick McHardy 
34b863ceb7SPatrick McHardy #define MACVLAN_HASH_SIZE	(1 << BITS_PER_BYTE)
35b863ceb7SPatrick McHardy 
36b863ceb7SPatrick McHardy struct macvlan_port {
37b863ceb7SPatrick McHardy 	struct net_device	*dev;
38b863ceb7SPatrick McHardy 	struct hlist_head	vlan_hash[MACVLAN_HASH_SIZE];
39b863ceb7SPatrick McHardy 	struct list_head	vlans;
40b863ceb7SPatrick McHardy };
41b863ceb7SPatrick McHardy 
42fccaf710SEric Dumazet /**
43fccaf710SEric Dumazet  *	struct macvlan_rx_stats - MACVLAN percpu rx stats
44fccaf710SEric Dumazet  *	@rx_packets: number of received packets
45fccaf710SEric Dumazet  *	@rx_bytes: number of received bytes
46fccaf710SEric Dumazet  *	@multicast: number of received multicast packets
47fccaf710SEric Dumazet  *	@rx_errors: number of errors
48fccaf710SEric Dumazet  */
49fccaf710SEric Dumazet struct macvlan_rx_stats {
50fccaf710SEric Dumazet 	unsigned long rx_packets;
51fccaf710SEric Dumazet 	unsigned long rx_bytes;
52fccaf710SEric Dumazet 	unsigned long multicast;
53fccaf710SEric Dumazet 	unsigned long rx_errors;
54fccaf710SEric Dumazet };
55fccaf710SEric Dumazet 
56b863ceb7SPatrick McHardy struct macvlan_dev {
57b863ceb7SPatrick McHardy 	struct net_device	*dev;
58b863ceb7SPatrick McHardy 	struct list_head	list;
59b863ceb7SPatrick McHardy 	struct hlist_node	hlist;
60b863ceb7SPatrick McHardy 	struct macvlan_port	*port;
61b863ceb7SPatrick McHardy 	struct net_device	*lowerdev;
62fccaf710SEric Dumazet 	struct macvlan_rx_stats *rx_stats;
63618e1b74SArnd Bergmann 	enum macvlan_mode	mode;
64b863ceb7SPatrick McHardy };
65b863ceb7SPatrick McHardy 
66b863ceb7SPatrick McHardy 
67b863ceb7SPatrick McHardy static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
68b863ceb7SPatrick McHardy 					       const unsigned char *addr)
69b863ceb7SPatrick McHardy {
70b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan;
71b863ceb7SPatrick McHardy 	struct hlist_node *n;
72b863ceb7SPatrick McHardy 
73b863ceb7SPatrick McHardy 	hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[addr[5]], hlist) {
74ac06713dSEric Dumazet 		if (!compare_ether_addr_64bits(vlan->dev->dev_addr, addr))
75b863ceb7SPatrick McHardy 			return vlan;
76b863ceb7SPatrick McHardy 	}
77b863ceb7SPatrick McHardy 	return NULL;
78b863ceb7SPatrick McHardy }
79b863ceb7SPatrick McHardy 
80f9ac30f0SEric Biederman static void macvlan_hash_add(struct macvlan_dev *vlan)
81f9ac30f0SEric Biederman {
82f9ac30f0SEric Biederman 	struct macvlan_port *port = vlan->port;
83f9ac30f0SEric Biederman 	const unsigned char *addr = vlan->dev->dev_addr;
84f9ac30f0SEric Biederman 
85f9ac30f0SEric Biederman 	hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]);
86f9ac30f0SEric Biederman }
87f9ac30f0SEric Biederman 
88f9ac30f0SEric Biederman static void macvlan_hash_del(struct macvlan_dev *vlan)
89f9ac30f0SEric Biederman {
90f9ac30f0SEric Biederman 	hlist_del_rcu(&vlan->hlist);
91f9ac30f0SEric Biederman 	synchronize_rcu();
92f9ac30f0SEric Biederman }
93f9ac30f0SEric Biederman 
94f9ac30f0SEric Biederman static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
95f9ac30f0SEric Biederman 					const unsigned char *addr)
96f9ac30f0SEric Biederman {
97f9ac30f0SEric Biederman 	macvlan_hash_del(vlan);
98f9ac30f0SEric Biederman 	/* Now that we are unhashed it is safe to change the device
99f9ac30f0SEric Biederman 	 * address without confusing packet delivery.
100f9ac30f0SEric Biederman 	 */
101f9ac30f0SEric Biederman 	memcpy(vlan->dev->dev_addr, addr, ETH_ALEN);
102f9ac30f0SEric Biederman 	macvlan_hash_add(vlan);
103f9ac30f0SEric Biederman }
104f9ac30f0SEric Biederman 
105f9ac30f0SEric Biederman static int macvlan_addr_busy(const struct macvlan_port *port,
106f9ac30f0SEric Biederman 				const unsigned char *addr)
107f9ac30f0SEric Biederman {
108f9ac30f0SEric Biederman 	/* Test to see if the specified multicast address is
109f9ac30f0SEric Biederman 	 * currently in use by the underlying device or
110f9ac30f0SEric Biederman 	 * another macvlan.
111f9ac30f0SEric Biederman 	 */
112ac06713dSEric Dumazet 	if (!compare_ether_addr_64bits(port->dev->dev_addr, addr))
113f9ac30f0SEric Biederman 		return 1;
114f9ac30f0SEric Biederman 
115f9ac30f0SEric Biederman 	if (macvlan_hash_lookup(port, addr))
116f9ac30f0SEric Biederman 		return 1;
117f9ac30f0SEric Biederman 
118f9ac30f0SEric Biederman 	return 0;
119f9ac30f0SEric Biederman }
120f9ac30f0SEric Biederman 
121a1e514c5SArnd Bergmann static inline void macvlan_count_rx(const struct macvlan_dev *vlan,
122a1e514c5SArnd Bergmann 				    unsigned int len, bool success,
123a1e514c5SArnd Bergmann 				    bool multicast)
124a1e514c5SArnd Bergmann {
125a1e514c5SArnd Bergmann 	struct macvlan_rx_stats *rx_stats;
126a1e514c5SArnd Bergmann 
127a1e514c5SArnd Bergmann 	rx_stats = per_cpu_ptr(vlan->rx_stats, smp_processor_id());
128a1e514c5SArnd Bergmann 	if (likely(success)) {
129a1e514c5SArnd Bergmann 		rx_stats->rx_packets++;;
130a1e514c5SArnd Bergmann 		rx_stats->rx_bytes += len;
131a1e514c5SArnd Bergmann 		if (multicast)
132a1e514c5SArnd Bergmann 			rx_stats->multicast++;
133a1e514c5SArnd Bergmann 	} else {
134a1e514c5SArnd Bergmann 		rx_stats->rx_errors++;
135a1e514c5SArnd Bergmann 	}
136a1e514c5SArnd Bergmann }
137a1e514c5SArnd Bergmann 
138a1e514c5SArnd Bergmann static int macvlan_broadcast_one(struct sk_buff *skb, struct net_device *dev,
139618e1b74SArnd Bergmann 				 const struct ethhdr *eth, bool local)
140a1e514c5SArnd Bergmann {
141a1e514c5SArnd Bergmann 	if (!skb)
142a1e514c5SArnd Bergmann 		return NET_RX_DROP;
143a1e514c5SArnd Bergmann 
144618e1b74SArnd Bergmann 	if (local)
145618e1b74SArnd Bergmann 		return dev_forward_skb(dev, skb);
146618e1b74SArnd Bergmann 
147a1e514c5SArnd Bergmann 	skb->dev = dev;
148a1e514c5SArnd Bergmann 	if (!compare_ether_addr_64bits(eth->h_dest,
149a1e514c5SArnd Bergmann 				       dev->broadcast))
150a1e514c5SArnd Bergmann 		skb->pkt_type = PACKET_BROADCAST;
151a1e514c5SArnd Bergmann 	else
152a1e514c5SArnd Bergmann 		skb->pkt_type = PACKET_MULTICAST;
153a1e514c5SArnd Bergmann 
154a1e514c5SArnd Bergmann 	return netif_rx(skb);
155a1e514c5SArnd Bergmann }
156a1e514c5SArnd Bergmann 
157b863ceb7SPatrick McHardy static void macvlan_broadcast(struct sk_buff *skb,
158618e1b74SArnd Bergmann 			      const struct macvlan_port *port,
159618e1b74SArnd Bergmann 			      struct net_device *src,
160618e1b74SArnd Bergmann 			      enum macvlan_mode mode)
161b863ceb7SPatrick McHardy {
162b863ceb7SPatrick McHardy 	const struct ethhdr *eth = eth_hdr(skb);
163b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan;
164b863ceb7SPatrick McHardy 	struct hlist_node *n;
165b863ceb7SPatrick McHardy 	struct sk_buff *nskb;
166b863ceb7SPatrick McHardy 	unsigned int i;
167a1e514c5SArnd Bergmann 	int err;
168b863ceb7SPatrick McHardy 
169efbbced3SPatrick McHardy 	if (skb->protocol == htons(ETH_P_PAUSE))
170efbbced3SPatrick McHardy 		return;
171efbbced3SPatrick McHardy 
172b863ceb7SPatrick McHardy 	for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
173b863ceb7SPatrick McHardy 		hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[i], hlist) {
174618e1b74SArnd Bergmann 			if (vlan->dev == src || !(vlan->mode & mode))
175618e1b74SArnd Bergmann 				continue;
176618e1b74SArnd Bergmann 
177b863ceb7SPatrick McHardy 			nskb = skb_clone(skb, GFP_ATOMIC);
178618e1b74SArnd Bergmann 			err = macvlan_broadcast_one(nskb, vlan->dev, eth,
179618e1b74SArnd Bergmann 					 mode == MACVLAN_MODE_BRIDGE);
180a1e514c5SArnd Bergmann 			macvlan_count_rx(vlan, skb->len + ETH_HLEN,
181a1e514c5SArnd Bergmann 					 err == NET_RX_SUCCESS, 1);
182b863ceb7SPatrick McHardy 		}
183b863ceb7SPatrick McHardy 	}
184b863ceb7SPatrick McHardy }
185b863ceb7SPatrick McHardy 
186b863ceb7SPatrick McHardy /* called under rcu_read_lock() from netif_receive_skb */
187b863ceb7SPatrick McHardy static struct sk_buff *macvlan_handle_frame(struct sk_buff *skb)
188b863ceb7SPatrick McHardy {
189b863ceb7SPatrick McHardy 	const struct ethhdr *eth = eth_hdr(skb);
190b863ceb7SPatrick McHardy 	const struct macvlan_port *port;
191b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan;
192618e1b74SArnd Bergmann 	const struct macvlan_dev *src;
193b863ceb7SPatrick McHardy 	struct net_device *dev;
194a1e514c5SArnd Bergmann 	unsigned int len;
195b863ceb7SPatrick McHardy 
196b863ceb7SPatrick McHardy 	port = rcu_dereference(skb->dev->macvlan_port);
197b863ceb7SPatrick McHardy 	if (port == NULL)
198b863ceb7SPatrick McHardy 		return skb;
199b863ceb7SPatrick McHardy 
200b863ceb7SPatrick McHardy 	if (is_multicast_ether_addr(eth->h_dest)) {
201618e1b74SArnd Bergmann 		src = macvlan_hash_lookup(port, eth->h_source);
202618e1b74SArnd Bergmann 		if (!src)
203618e1b74SArnd Bergmann 			/* frame comes from an external address */
204618e1b74SArnd Bergmann 			macvlan_broadcast(skb, port, NULL,
205618e1b74SArnd Bergmann 					  MACVLAN_MODE_PRIVATE |
206618e1b74SArnd Bergmann 					  MACVLAN_MODE_VEPA    |
207618e1b74SArnd Bergmann 					  MACVLAN_MODE_BRIDGE);
208618e1b74SArnd Bergmann 		else if (src->mode == MACVLAN_MODE_VEPA)
209618e1b74SArnd Bergmann 			/* flood to everyone except source */
210618e1b74SArnd Bergmann 			macvlan_broadcast(skb, port, src->dev,
211618e1b74SArnd Bergmann 					  MACVLAN_MODE_VEPA |
212618e1b74SArnd Bergmann 					  MACVLAN_MODE_BRIDGE);
213618e1b74SArnd Bergmann 		else if (src->mode == MACVLAN_MODE_BRIDGE)
214618e1b74SArnd Bergmann 			/*
215618e1b74SArnd Bergmann 			 * flood only to VEPA ports, bridge ports
216618e1b74SArnd Bergmann 			 * already saw the frame on the way out.
217618e1b74SArnd Bergmann 			 */
218618e1b74SArnd Bergmann 			macvlan_broadcast(skb, port, src->dev,
219618e1b74SArnd Bergmann 					  MACVLAN_MODE_VEPA);
220b863ceb7SPatrick McHardy 		return skb;
221b863ceb7SPatrick McHardy 	}
222b863ceb7SPatrick McHardy 
223b863ceb7SPatrick McHardy 	vlan = macvlan_hash_lookup(port, eth->h_dest);
224b863ceb7SPatrick McHardy 	if (vlan == NULL)
225b863ceb7SPatrick McHardy 		return skb;
226b863ceb7SPatrick McHardy 
227b863ceb7SPatrick McHardy 	dev = vlan->dev;
228b863ceb7SPatrick McHardy 	if (unlikely(!(dev->flags & IFF_UP))) {
229b863ceb7SPatrick McHardy 		kfree_skb(skb);
230b863ceb7SPatrick McHardy 		return NULL;
231b863ceb7SPatrick McHardy 	}
232a1e514c5SArnd Bergmann 	len = skb->len + ETH_HLEN;
233b863ceb7SPatrick McHardy 	skb = skb_share_check(skb, GFP_ATOMIC);
234a1e514c5SArnd Bergmann 	macvlan_count_rx(vlan, len, skb != NULL, 0);
235a1e514c5SArnd Bergmann 	if (!skb)
236b863ceb7SPatrick McHardy 		return NULL;
237b863ceb7SPatrick McHardy 
238b863ceb7SPatrick McHardy 	skb->dev = dev;
239b863ceb7SPatrick McHardy 	skb->pkt_type = PACKET_HOST;
240b863ceb7SPatrick McHardy 
241b863ceb7SPatrick McHardy 	netif_rx(skb);
242b863ceb7SPatrick McHardy 	return NULL;
243b863ceb7SPatrick McHardy }
244b863ceb7SPatrick McHardy 
245618e1b74SArnd Bergmann static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
246618e1b74SArnd Bergmann {
247618e1b74SArnd Bergmann 	const struct macvlan_dev *vlan = netdev_priv(dev);
248618e1b74SArnd Bergmann 	const struct macvlan_port *port = vlan->port;
249618e1b74SArnd Bergmann 	const struct macvlan_dev *dest;
250618e1b74SArnd Bergmann 
251618e1b74SArnd Bergmann 	if (vlan->mode == MACVLAN_MODE_BRIDGE) {
252618e1b74SArnd Bergmann 		const struct ethhdr *eth = (void *)skb->data;
253618e1b74SArnd Bergmann 
254618e1b74SArnd Bergmann 		/* send to other bridge ports directly */
255618e1b74SArnd Bergmann 		if (is_multicast_ether_addr(eth->h_dest)) {
256618e1b74SArnd Bergmann 			macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE);
257618e1b74SArnd Bergmann 			goto xmit_world;
258618e1b74SArnd Bergmann 		}
259618e1b74SArnd Bergmann 
260618e1b74SArnd Bergmann 		dest = macvlan_hash_lookup(port, eth->h_dest);
261618e1b74SArnd Bergmann 		if (dest && dest->mode == MACVLAN_MODE_BRIDGE) {
262618e1b74SArnd Bergmann 			unsigned int length = skb->len + ETH_HLEN;
263618e1b74SArnd Bergmann 			int ret = dev_forward_skb(dest->dev, skb);
264618e1b74SArnd Bergmann 			macvlan_count_rx(dest, length,
265618e1b74SArnd Bergmann 					 ret == NET_RX_SUCCESS, 0);
266618e1b74SArnd Bergmann 
267618e1b74SArnd Bergmann 			return NET_XMIT_SUCCESS;
268618e1b74SArnd Bergmann 		}
269618e1b74SArnd Bergmann 	}
270618e1b74SArnd Bergmann 
271618e1b74SArnd Bergmann xmit_world:
272*8a83a00bSArnd Bergmann 	skb_set_dev(skb, vlan->lowerdev);
273618e1b74SArnd Bergmann 	return dev_queue_xmit(skb);
274618e1b74SArnd Bergmann }
275618e1b74SArnd Bergmann 
276424efe9cSStephen Hemminger static netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
277424efe9cSStephen Hemminger 				      struct net_device *dev)
278b863ceb7SPatrick McHardy {
2792c114553SEric Dumazet 	int i = skb_get_queue_mapping(skb);
2802c114553SEric Dumazet 	struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
281b863ceb7SPatrick McHardy 	unsigned int len = skb->len;
282b863ceb7SPatrick McHardy 	int ret;
283b863ceb7SPatrick McHardy 
284618e1b74SArnd Bergmann 	ret = macvlan_queue_xmit(skb, dev);
285b863ceb7SPatrick McHardy 	if (likely(ret == NET_XMIT_SUCCESS)) {
2862c114553SEric Dumazet 		txq->tx_packets++;
2872c114553SEric Dumazet 		txq->tx_bytes += len;
2882c114553SEric Dumazet 	} else
2892c114553SEric Dumazet 		txq->tx_dropped++;
2902c114553SEric Dumazet 
291cbbef5e1SPatrick McHardy 	return ret;
292b863ceb7SPatrick McHardy }
293b863ceb7SPatrick McHardy 
294b863ceb7SPatrick McHardy static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
2953b04dddeSStephen Hemminger 			       unsigned short type, const void *daddr,
2963b04dddeSStephen Hemminger 			       const void *saddr, unsigned len)
297b863ceb7SPatrick McHardy {
298b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan = netdev_priv(dev);
299b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
300b863ceb7SPatrick McHardy 
3010c4e8581SStephen Hemminger 	return dev_hard_header(skb, lowerdev, type, daddr,
302b863ceb7SPatrick McHardy 			       saddr ? : dev->dev_addr, len);
303b863ceb7SPatrick McHardy }
304b863ceb7SPatrick McHardy 
3053b04dddeSStephen Hemminger static const struct header_ops macvlan_hard_header_ops = {
3063b04dddeSStephen Hemminger 	.create  	= macvlan_hard_header,
3073b04dddeSStephen Hemminger 	.rebuild	= eth_rebuild_header,
3083b04dddeSStephen Hemminger 	.parse		= eth_header_parse,
3093b04dddeSStephen Hemminger 	.cache		= eth_header_cache,
3103b04dddeSStephen Hemminger 	.cache_update	= eth_header_cache_update,
3113b04dddeSStephen Hemminger };
3123b04dddeSStephen Hemminger 
313b863ceb7SPatrick McHardy static int macvlan_open(struct net_device *dev)
314b863ceb7SPatrick McHardy {
315b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
316b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
317b863ceb7SPatrick McHardy 	int err;
318b863ceb7SPatrick McHardy 
319f9ac30f0SEric Biederman 	err = -EBUSY;
320f9ac30f0SEric Biederman 	if (macvlan_addr_busy(vlan->port, dev->dev_addr))
321f9ac30f0SEric Biederman 		goto out;
322f9ac30f0SEric Biederman 
323ccffad25SJiri Pirko 	err = dev_unicast_add(lowerdev, dev->dev_addr);
324b863ceb7SPatrick McHardy 	if (err < 0)
325b89fb7daSWang Chen 		goto out;
326b89fb7daSWang Chen 	if (dev->flags & IFF_ALLMULTI) {
327b89fb7daSWang Chen 		err = dev_set_allmulti(lowerdev, 1);
328b89fb7daSWang Chen 		if (err < 0)
329b89fb7daSWang Chen 			goto del_unicast;
330b89fb7daSWang Chen 	}
331f9ac30f0SEric Biederman 	macvlan_hash_add(vlan);
332b863ceb7SPatrick McHardy 	return 0;
333b89fb7daSWang Chen 
334b89fb7daSWang Chen del_unicast:
335ccffad25SJiri Pirko 	dev_unicast_delete(lowerdev, dev->dev_addr);
336b89fb7daSWang Chen out:
337b89fb7daSWang Chen 	return err;
338b863ceb7SPatrick McHardy }
339b863ceb7SPatrick McHardy 
340b863ceb7SPatrick McHardy static int macvlan_stop(struct net_device *dev)
341b863ceb7SPatrick McHardy {
342b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
343b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
344b863ceb7SPatrick McHardy 
345b863ceb7SPatrick McHardy 	dev_mc_unsync(lowerdev, dev);
346b863ceb7SPatrick McHardy 	if (dev->flags & IFF_ALLMULTI)
347b863ceb7SPatrick McHardy 		dev_set_allmulti(lowerdev, -1);
348b863ceb7SPatrick McHardy 
349ccffad25SJiri Pirko 	dev_unicast_delete(lowerdev, dev->dev_addr);
350b863ceb7SPatrick McHardy 
351f9ac30f0SEric Biederman 	macvlan_hash_del(vlan);
352b863ceb7SPatrick McHardy 	return 0;
353b863ceb7SPatrick McHardy }
354b863ceb7SPatrick McHardy 
355ad5d20a6SPatrick McHardy static int macvlan_set_mac_address(struct net_device *dev, void *p)
356ad5d20a6SPatrick McHardy {
357ad5d20a6SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
358ad5d20a6SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
359ad5d20a6SPatrick McHardy 	struct sockaddr *addr = p;
360ad5d20a6SPatrick McHardy 	int err;
361ad5d20a6SPatrick McHardy 
362ad5d20a6SPatrick McHardy 	if (!is_valid_ether_addr(addr->sa_data))
363ad5d20a6SPatrick McHardy 		return -EADDRNOTAVAIL;
364ad5d20a6SPatrick McHardy 
365f9ac30f0SEric Biederman 	if (!(dev->flags & IFF_UP)) {
366f9ac30f0SEric Biederman 		/* Just copy in the new address */
367f9ac30f0SEric Biederman 		memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
368f9ac30f0SEric Biederman 	} else {
369f9ac30f0SEric Biederman 		/* Rehash and update the device filters */
370f9ac30f0SEric Biederman 		if (macvlan_addr_busy(vlan->port, addr->sa_data))
371f9ac30f0SEric Biederman 			return -EBUSY;
372ad5d20a6SPatrick McHardy 
373ccffad25SJiri Pirko 		err = dev_unicast_add(lowerdev, addr->sa_data);
374ccffad25SJiri Pirko 		if (err)
375ad5d20a6SPatrick McHardy 			return err;
376f9ac30f0SEric Biederman 
377ccffad25SJiri Pirko 		dev_unicast_delete(lowerdev, dev->dev_addr);
378ad5d20a6SPatrick McHardy 
379f9ac30f0SEric Biederman 		macvlan_hash_change_addr(vlan, addr->sa_data);
380f9ac30f0SEric Biederman 	}
381ad5d20a6SPatrick McHardy 	return 0;
382ad5d20a6SPatrick McHardy }
383ad5d20a6SPatrick McHardy 
384b863ceb7SPatrick McHardy static void macvlan_change_rx_flags(struct net_device *dev, int change)
385b863ceb7SPatrick McHardy {
386b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
387b863ceb7SPatrick McHardy 	struct net_device *lowerdev = vlan->lowerdev;
388b863ceb7SPatrick McHardy 
389b863ceb7SPatrick McHardy 	if (change & IFF_ALLMULTI)
390b863ceb7SPatrick McHardy 		dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
391b863ceb7SPatrick McHardy }
392b863ceb7SPatrick McHardy 
393b863ceb7SPatrick McHardy static void macvlan_set_multicast_list(struct net_device *dev)
394b863ceb7SPatrick McHardy {
395b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
396b863ceb7SPatrick McHardy 
397b863ceb7SPatrick McHardy 	dev_mc_sync(vlan->lowerdev, dev);
398b863ceb7SPatrick McHardy }
399b863ceb7SPatrick McHardy 
400b863ceb7SPatrick McHardy static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
401b863ceb7SPatrick McHardy {
402b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
403b863ceb7SPatrick McHardy 
404b863ceb7SPatrick McHardy 	if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
405b863ceb7SPatrick McHardy 		return -EINVAL;
406b863ceb7SPatrick McHardy 	dev->mtu = new_mtu;
407b863ceb7SPatrick McHardy 	return 0;
408b863ceb7SPatrick McHardy }
409b863ceb7SPatrick McHardy 
410b863ceb7SPatrick McHardy /*
411b863ceb7SPatrick McHardy  * macvlan network devices have devices nesting below it and are a special
412b863ceb7SPatrick McHardy  * "super class" of normal network devices; split their locks off into a
413b863ceb7SPatrick McHardy  * separate class since they always nest.
414b863ceb7SPatrick McHardy  */
415b863ceb7SPatrick McHardy static struct lock_class_key macvlan_netdev_xmit_lock_key;
416cf508b12SDavid S. Miller static struct lock_class_key macvlan_netdev_addr_lock_key;
417b863ceb7SPatrick McHardy 
418b863ceb7SPatrick McHardy #define MACVLAN_FEATURES \
419b863ceb7SPatrick McHardy 	(NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
420b863ceb7SPatrick McHardy 	 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
4216eb3a855SPatrick Mullaney 	 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO)
422b863ceb7SPatrick McHardy 
423b863ceb7SPatrick McHardy #define MACVLAN_STATE_MASK \
424b863ceb7SPatrick McHardy 	((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
425b863ceb7SPatrick McHardy 
426e8a0464cSDavid S. Miller static void macvlan_set_lockdep_class_one(struct net_device *dev,
427e8a0464cSDavid S. Miller 					  struct netdev_queue *txq,
428e8a0464cSDavid S. Miller 					  void *_unused)
429c773e847SDavid S. Miller {
430c773e847SDavid S. Miller 	lockdep_set_class(&txq->_xmit_lock,
431c773e847SDavid S. Miller 			  &macvlan_netdev_xmit_lock_key);
432c773e847SDavid S. Miller }
433c773e847SDavid S. Miller 
434c773e847SDavid S. Miller static void macvlan_set_lockdep_class(struct net_device *dev)
435c773e847SDavid S. Miller {
436cf508b12SDavid S. Miller 	lockdep_set_class(&dev->addr_list_lock,
437cf508b12SDavid S. Miller 			  &macvlan_netdev_addr_lock_key);
438e8a0464cSDavid S. Miller 	netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL);
439c773e847SDavid S. Miller }
440c773e847SDavid S. Miller 
441b863ceb7SPatrick McHardy static int macvlan_init(struct net_device *dev)
442b863ceb7SPatrick McHardy {
443b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
444b863ceb7SPatrick McHardy 	const struct net_device *lowerdev = vlan->lowerdev;
445b863ceb7SPatrick McHardy 
446b863ceb7SPatrick McHardy 	dev->state		= (dev->state & ~MACVLAN_STATE_MASK) |
447b863ceb7SPatrick McHardy 				  (lowerdev->state & MACVLAN_STATE_MASK);
448b863ceb7SPatrick McHardy 	dev->features 		= lowerdev->features & MACVLAN_FEATURES;
4498c2acc53SPatrick McHardy 	dev->gso_max_size	= lowerdev->gso_max_size;
450b863ceb7SPatrick McHardy 	dev->iflink		= lowerdev->ifindex;
451ef5c8996Ssg.tweak@gmail.com 	dev->hard_header_len	= lowerdev->hard_header_len;
452b863ceb7SPatrick McHardy 
453c773e847SDavid S. Miller 	macvlan_set_lockdep_class(dev);
454c773e847SDavid S. Miller 
455fccaf710SEric Dumazet 	vlan->rx_stats = alloc_percpu(struct macvlan_rx_stats);
456fccaf710SEric Dumazet 	if (!vlan->rx_stats)
457fccaf710SEric Dumazet 		return -ENOMEM;
458fccaf710SEric Dumazet 
459b863ceb7SPatrick McHardy 	return 0;
460b863ceb7SPatrick McHardy }
461b863ceb7SPatrick McHardy 
462fccaf710SEric Dumazet static void macvlan_uninit(struct net_device *dev)
463fccaf710SEric Dumazet {
464fccaf710SEric Dumazet 	struct macvlan_dev *vlan = netdev_priv(dev);
465fccaf710SEric Dumazet 
466fccaf710SEric Dumazet 	free_percpu(vlan->rx_stats);
467fccaf710SEric Dumazet }
468fccaf710SEric Dumazet 
469fccaf710SEric Dumazet static struct net_device_stats *macvlan_dev_get_stats(struct net_device *dev)
470fccaf710SEric Dumazet {
471fccaf710SEric Dumazet 	struct net_device_stats *stats = &dev->stats;
472fccaf710SEric Dumazet 	struct macvlan_dev *vlan = netdev_priv(dev);
473fccaf710SEric Dumazet 
474fccaf710SEric Dumazet 	dev_txq_stats_fold(dev, stats);
475fccaf710SEric Dumazet 
476fccaf710SEric Dumazet 	if (vlan->rx_stats) {
477fccaf710SEric Dumazet 		struct macvlan_rx_stats *p, rx = {0};
478fccaf710SEric Dumazet 		int i;
479fccaf710SEric Dumazet 
480fccaf710SEric Dumazet 		for_each_possible_cpu(i) {
481fccaf710SEric Dumazet 			p = per_cpu_ptr(vlan->rx_stats, i);
482fccaf710SEric Dumazet 			rx.rx_packets += p->rx_packets;
483fccaf710SEric Dumazet 			rx.rx_bytes   += p->rx_bytes;
484fccaf710SEric Dumazet 			rx.rx_errors  += p->rx_errors;
485fccaf710SEric Dumazet 			rx.multicast  += p->multicast;
486fccaf710SEric Dumazet 		}
487fccaf710SEric Dumazet 		stats->rx_packets = rx.rx_packets;
488fccaf710SEric Dumazet 		stats->rx_bytes   = rx.rx_bytes;
489fccaf710SEric Dumazet 		stats->rx_errors  = rx.rx_errors;
490fccaf710SEric Dumazet 		stats->rx_dropped = rx.rx_errors;
491fccaf710SEric Dumazet 		stats->multicast  = rx.multicast;
492fccaf710SEric Dumazet 	}
493fccaf710SEric Dumazet 	return stats;
494fccaf710SEric Dumazet }
495fccaf710SEric Dumazet 
496b863ceb7SPatrick McHardy static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
497b863ceb7SPatrick McHardy 					struct ethtool_drvinfo *drvinfo)
498b863ceb7SPatrick McHardy {
499b863ceb7SPatrick McHardy 	snprintf(drvinfo->driver, 32, "macvlan");
500b863ceb7SPatrick McHardy 	snprintf(drvinfo->version, 32, "0.1");
501b863ceb7SPatrick McHardy }
502b863ceb7SPatrick McHardy 
503b863ceb7SPatrick McHardy static u32 macvlan_ethtool_get_rx_csum(struct net_device *dev)
504b863ceb7SPatrick McHardy {
505b863ceb7SPatrick McHardy 	const struct macvlan_dev *vlan = netdev_priv(dev);
506b1b67dd4SPatrick McHardy 	return dev_ethtool_get_rx_csum(vlan->lowerdev);
507b863ceb7SPatrick McHardy }
508b863ceb7SPatrick McHardy 
5099edb8bb6SStephen Hemminger static int macvlan_ethtool_get_settings(struct net_device *dev,
5109edb8bb6SStephen Hemminger 					struct ethtool_cmd *cmd)
5119edb8bb6SStephen Hemminger {
5129edb8bb6SStephen Hemminger 	const struct macvlan_dev *vlan = netdev_priv(dev);
513b1b67dd4SPatrick McHardy 	return dev_ethtool_get_settings(vlan->lowerdev, cmd);
5149edb8bb6SStephen Hemminger }
5159edb8bb6SStephen Hemminger 
5169edb8bb6SStephen Hemminger static u32 macvlan_ethtool_get_flags(struct net_device *dev)
5179edb8bb6SStephen Hemminger {
5189edb8bb6SStephen Hemminger 	const struct macvlan_dev *vlan = netdev_priv(dev);
519b1b67dd4SPatrick McHardy 	return dev_ethtool_get_flags(vlan->lowerdev);
5209edb8bb6SStephen Hemminger }
5219edb8bb6SStephen Hemminger 
522b863ceb7SPatrick McHardy static const struct ethtool_ops macvlan_ethtool_ops = {
523b863ceb7SPatrick McHardy 	.get_link		= ethtool_op_get_link,
5249edb8bb6SStephen Hemminger 	.get_settings		= macvlan_ethtool_get_settings,
525b863ceb7SPatrick McHardy 	.get_rx_csum		= macvlan_ethtool_get_rx_csum,
526b863ceb7SPatrick McHardy 	.get_drvinfo		= macvlan_ethtool_get_drvinfo,
5279edb8bb6SStephen Hemminger 	.get_flags		= macvlan_ethtool_get_flags,
528b863ceb7SPatrick McHardy };
529b863ceb7SPatrick McHardy 
53054a30c97SStephen Hemminger static const struct net_device_ops macvlan_netdev_ops = {
53154a30c97SStephen Hemminger 	.ndo_init		= macvlan_init,
532fccaf710SEric Dumazet 	.ndo_uninit		= macvlan_uninit,
53354a30c97SStephen Hemminger 	.ndo_open		= macvlan_open,
53454a30c97SStephen Hemminger 	.ndo_stop		= macvlan_stop,
53500829823SStephen Hemminger 	.ndo_start_xmit		= macvlan_start_xmit,
53654a30c97SStephen Hemminger 	.ndo_change_mtu		= macvlan_change_mtu,
53754a30c97SStephen Hemminger 	.ndo_change_rx_flags	= macvlan_change_rx_flags,
53854a30c97SStephen Hemminger 	.ndo_set_mac_address	= macvlan_set_mac_address,
53954a30c97SStephen Hemminger 	.ndo_set_multicast_list	= macvlan_set_multicast_list,
540fccaf710SEric Dumazet 	.ndo_get_stats		= macvlan_dev_get_stats,
54154a30c97SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
54254a30c97SStephen Hemminger };
54354a30c97SStephen Hemminger 
544b863ceb7SPatrick McHardy static void macvlan_setup(struct net_device *dev)
545b863ceb7SPatrick McHardy {
546b863ceb7SPatrick McHardy 	ether_setup(dev);
547b863ceb7SPatrick McHardy 
54893f154b5SEric Dumazet 	dev->priv_flags	       &= ~IFF_XMIT_DST_RELEASE;
54954a30c97SStephen Hemminger 	dev->netdev_ops		= &macvlan_netdev_ops;
550b863ceb7SPatrick McHardy 	dev->destructor		= free_netdev;
5513b04dddeSStephen Hemminger 	dev->header_ops		= &macvlan_hard_header_ops,
552b863ceb7SPatrick McHardy 	dev->ethtool_ops	= &macvlan_ethtool_ops;
553b863ceb7SPatrick McHardy 	dev->tx_queue_len	= 0;
554b863ceb7SPatrick McHardy }
555b863ceb7SPatrick McHardy 
556b863ceb7SPatrick McHardy static int macvlan_port_create(struct net_device *dev)
557b863ceb7SPatrick McHardy {
558b863ceb7SPatrick McHardy 	struct macvlan_port *port;
559b863ceb7SPatrick McHardy 	unsigned int i;
560b863ceb7SPatrick McHardy 
561b863ceb7SPatrick McHardy 	if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
562b863ceb7SPatrick McHardy 		return -EINVAL;
563b863ceb7SPatrick McHardy 
564b863ceb7SPatrick McHardy 	port = kzalloc(sizeof(*port), GFP_KERNEL);
565b863ceb7SPatrick McHardy 	if (port == NULL)
566b863ceb7SPatrick McHardy 		return -ENOMEM;
567b863ceb7SPatrick McHardy 
568b863ceb7SPatrick McHardy 	port->dev = dev;
569b863ceb7SPatrick McHardy 	INIT_LIST_HEAD(&port->vlans);
570b863ceb7SPatrick McHardy 	for (i = 0; i < MACVLAN_HASH_SIZE; i++)
571b863ceb7SPatrick McHardy 		INIT_HLIST_HEAD(&port->vlan_hash[i]);
572b863ceb7SPatrick McHardy 	rcu_assign_pointer(dev->macvlan_port, port);
573b863ceb7SPatrick McHardy 	return 0;
574b863ceb7SPatrick McHardy }
575b863ceb7SPatrick McHardy 
576b863ceb7SPatrick McHardy static void macvlan_port_destroy(struct net_device *dev)
577b863ceb7SPatrick McHardy {
578b863ceb7SPatrick McHardy 	struct macvlan_port *port = dev->macvlan_port;
579b863ceb7SPatrick McHardy 
580b863ceb7SPatrick McHardy 	rcu_assign_pointer(dev->macvlan_port, NULL);
581b863ceb7SPatrick McHardy 	synchronize_rcu();
582b863ceb7SPatrick McHardy 	kfree(port);
583b863ceb7SPatrick McHardy }
584b863ceb7SPatrick McHardy 
585b863ceb7SPatrick McHardy static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
586b863ceb7SPatrick McHardy {
587b863ceb7SPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
588b863ceb7SPatrick McHardy 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
589b863ceb7SPatrick McHardy 			return -EINVAL;
590b863ceb7SPatrick McHardy 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
591b863ceb7SPatrick McHardy 			return -EADDRNOTAVAIL;
592b863ceb7SPatrick McHardy 	}
59327c0b1a8SArnd Bergmann 
59427c0b1a8SArnd Bergmann 	if (data && data[IFLA_MACVLAN_MODE]) {
59527c0b1a8SArnd Bergmann 		switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) {
59627c0b1a8SArnd Bergmann 		case MACVLAN_MODE_PRIVATE:
59727c0b1a8SArnd Bergmann 		case MACVLAN_MODE_VEPA:
59827c0b1a8SArnd Bergmann 		case MACVLAN_MODE_BRIDGE:
59927c0b1a8SArnd Bergmann 			break;
60027c0b1a8SArnd Bergmann 		default:
60127c0b1a8SArnd Bergmann 			return -EINVAL;
60227c0b1a8SArnd Bergmann 		}
60327c0b1a8SArnd Bergmann 	}
604b863ceb7SPatrick McHardy 	return 0;
605b863ceb7SPatrick McHardy }
606b863ceb7SPatrick McHardy 
6072c114553SEric Dumazet static int macvlan_get_tx_queues(struct net *net,
6082c114553SEric Dumazet 				 struct nlattr *tb[],
6092c114553SEric Dumazet 				 unsigned int *num_tx_queues,
6102c114553SEric Dumazet 				 unsigned int *real_num_tx_queues)
6112c114553SEric Dumazet {
6122c114553SEric Dumazet 	struct net_device *real_dev;
6132c114553SEric Dumazet 
6142c114553SEric Dumazet 	if (!tb[IFLA_LINK])
6152c114553SEric Dumazet 		return -EINVAL;
6162c114553SEric Dumazet 
6172c114553SEric Dumazet 	real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
6182c114553SEric Dumazet 	if (!real_dev)
6192c114553SEric Dumazet 		return -ENODEV;
6202c114553SEric Dumazet 
6212c114553SEric Dumazet 	*num_tx_queues      = real_dev->num_tx_queues;
6222c114553SEric Dumazet 	*real_num_tx_queues = real_dev->real_num_tx_queues;
6232c114553SEric Dumazet 	return 0;
6242c114553SEric Dumazet }
6252c114553SEric Dumazet 
62681adee47SEric W. Biederman static int macvlan_newlink(struct net *src_net, struct net_device *dev,
627b863ceb7SPatrick McHardy 			   struct nlattr *tb[], struct nlattr *data[])
628b863ceb7SPatrick McHardy {
629b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
630b863ceb7SPatrick McHardy 	struct macvlan_port *port;
631b863ceb7SPatrick McHardy 	struct net_device *lowerdev;
632b863ceb7SPatrick McHardy 	int err;
633b863ceb7SPatrick McHardy 
634b863ceb7SPatrick McHardy 	if (!tb[IFLA_LINK])
635b863ceb7SPatrick McHardy 		return -EINVAL;
636b863ceb7SPatrick McHardy 
63781adee47SEric W. Biederman 	lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
638b863ceb7SPatrick McHardy 	if (lowerdev == NULL)
639b863ceb7SPatrick McHardy 		return -ENODEV;
640b863ceb7SPatrick McHardy 
641b0832a29SEric Biederman 	/* When creating macvlans on top of other macvlans - use
642b0832a29SEric Biederman 	 * the real device as the lowerdev.
643a6ca5f1dSPatrick McHardy 	 */
644b0832a29SEric Biederman 	if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) {
645b0832a29SEric Biederman 		struct macvlan_dev *lowervlan = netdev_priv(lowerdev);
646b0832a29SEric Biederman 		lowerdev = lowervlan->lowerdev;
647b0832a29SEric Biederman 	}
648a6ca5f1dSPatrick McHardy 
649b863ceb7SPatrick McHardy 	if (!tb[IFLA_MTU])
650b863ceb7SPatrick McHardy 		dev->mtu = lowerdev->mtu;
651b863ceb7SPatrick McHardy 	else if (dev->mtu > lowerdev->mtu)
652b863ceb7SPatrick McHardy 		return -EINVAL;
653b863ceb7SPatrick McHardy 
654b863ceb7SPatrick McHardy 	if (!tb[IFLA_ADDRESS])
655b863ceb7SPatrick McHardy 		random_ether_addr(dev->dev_addr);
656b863ceb7SPatrick McHardy 
657b863ceb7SPatrick McHardy 	if (lowerdev->macvlan_port == NULL) {
658b863ceb7SPatrick McHardy 		err = macvlan_port_create(lowerdev);
659b863ceb7SPatrick McHardy 		if (err < 0)
660b863ceb7SPatrick McHardy 			return err;
661b863ceb7SPatrick McHardy 	}
662b863ceb7SPatrick McHardy 	port = lowerdev->macvlan_port;
663b863ceb7SPatrick McHardy 
664b863ceb7SPatrick McHardy 	vlan->lowerdev = lowerdev;
665b863ceb7SPatrick McHardy 	vlan->dev      = dev;
666b863ceb7SPatrick McHardy 	vlan->port     = port;
667b863ceb7SPatrick McHardy 
66827c0b1a8SArnd Bergmann 	vlan->mode     = MACVLAN_MODE_VEPA;
66927c0b1a8SArnd Bergmann 	if (data && data[IFLA_MACVLAN_MODE])
67027c0b1a8SArnd Bergmann 		vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
67127c0b1a8SArnd Bergmann 
672b863ceb7SPatrick McHardy 	err = register_netdevice(dev);
673b863ceb7SPatrick McHardy 	if (err < 0)
674b863ceb7SPatrick McHardy 		return err;
675b863ceb7SPatrick McHardy 
676b863ceb7SPatrick McHardy 	list_add_tail(&vlan->list, &port->vlans);
677fc4a7489SPatrick Mullaney 	netif_stacked_transfer_operstate(lowerdev, dev);
678b863ceb7SPatrick McHardy 	return 0;
679b863ceb7SPatrick McHardy }
680b863ceb7SPatrick McHardy 
68123289a37SEric Dumazet static void macvlan_dellink(struct net_device *dev, struct list_head *head)
682b863ceb7SPatrick McHardy {
683b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan = netdev_priv(dev);
684b863ceb7SPatrick McHardy 	struct macvlan_port *port = vlan->port;
685b863ceb7SPatrick McHardy 
686b863ceb7SPatrick McHardy 	list_del(&vlan->list);
68723289a37SEric Dumazet 	unregister_netdevice_queue(dev, head);
688b863ceb7SPatrick McHardy 
689b863ceb7SPatrick McHardy 	if (list_empty(&port->vlans))
69073120964SPatrick McHardy 		macvlan_port_destroy(port->dev);
691b863ceb7SPatrick McHardy }
692b863ceb7SPatrick McHardy 
69327c0b1a8SArnd Bergmann static int macvlan_changelink(struct net_device *dev,
69427c0b1a8SArnd Bergmann 		struct nlattr *tb[], struct nlattr *data[])
69527c0b1a8SArnd Bergmann {
69627c0b1a8SArnd Bergmann 	struct macvlan_dev *vlan = netdev_priv(dev);
69727c0b1a8SArnd Bergmann 	if (data && data[IFLA_MACVLAN_MODE])
69827c0b1a8SArnd Bergmann 		vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
69927c0b1a8SArnd Bergmann 	return 0;
70027c0b1a8SArnd Bergmann }
70127c0b1a8SArnd Bergmann 
70227c0b1a8SArnd Bergmann static size_t macvlan_get_size(const struct net_device *dev)
70327c0b1a8SArnd Bergmann {
70427c0b1a8SArnd Bergmann 	return nla_total_size(4);
70527c0b1a8SArnd Bergmann }
70627c0b1a8SArnd Bergmann 
70727c0b1a8SArnd Bergmann static int macvlan_fill_info(struct sk_buff *skb,
70827c0b1a8SArnd Bergmann 				const struct net_device *dev)
70927c0b1a8SArnd Bergmann {
71027c0b1a8SArnd Bergmann 	struct macvlan_dev *vlan = netdev_priv(dev);
71127c0b1a8SArnd Bergmann 
71227c0b1a8SArnd Bergmann 	NLA_PUT_U32(skb, IFLA_MACVLAN_MODE, vlan->mode);
71327c0b1a8SArnd Bergmann 	return 0;
71427c0b1a8SArnd Bergmann 
71527c0b1a8SArnd Bergmann nla_put_failure:
71627c0b1a8SArnd Bergmann 	return -EMSGSIZE;
71727c0b1a8SArnd Bergmann }
71827c0b1a8SArnd Bergmann 
71927c0b1a8SArnd Bergmann static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = {
72027c0b1a8SArnd Bergmann 	[IFLA_MACVLAN_MODE] = { .type = NLA_U32 },
72127c0b1a8SArnd Bergmann };
72227c0b1a8SArnd Bergmann 
723b863ceb7SPatrick McHardy static struct rtnl_link_ops macvlan_link_ops __read_mostly = {
724b863ceb7SPatrick McHardy 	.kind		= "macvlan",
725b863ceb7SPatrick McHardy 	.priv_size	= sizeof(struct macvlan_dev),
7262c114553SEric Dumazet 	.get_tx_queues  = macvlan_get_tx_queues,
727b863ceb7SPatrick McHardy 	.setup		= macvlan_setup,
728b863ceb7SPatrick McHardy 	.validate	= macvlan_validate,
729b863ceb7SPatrick McHardy 	.newlink	= macvlan_newlink,
730b863ceb7SPatrick McHardy 	.dellink	= macvlan_dellink,
73127c0b1a8SArnd Bergmann 	.maxtype	= IFLA_MACVLAN_MAX,
73227c0b1a8SArnd Bergmann 	.policy		= macvlan_policy,
73327c0b1a8SArnd Bergmann 	.changelink	= macvlan_changelink,
73427c0b1a8SArnd Bergmann 	.get_size	= macvlan_get_size,
73527c0b1a8SArnd Bergmann 	.fill_info	= macvlan_fill_info,
736b863ceb7SPatrick McHardy };
737b863ceb7SPatrick McHardy 
738b863ceb7SPatrick McHardy static int macvlan_device_event(struct notifier_block *unused,
739b863ceb7SPatrick McHardy 				unsigned long event, void *ptr)
740b863ceb7SPatrick McHardy {
741b863ceb7SPatrick McHardy 	struct net_device *dev = ptr;
742b863ceb7SPatrick McHardy 	struct macvlan_dev *vlan, *next;
743b863ceb7SPatrick McHardy 	struct macvlan_port *port;
744b863ceb7SPatrick McHardy 
745b863ceb7SPatrick McHardy 	port = dev->macvlan_port;
746b863ceb7SPatrick McHardy 	if (port == NULL)
747b863ceb7SPatrick McHardy 		return NOTIFY_DONE;
748b863ceb7SPatrick McHardy 
749b863ceb7SPatrick McHardy 	switch (event) {
750b863ceb7SPatrick McHardy 	case NETDEV_CHANGE:
751b863ceb7SPatrick McHardy 		list_for_each_entry(vlan, &port->vlans, list)
752fc4a7489SPatrick Mullaney 			netif_stacked_transfer_operstate(vlan->lowerdev,
753fc4a7489SPatrick Mullaney 							 vlan->dev);
754b863ceb7SPatrick McHardy 		break;
755b863ceb7SPatrick McHardy 	case NETDEV_FEAT_CHANGE:
756b863ceb7SPatrick McHardy 		list_for_each_entry(vlan, &port->vlans, list) {
757b863ceb7SPatrick McHardy 			vlan->dev->features = dev->features & MACVLAN_FEATURES;
7588c2acc53SPatrick McHardy 			vlan->dev->gso_max_size = dev->gso_max_size;
759b863ceb7SPatrick McHardy 			netdev_features_change(vlan->dev);
760b863ceb7SPatrick McHardy 		}
761b863ceb7SPatrick McHardy 		break;
762b863ceb7SPatrick McHardy 	case NETDEV_UNREGISTER:
763b863ceb7SPatrick McHardy 		list_for_each_entry_safe(vlan, next, &port->vlans, list)
76423289a37SEric Dumazet 			macvlan_dellink(vlan->dev, NULL);
765b863ceb7SPatrick McHardy 		break;
766b863ceb7SPatrick McHardy 	}
767b863ceb7SPatrick McHardy 	return NOTIFY_DONE;
768b863ceb7SPatrick McHardy }
769b863ceb7SPatrick McHardy 
770b863ceb7SPatrick McHardy static struct notifier_block macvlan_notifier_block __read_mostly = {
771b863ceb7SPatrick McHardy 	.notifier_call	= macvlan_device_event,
772b863ceb7SPatrick McHardy };
773b863ceb7SPatrick McHardy 
774b863ceb7SPatrick McHardy static int __init macvlan_init_module(void)
775b863ceb7SPatrick McHardy {
776b863ceb7SPatrick McHardy 	int err;
777b863ceb7SPatrick McHardy 
778b863ceb7SPatrick McHardy 	register_netdevice_notifier(&macvlan_notifier_block);
779b863ceb7SPatrick McHardy 	macvlan_handle_frame_hook = macvlan_handle_frame;
780b863ceb7SPatrick McHardy 
781b863ceb7SPatrick McHardy 	err = rtnl_link_register(&macvlan_link_ops);
782b863ceb7SPatrick McHardy 	if (err < 0)
783b863ceb7SPatrick McHardy 		goto err1;
784b863ceb7SPatrick McHardy 	return 0;
785b863ceb7SPatrick McHardy err1:
78652913246SRami Rosen 	macvlan_handle_frame_hook = NULL;
787b863ceb7SPatrick McHardy 	unregister_netdevice_notifier(&macvlan_notifier_block);
788b863ceb7SPatrick McHardy 	return err;
789b863ceb7SPatrick McHardy }
790b863ceb7SPatrick McHardy 
791b863ceb7SPatrick McHardy static void __exit macvlan_cleanup_module(void)
792b863ceb7SPatrick McHardy {
793b863ceb7SPatrick McHardy 	rtnl_link_unregister(&macvlan_link_ops);
794b863ceb7SPatrick McHardy 	macvlan_handle_frame_hook = NULL;
795b863ceb7SPatrick McHardy 	unregister_netdevice_notifier(&macvlan_notifier_block);
796b863ceb7SPatrick McHardy }
797b863ceb7SPatrick McHardy 
798b863ceb7SPatrick McHardy module_init(macvlan_init_module);
799b863ceb7SPatrick McHardy module_exit(macvlan_cleanup_module);
800b863ceb7SPatrick McHardy 
801b863ceb7SPatrick McHardy MODULE_LICENSE("GPL");
802b863ceb7SPatrick McHardy MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
803b863ceb7SPatrick McHardy MODULE_DESCRIPTION("Driver for MAC address based VLANs");
804b863ceb7SPatrick McHardy MODULE_ALIAS_RTNL_LINK("macvlan");
805