xref: /linux/drivers/net/macvlan.c (revision 9410645520e9b820069761f3450ef6661418e279)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2007 Patrick McHardy <kaber@trash.net>
4  *
5  * The code this is based on carried the following copyright notice:
6  * ---
7  * (C) Copyright 2001-2006
8  * Alex Zeffertt, Cambridge Broadband Ltd, ajz@cambridgebroadband.com
9  * Re-worked by Ben Greear <greearb@candelatech.com>
10  * ---
11  */
12 #include <linux/kernel.h>
13 #include <linux/types.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/errno.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/rculist.h>
20 #include <linux/notifier.h>
21 #include <linux/netdevice.h>
22 #include <linux/etherdevice.h>
23 #include <linux/net_tstamp.h>
24 #include <linux/ethtool.h>
25 #include <linux/if_arp.h>
26 #include <linux/if_vlan.h>
27 #include <linux/if_link.h>
28 #include <linux/if_macvlan.h>
29 #include <linux/hash.h>
30 #include <linux/workqueue.h>
31 #include <net/rtnetlink.h>
32 #include <net/xfrm.h>
33 #include <linux/netpoll.h>
34 #include <linux/phy.h>
35 
36 #define MACVLAN_HASH_BITS	8
37 #define MACVLAN_HASH_SIZE	(1<<MACVLAN_HASH_BITS)
38 #define MACVLAN_DEFAULT_BC_QUEUE_LEN	1000
39 
40 #define MACVLAN_F_PASSTHRU	1
41 #define MACVLAN_F_ADDRCHANGE	2
42 
43 struct macvlan_port {
44 	struct net_device	*dev;
45 	struct hlist_head	vlan_hash[MACVLAN_HASH_SIZE];
46 	struct list_head	vlans;
47 	struct sk_buff_head	bc_queue;
48 	struct work_struct	bc_work;
49 	u32			bc_queue_len_used;
50 	int			bc_cutoff;
51 	u32			flags;
52 	int			count;
53 	struct hlist_head	vlan_source_hash[MACVLAN_HASH_SIZE];
54 	DECLARE_BITMAP(bc_filter, MACVLAN_MC_FILTER_SZ);
55 	DECLARE_BITMAP(mc_filter, MACVLAN_MC_FILTER_SZ);
56 	unsigned char           perm_addr[ETH_ALEN];
57 };
58 
59 struct macvlan_source_entry {
60 	struct hlist_node	hlist;
61 	struct macvlan_dev	*vlan;
62 	unsigned char		addr[6+2] __aligned(sizeof(u16));
63 	struct rcu_head		rcu;
64 };
65 
66 struct macvlan_skb_cb {
67 	const struct macvlan_dev *src;
68 };
69 
70 #define MACVLAN_SKB_CB(__skb) ((struct macvlan_skb_cb *)&((__skb)->cb[0]))
71 
72 static void macvlan_port_destroy(struct net_device *dev);
73 static void update_port_bc_queue_len(struct macvlan_port *port);
74 
macvlan_passthru(const struct macvlan_port * port)75 static inline bool macvlan_passthru(const struct macvlan_port *port)
76 {
77 	return port->flags & MACVLAN_F_PASSTHRU;
78 }
79 
macvlan_set_passthru(struct macvlan_port * port)80 static inline void macvlan_set_passthru(struct macvlan_port *port)
81 {
82 	port->flags |= MACVLAN_F_PASSTHRU;
83 }
84 
macvlan_addr_change(const struct macvlan_port * port)85 static inline bool macvlan_addr_change(const struct macvlan_port *port)
86 {
87 	return port->flags & MACVLAN_F_ADDRCHANGE;
88 }
89 
macvlan_set_addr_change(struct macvlan_port * port)90 static inline void macvlan_set_addr_change(struct macvlan_port *port)
91 {
92 	port->flags |= MACVLAN_F_ADDRCHANGE;
93 }
94 
macvlan_clear_addr_change(struct macvlan_port * port)95 static inline void macvlan_clear_addr_change(struct macvlan_port *port)
96 {
97 	port->flags &= ~MACVLAN_F_ADDRCHANGE;
98 }
99 
100 /* Hash Ethernet address */
macvlan_eth_hash(const unsigned char * addr)101 static u32 macvlan_eth_hash(const unsigned char *addr)
102 {
103 	u64 value = get_unaligned((u64 *)addr);
104 
105 	/* only want 6 bytes */
106 #ifdef __BIG_ENDIAN
107 	value >>= 16;
108 #else
109 	value <<= 16;
110 #endif
111 	return hash_64(value, MACVLAN_HASH_BITS);
112 }
113 
macvlan_port_get_rcu(const struct net_device * dev)114 static struct macvlan_port *macvlan_port_get_rcu(const struct net_device *dev)
115 {
116 	return rcu_dereference(dev->rx_handler_data);
117 }
118 
macvlan_port_get_rtnl(const struct net_device * dev)119 static struct macvlan_port *macvlan_port_get_rtnl(const struct net_device *dev)
120 {
121 	return rtnl_dereference(dev->rx_handler_data);
122 }
123 
macvlan_hash_lookup(const struct macvlan_port * port,const unsigned char * addr)124 static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
125 					       const unsigned char *addr)
126 {
127 	struct macvlan_dev *vlan;
128 	u32 idx = macvlan_eth_hash(addr);
129 
130 	hlist_for_each_entry_rcu(vlan, &port->vlan_hash[idx], hlist,
131 				 lockdep_rtnl_is_held()) {
132 		if (ether_addr_equal_64bits(vlan->dev->dev_addr, addr))
133 			return vlan;
134 	}
135 	return NULL;
136 }
137 
macvlan_hash_lookup_source(const struct macvlan_dev * vlan,const unsigned char * addr)138 static struct macvlan_source_entry *macvlan_hash_lookup_source(
139 	const struct macvlan_dev *vlan,
140 	const unsigned char *addr)
141 {
142 	struct macvlan_source_entry *entry;
143 	u32 idx = macvlan_eth_hash(addr);
144 	struct hlist_head *h = &vlan->port->vlan_source_hash[idx];
145 
146 	hlist_for_each_entry_rcu(entry, h, hlist, lockdep_rtnl_is_held()) {
147 		if (ether_addr_equal_64bits(entry->addr, addr) &&
148 		    entry->vlan == vlan)
149 			return entry;
150 	}
151 	return NULL;
152 }
153 
macvlan_hash_add_source(struct macvlan_dev * vlan,const unsigned char * addr)154 static int macvlan_hash_add_source(struct macvlan_dev *vlan,
155 				   const unsigned char *addr)
156 {
157 	struct macvlan_port *port = vlan->port;
158 	struct macvlan_source_entry *entry;
159 	struct hlist_head *h;
160 
161 	entry = macvlan_hash_lookup_source(vlan, addr);
162 	if (entry)
163 		return 0;
164 
165 	entry = kmalloc(sizeof(*entry), GFP_KERNEL);
166 	if (!entry)
167 		return -ENOMEM;
168 
169 	ether_addr_copy(entry->addr, addr);
170 	entry->vlan = vlan;
171 	h = &port->vlan_source_hash[macvlan_eth_hash(addr)];
172 	hlist_add_head_rcu(&entry->hlist, h);
173 	vlan->macaddr_count++;
174 
175 	return 0;
176 }
177 
macvlan_hash_add(struct macvlan_dev * vlan)178 static void macvlan_hash_add(struct macvlan_dev *vlan)
179 {
180 	struct macvlan_port *port = vlan->port;
181 	const unsigned char *addr = vlan->dev->dev_addr;
182 	u32 idx = macvlan_eth_hash(addr);
183 
184 	hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[idx]);
185 }
186 
macvlan_hash_del_source(struct macvlan_source_entry * entry)187 static void macvlan_hash_del_source(struct macvlan_source_entry *entry)
188 {
189 	hlist_del_rcu(&entry->hlist);
190 	kfree_rcu(entry, rcu);
191 }
192 
macvlan_hash_del(struct macvlan_dev * vlan,bool sync)193 static void macvlan_hash_del(struct macvlan_dev *vlan, bool sync)
194 {
195 	hlist_del_rcu(&vlan->hlist);
196 	if (sync)
197 		synchronize_rcu();
198 }
199 
macvlan_hash_change_addr(struct macvlan_dev * vlan,const unsigned char * addr)200 static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
201 					const unsigned char *addr)
202 {
203 	macvlan_hash_del(vlan, true);
204 	/* Now that we are unhashed it is safe to change the device
205 	 * address without confusing packet delivery.
206 	 */
207 	eth_hw_addr_set(vlan->dev, addr);
208 	macvlan_hash_add(vlan);
209 }
210 
macvlan_addr_busy(const struct macvlan_port * port,const unsigned char * addr)211 static bool macvlan_addr_busy(const struct macvlan_port *port,
212 			      const unsigned char *addr)
213 {
214 	/* Test to see if the specified address is
215 	 * currently in use by the underlying device or
216 	 * another macvlan.
217 	 */
218 	if (!macvlan_passthru(port) && !macvlan_addr_change(port) &&
219 	    ether_addr_equal_64bits(port->dev->dev_addr, addr))
220 		return true;
221 
222 	if (macvlan_hash_lookup(port, addr))
223 		return true;
224 
225 	return false;
226 }
227 
228 
macvlan_broadcast_one(struct sk_buff * skb,const struct macvlan_dev * vlan,const struct ethhdr * eth,bool local)229 static int macvlan_broadcast_one(struct sk_buff *skb,
230 				 const struct macvlan_dev *vlan,
231 				 const struct ethhdr *eth, bool local)
232 {
233 	struct net_device *dev = vlan->dev;
234 
235 	if (local)
236 		return __dev_forward_skb(dev, skb);
237 
238 	skb->dev = dev;
239 	if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast))
240 		skb->pkt_type = PACKET_BROADCAST;
241 	else
242 		skb->pkt_type = PACKET_MULTICAST;
243 
244 	return 0;
245 }
246 
macvlan_hash_mix(const struct macvlan_dev * vlan)247 static u32 macvlan_hash_mix(const struct macvlan_dev *vlan)
248 {
249 	return (u32)(((unsigned long)vlan) >> L1_CACHE_SHIFT);
250 }
251 
252 
mc_hash(const struct macvlan_dev * vlan,const unsigned char * addr)253 static unsigned int mc_hash(const struct macvlan_dev *vlan,
254 			    const unsigned char *addr)
255 {
256 	u32 val = __get_unaligned_cpu32(addr + 2);
257 
258 	val ^= macvlan_hash_mix(vlan);
259 	return hash_32(val, MACVLAN_MC_FILTER_BITS);
260 }
261 
macvlan_broadcast(struct sk_buff * skb,const struct macvlan_port * port,struct net_device * src,enum macvlan_mode mode)262 static void macvlan_broadcast(struct sk_buff *skb,
263 			      const struct macvlan_port *port,
264 			      struct net_device *src,
265 			      enum macvlan_mode mode)
266 {
267 	const struct ethhdr *eth = eth_hdr(skb);
268 	const struct macvlan_dev *vlan;
269 	struct sk_buff *nskb;
270 	unsigned int i;
271 	int err;
272 	unsigned int hash;
273 
274 	if (skb->protocol == htons(ETH_P_PAUSE))
275 		return;
276 
277 	hash_for_each_rcu(port->vlan_hash, i, vlan, hlist) {
278 		if (vlan->dev == src || !(vlan->mode & mode))
279 			continue;
280 
281 		hash = mc_hash(vlan, eth->h_dest);
282 		if (!test_bit(hash, vlan->mc_filter))
283 			continue;
284 
285 		err = NET_RX_DROP;
286 		nskb = skb_clone(skb, GFP_ATOMIC);
287 		if (likely(nskb))
288 			err = macvlan_broadcast_one(nskb, vlan, eth,
289 					mode == MACVLAN_MODE_BRIDGE) ?:
290 			      netif_rx(nskb);
291 		macvlan_count_rx(vlan, skb->len + ETH_HLEN,
292 				 err == NET_RX_SUCCESS, true);
293 	}
294 }
295 
macvlan_multicast_rx(const struct macvlan_port * port,const struct macvlan_dev * src,struct sk_buff * skb)296 static void macvlan_multicast_rx(const struct macvlan_port *port,
297 				 const struct macvlan_dev *src,
298 				 struct sk_buff *skb)
299 {
300 	if (!src)
301 		/* frame comes from an external address */
302 		macvlan_broadcast(skb, port, NULL,
303 				  MACVLAN_MODE_PRIVATE |
304 				  MACVLAN_MODE_VEPA    |
305 				  MACVLAN_MODE_PASSTHRU|
306 				  MACVLAN_MODE_BRIDGE);
307 	else if (src->mode == MACVLAN_MODE_VEPA)
308 		/* flood to everyone except source */
309 		macvlan_broadcast(skb, port, src->dev,
310 				  MACVLAN_MODE_VEPA |
311 				  MACVLAN_MODE_BRIDGE);
312 	else
313 		/*
314 		 * flood only to VEPA ports, bridge ports
315 		 * already saw the frame on the way out.
316 		 */
317 		macvlan_broadcast(skb, port, src->dev,
318 				  MACVLAN_MODE_VEPA);
319 }
320 
macvlan_process_broadcast(struct work_struct * w)321 static void macvlan_process_broadcast(struct work_struct *w)
322 {
323 	struct macvlan_port *port = container_of(w, struct macvlan_port,
324 						 bc_work);
325 	struct sk_buff *skb;
326 	struct sk_buff_head list;
327 
328 	__skb_queue_head_init(&list);
329 
330 	spin_lock_bh(&port->bc_queue.lock);
331 	skb_queue_splice_tail_init(&port->bc_queue, &list);
332 	spin_unlock_bh(&port->bc_queue.lock);
333 
334 	while ((skb = __skb_dequeue(&list))) {
335 		const struct macvlan_dev *src = MACVLAN_SKB_CB(skb)->src;
336 
337 		rcu_read_lock();
338 		macvlan_multicast_rx(port, src, skb);
339 		rcu_read_unlock();
340 
341 		if (src)
342 			dev_put(src->dev);
343 		consume_skb(skb);
344 
345 		cond_resched();
346 	}
347 }
348 
macvlan_broadcast_enqueue(struct macvlan_port * port,const struct macvlan_dev * src,struct sk_buff * skb)349 static void macvlan_broadcast_enqueue(struct macvlan_port *port,
350 				      const struct macvlan_dev *src,
351 				      struct sk_buff *skb)
352 {
353 	struct sk_buff *nskb;
354 	int err = -ENOMEM;
355 
356 	nskb = skb_clone(skb, GFP_ATOMIC);
357 	if (!nskb)
358 		goto err;
359 
360 	MACVLAN_SKB_CB(nskb)->src = src;
361 
362 	spin_lock(&port->bc_queue.lock);
363 	if (skb_queue_len(&port->bc_queue) < port->bc_queue_len_used) {
364 		if (src)
365 			dev_hold(src->dev);
366 		__skb_queue_tail(&port->bc_queue, nskb);
367 		err = 0;
368 	}
369 	spin_unlock(&port->bc_queue.lock);
370 
371 	queue_work(system_unbound_wq, &port->bc_work);
372 
373 	if (err)
374 		goto free_nskb;
375 
376 	return;
377 
378 free_nskb:
379 	kfree_skb(nskb);
380 err:
381 	dev_core_stats_rx_dropped_inc(skb->dev);
382 }
383 
macvlan_flush_sources(struct macvlan_port * port,struct macvlan_dev * vlan)384 static void macvlan_flush_sources(struct macvlan_port *port,
385 				  struct macvlan_dev *vlan)
386 {
387 	struct macvlan_source_entry *entry;
388 	struct hlist_node *next;
389 	int i;
390 
391 	hash_for_each_safe(port->vlan_source_hash, i, next, entry, hlist)
392 		if (entry->vlan == vlan)
393 			macvlan_hash_del_source(entry);
394 
395 	vlan->macaddr_count = 0;
396 }
397 
macvlan_forward_source_one(struct sk_buff * skb,struct macvlan_dev * vlan)398 static void macvlan_forward_source_one(struct sk_buff *skb,
399 				       struct macvlan_dev *vlan)
400 {
401 	struct sk_buff *nskb;
402 	struct net_device *dev;
403 	int len;
404 	int ret;
405 
406 	dev = vlan->dev;
407 	if (unlikely(!(dev->flags & IFF_UP)))
408 		return;
409 
410 	nskb = skb_clone(skb, GFP_ATOMIC);
411 	if (!nskb)
412 		return;
413 
414 	len = nskb->len + ETH_HLEN;
415 	nskb->dev = dev;
416 
417 	if (ether_addr_equal_64bits(eth_hdr(skb)->h_dest, dev->dev_addr))
418 		nskb->pkt_type = PACKET_HOST;
419 
420 	ret = __netif_rx(nskb);
421 	macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, false);
422 }
423 
macvlan_forward_source(struct sk_buff * skb,struct macvlan_port * port,const unsigned char * addr)424 static bool macvlan_forward_source(struct sk_buff *skb,
425 				   struct macvlan_port *port,
426 				   const unsigned char *addr)
427 {
428 	struct macvlan_source_entry *entry;
429 	u32 idx = macvlan_eth_hash(addr);
430 	struct hlist_head *h = &port->vlan_source_hash[idx];
431 	bool consume = false;
432 
433 	hlist_for_each_entry_rcu(entry, h, hlist) {
434 		if (ether_addr_equal_64bits(entry->addr, addr)) {
435 			if (entry->vlan->flags & MACVLAN_FLAG_NODST)
436 				consume = true;
437 			macvlan_forward_source_one(skb, entry->vlan);
438 		}
439 	}
440 
441 	return consume;
442 }
443 
444 /* called under rcu_read_lock() from netif_receive_skb */
macvlan_handle_frame(struct sk_buff ** pskb)445 static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb)
446 {
447 	struct macvlan_port *port;
448 	struct sk_buff *skb = *pskb;
449 	const struct ethhdr *eth = eth_hdr(skb);
450 	const struct macvlan_dev *vlan;
451 	const struct macvlan_dev *src;
452 	struct net_device *dev;
453 	unsigned int len = 0;
454 	int ret;
455 	rx_handler_result_t handle_res;
456 
457 	/* Packets from dev_loopback_xmit() do not have L2 header, bail out */
458 	if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
459 		return RX_HANDLER_PASS;
460 
461 	port = macvlan_port_get_rcu(skb->dev);
462 	if (is_multicast_ether_addr(eth->h_dest)) {
463 		unsigned int hash;
464 
465 		skb = ip_check_defrag(dev_net(skb->dev), skb, IP_DEFRAG_MACVLAN);
466 		if (!skb)
467 			return RX_HANDLER_CONSUMED;
468 		*pskb = skb;
469 		eth = eth_hdr(skb);
470 		if (macvlan_forward_source(skb, port, eth->h_source)) {
471 			kfree_skb(skb);
472 			return RX_HANDLER_CONSUMED;
473 		}
474 		src = macvlan_hash_lookup(port, eth->h_source);
475 		if (src && src->mode != MACVLAN_MODE_VEPA &&
476 		    src->mode != MACVLAN_MODE_BRIDGE) {
477 			/* forward to original port. */
478 			vlan = src;
479 			ret = macvlan_broadcast_one(skb, vlan, eth, 0) ?:
480 			      __netif_rx(skb);
481 			handle_res = RX_HANDLER_CONSUMED;
482 			goto out;
483 		}
484 
485 		hash = mc_hash(NULL, eth->h_dest);
486 		if (test_bit(hash, port->bc_filter))
487 			macvlan_broadcast_enqueue(port, src, skb);
488 		else if (test_bit(hash, port->mc_filter))
489 			macvlan_multicast_rx(port, src, skb);
490 
491 		return RX_HANDLER_PASS;
492 	}
493 
494 	if (macvlan_forward_source(skb, port, eth->h_source)) {
495 		kfree_skb(skb);
496 		return RX_HANDLER_CONSUMED;
497 	}
498 	if (macvlan_passthru(port))
499 		vlan = list_first_or_null_rcu(&port->vlans,
500 					      struct macvlan_dev, list);
501 	else
502 		vlan = macvlan_hash_lookup(port, eth->h_dest);
503 	if (!vlan || vlan->mode == MACVLAN_MODE_SOURCE)
504 		return RX_HANDLER_PASS;
505 
506 	dev = vlan->dev;
507 	if (unlikely(!(dev->flags & IFF_UP))) {
508 		kfree_skb(skb);
509 		return RX_HANDLER_CONSUMED;
510 	}
511 	len = skb->len + ETH_HLEN;
512 	skb = skb_share_check(skb, GFP_ATOMIC);
513 	if (!skb) {
514 		ret = NET_RX_DROP;
515 		handle_res = RX_HANDLER_CONSUMED;
516 		goto out;
517 	}
518 
519 	*pskb = skb;
520 	skb->dev = dev;
521 	skb->pkt_type = PACKET_HOST;
522 
523 	ret = NET_RX_SUCCESS;
524 	handle_res = RX_HANDLER_ANOTHER;
525 out:
526 	macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, false);
527 	return handle_res;
528 }
529 
macvlan_queue_xmit(struct sk_buff * skb,struct net_device * dev)530 static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
531 {
532 	const struct macvlan_dev *vlan = netdev_priv(dev);
533 	const struct macvlan_port *port = vlan->port;
534 	const struct macvlan_dev *dest;
535 
536 	if (vlan->mode == MACVLAN_MODE_BRIDGE) {
537 		const struct ethhdr *eth = skb_eth_hdr(skb);
538 
539 		/* send to other bridge ports directly */
540 		if (is_multicast_ether_addr(eth->h_dest)) {
541 			skb_reset_mac_header(skb);
542 			macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE);
543 			goto xmit_world;
544 		}
545 
546 		dest = macvlan_hash_lookup(port, eth->h_dest);
547 		if (dest && dest->mode == MACVLAN_MODE_BRIDGE) {
548 			/* send to lowerdev first for its network taps */
549 			dev_forward_skb(vlan->lowerdev, skb);
550 
551 			return NET_XMIT_SUCCESS;
552 		}
553 	}
554 xmit_world:
555 	skb->dev = vlan->lowerdev;
556 	return dev_queue_xmit_accel(skb,
557 				    netdev_get_sb_channel(dev) ? dev : NULL);
558 }
559 
macvlan_netpoll_send_skb(struct macvlan_dev * vlan,struct sk_buff * skb)560 static inline netdev_tx_t macvlan_netpoll_send_skb(struct macvlan_dev *vlan, struct sk_buff *skb)
561 {
562 #ifdef CONFIG_NET_POLL_CONTROLLER
563 	return netpoll_send_skb(vlan->netpoll, skb);
564 #else
565 	BUG();
566 	return NETDEV_TX_OK;
567 #endif
568 }
569 
macvlan_start_xmit(struct sk_buff * skb,struct net_device * dev)570 static netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
571 				      struct net_device *dev)
572 {
573 	struct macvlan_dev *vlan = netdev_priv(dev);
574 	unsigned int len = skb->len;
575 	int ret;
576 
577 	if (unlikely(netpoll_tx_running(dev)))
578 		return macvlan_netpoll_send_skb(vlan, skb);
579 
580 	ret = macvlan_queue_xmit(skb, dev);
581 
582 	if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
583 		struct vlan_pcpu_stats *pcpu_stats;
584 
585 		pcpu_stats = this_cpu_ptr(vlan->pcpu_stats);
586 		u64_stats_update_begin(&pcpu_stats->syncp);
587 		u64_stats_inc(&pcpu_stats->tx_packets);
588 		u64_stats_add(&pcpu_stats->tx_bytes, len);
589 		u64_stats_update_end(&pcpu_stats->syncp);
590 	} else {
591 		this_cpu_inc(vlan->pcpu_stats->tx_dropped);
592 	}
593 	return ret;
594 }
595 
macvlan_hard_header(struct sk_buff * skb,struct net_device * dev,unsigned short type,const void * daddr,const void * saddr,unsigned len)596 static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
597 			       unsigned short type, const void *daddr,
598 			       const void *saddr, unsigned len)
599 {
600 	const struct macvlan_dev *vlan = netdev_priv(dev);
601 	struct net_device *lowerdev = vlan->lowerdev;
602 
603 	return dev_hard_header(skb, lowerdev, type, daddr,
604 			       saddr ? : dev->dev_addr, len);
605 }
606 
607 static const struct header_ops macvlan_hard_header_ops = {
608 	.create  	= macvlan_hard_header,
609 	.parse		= eth_header_parse,
610 	.cache		= eth_header_cache,
611 	.cache_update	= eth_header_cache_update,
612 	.parse_protocol	= eth_header_parse_protocol,
613 };
614 
macvlan_open(struct net_device * dev)615 static int macvlan_open(struct net_device *dev)
616 {
617 	struct macvlan_dev *vlan = netdev_priv(dev);
618 	struct net_device *lowerdev = vlan->lowerdev;
619 	int err;
620 
621 	if (macvlan_passthru(vlan->port)) {
622 		if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) {
623 			err = dev_set_promiscuity(lowerdev, 1);
624 			if (err < 0)
625 				goto out;
626 		}
627 		goto hash_add;
628 	}
629 
630 	err = -EADDRINUSE;
631 	if (macvlan_addr_busy(vlan->port, dev->dev_addr))
632 		goto out;
633 
634 	/* Attempt to populate accel_priv which is used to offload the L2
635 	 * forwarding requests for unicast packets.
636 	 */
637 	if (lowerdev->features & NETIF_F_HW_L2FW_DOFFLOAD)
638 		vlan->accel_priv =
639 		      lowerdev->netdev_ops->ndo_dfwd_add_station(lowerdev, dev);
640 
641 	/* If earlier attempt to offload failed, or accel_priv is not
642 	 * populated we must add the unicast address to the lower device.
643 	 */
644 	if (IS_ERR_OR_NULL(vlan->accel_priv)) {
645 		vlan->accel_priv = NULL;
646 		err = dev_uc_add(lowerdev, dev->dev_addr);
647 		if (err < 0)
648 			goto out;
649 	}
650 
651 	if (dev->flags & IFF_ALLMULTI) {
652 		err = dev_set_allmulti(lowerdev, 1);
653 		if (err < 0)
654 			goto del_unicast;
655 	}
656 
657 	if (dev->flags & IFF_PROMISC) {
658 		err = dev_set_promiscuity(lowerdev, 1);
659 		if (err < 0)
660 			goto clear_multi;
661 	}
662 
663 hash_add:
664 	macvlan_hash_add(vlan);
665 	return 0;
666 
667 clear_multi:
668 	if (dev->flags & IFF_ALLMULTI)
669 		dev_set_allmulti(lowerdev, -1);
670 del_unicast:
671 	if (vlan->accel_priv) {
672 		lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev,
673 							   vlan->accel_priv);
674 		vlan->accel_priv = NULL;
675 	} else {
676 		dev_uc_del(lowerdev, dev->dev_addr);
677 	}
678 out:
679 	return err;
680 }
681 
macvlan_stop(struct net_device * dev)682 static int macvlan_stop(struct net_device *dev)
683 {
684 	struct macvlan_dev *vlan = netdev_priv(dev);
685 	struct net_device *lowerdev = vlan->lowerdev;
686 
687 	if (vlan->accel_priv) {
688 		lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev,
689 							   vlan->accel_priv);
690 		vlan->accel_priv = NULL;
691 	}
692 
693 	dev_uc_unsync(lowerdev, dev);
694 	dev_mc_unsync(lowerdev, dev);
695 
696 	if (macvlan_passthru(vlan->port)) {
697 		if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC))
698 			dev_set_promiscuity(lowerdev, -1);
699 		goto hash_del;
700 	}
701 
702 	if (dev->flags & IFF_ALLMULTI)
703 		dev_set_allmulti(lowerdev, -1);
704 
705 	if (dev->flags & IFF_PROMISC)
706 		dev_set_promiscuity(lowerdev, -1);
707 
708 	dev_uc_del(lowerdev, dev->dev_addr);
709 
710 hash_del:
711 	macvlan_hash_del(vlan, !dev->dismantle);
712 	return 0;
713 }
714 
macvlan_sync_address(struct net_device * dev,const unsigned char * addr)715 static int macvlan_sync_address(struct net_device *dev,
716 				const unsigned char *addr)
717 {
718 	struct macvlan_dev *vlan = netdev_priv(dev);
719 	struct net_device *lowerdev = vlan->lowerdev;
720 	struct macvlan_port *port = vlan->port;
721 	int err;
722 
723 	if (!(dev->flags & IFF_UP)) {
724 		/* Just copy in the new address */
725 		eth_hw_addr_set(dev, addr);
726 	} else {
727 		/* Rehash and update the device filters */
728 		if (macvlan_addr_busy(vlan->port, addr))
729 			return -EADDRINUSE;
730 
731 		if (!macvlan_passthru(port)) {
732 			err = dev_uc_add(lowerdev, addr);
733 			if (err)
734 				return err;
735 
736 			dev_uc_del(lowerdev, dev->dev_addr);
737 		}
738 
739 		macvlan_hash_change_addr(vlan, addr);
740 	}
741 	if (macvlan_passthru(port) && !macvlan_addr_change(port)) {
742 		/* Since addr_change isn't set, we are here due to lower
743 		 * device change.  Save the lower-dev address so we can
744 		 * restore it later.
745 		 */
746 		ether_addr_copy(vlan->port->perm_addr,
747 				lowerdev->dev_addr);
748 	}
749 	macvlan_clear_addr_change(port);
750 	return 0;
751 }
752 
macvlan_set_mac_address(struct net_device * dev,void * p)753 static int macvlan_set_mac_address(struct net_device *dev, void *p)
754 {
755 	struct macvlan_dev *vlan = netdev_priv(dev);
756 	struct sockaddr *addr = p;
757 
758 	if (!is_valid_ether_addr(addr->sa_data))
759 		return -EADDRNOTAVAIL;
760 
761 	/* If the addresses are the same, this is a no-op */
762 	if (ether_addr_equal(dev->dev_addr, addr->sa_data))
763 		return 0;
764 
765 	if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
766 		macvlan_set_addr_change(vlan->port);
767 		return dev_set_mac_address(vlan->lowerdev, addr, NULL);
768 	}
769 
770 	if (macvlan_addr_busy(vlan->port, addr->sa_data))
771 		return -EADDRINUSE;
772 
773 	return macvlan_sync_address(dev, addr->sa_data);
774 }
775 
macvlan_change_rx_flags(struct net_device * dev,int change)776 static void macvlan_change_rx_flags(struct net_device *dev, int change)
777 {
778 	struct macvlan_dev *vlan = netdev_priv(dev);
779 	struct net_device *lowerdev = vlan->lowerdev;
780 
781 	if (dev->flags & IFF_UP) {
782 		if (change & IFF_ALLMULTI)
783 			dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
784 		if (!macvlan_passthru(vlan->port) && change & IFF_PROMISC)
785 			dev_set_promiscuity(lowerdev,
786 					    dev->flags & IFF_PROMISC ? 1 : -1);
787 
788 	}
789 }
790 
macvlan_compute_filter(unsigned long * mc_filter,struct net_device * dev,struct macvlan_dev * vlan,int cutoff)791 static void macvlan_compute_filter(unsigned long *mc_filter,
792 				   struct net_device *dev,
793 				   struct macvlan_dev *vlan, int cutoff)
794 {
795 	if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
796 		bitmap_fill(mc_filter, MACVLAN_MC_FILTER_SZ);
797 	} else {
798 		DECLARE_BITMAP(filter, MACVLAN_MC_FILTER_SZ);
799 		struct netdev_hw_addr *ha;
800 
801 		bitmap_zero(filter, MACVLAN_MC_FILTER_SZ);
802 		netdev_for_each_mc_addr(ha, dev) {
803 			if (!vlan && ha->synced <= cutoff)
804 				continue;
805 
806 			__set_bit(mc_hash(vlan, ha->addr), filter);
807 		}
808 
809 		__set_bit(mc_hash(vlan, dev->broadcast), filter);
810 
811 		bitmap_copy(mc_filter, filter, MACVLAN_MC_FILTER_SZ);
812 	}
813 }
814 
macvlan_recompute_bc_filter(struct macvlan_dev * vlan)815 static void macvlan_recompute_bc_filter(struct macvlan_dev *vlan)
816 {
817 	if (vlan->port->bc_cutoff < 0) {
818 		bitmap_zero(vlan->port->bc_filter, MACVLAN_MC_FILTER_SZ);
819 		return;
820 	}
821 
822 	macvlan_compute_filter(vlan->port->bc_filter, vlan->lowerdev, NULL,
823 			       vlan->port->bc_cutoff);
824 }
825 
macvlan_set_mac_lists(struct net_device * dev)826 static void macvlan_set_mac_lists(struct net_device *dev)
827 {
828 	struct macvlan_dev *vlan = netdev_priv(dev);
829 
830 	macvlan_compute_filter(vlan->mc_filter, dev, vlan, 0);
831 
832 	dev_uc_sync(vlan->lowerdev, dev);
833 	dev_mc_sync(vlan->lowerdev, dev);
834 
835 	/* This is slightly inaccurate as we're including the subscription
836 	 * list of vlan->lowerdev too.
837 	 *
838 	 * Bug alert: This only works if everyone has the same broadcast
839 	 * address as lowerdev.  As soon as someone changes theirs this
840 	 * will break.
841 	 *
842 	 * However, this is already broken as when you change your broadcast
843 	 * address we don't get called.
844 	 *
845 	 * The solution is to maintain a list of broadcast addresses like
846 	 * we do for uc/mc, if you care.
847 	 */
848 	macvlan_compute_filter(vlan->port->mc_filter, vlan->lowerdev, NULL,
849 			       0);
850 	macvlan_recompute_bc_filter(vlan);
851 }
852 
update_port_bc_cutoff(struct macvlan_dev * vlan,int cutoff)853 static void update_port_bc_cutoff(struct macvlan_dev *vlan, int cutoff)
854 {
855 	if (vlan->port->bc_cutoff == cutoff)
856 		return;
857 
858 	vlan->port->bc_cutoff = cutoff;
859 	macvlan_recompute_bc_filter(vlan);
860 }
861 
macvlan_change_mtu(struct net_device * dev,int new_mtu)862 static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
863 {
864 	struct macvlan_dev *vlan = netdev_priv(dev);
865 
866 	if (vlan->lowerdev->mtu < new_mtu)
867 		return -EINVAL;
868 	WRITE_ONCE(dev->mtu, new_mtu);
869 	return 0;
870 }
871 
macvlan_hwtstamp_get(struct net_device * dev,struct kernel_hwtstamp_config * cfg)872 static int macvlan_hwtstamp_get(struct net_device *dev,
873 				struct kernel_hwtstamp_config *cfg)
874 {
875 	struct net_device *real_dev = macvlan_dev_real_dev(dev);
876 
877 	return generic_hwtstamp_get_lower(real_dev, cfg);
878 }
879 
macvlan_hwtstamp_set(struct net_device * dev,struct kernel_hwtstamp_config * cfg,struct netlink_ext_ack * extack)880 static int macvlan_hwtstamp_set(struct net_device *dev,
881 				struct kernel_hwtstamp_config *cfg,
882 				struct netlink_ext_ack *extack)
883 {
884 	struct net_device *real_dev = macvlan_dev_real_dev(dev);
885 
886 	if (!net_eq(dev_net(dev), &init_net))
887 		return -EOPNOTSUPP;
888 
889 	return generic_hwtstamp_set_lower(real_dev, cfg, extack);
890 }
891 
892 /*
893  * macvlan network devices have devices nesting below it and are a special
894  * "super class" of normal network devices; split their locks off into a
895  * separate class since they always nest.
896  */
897 static struct lock_class_key macvlan_netdev_addr_lock_key;
898 
899 #define ALWAYS_ON_OFFLOADS \
900 	(NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE | \
901 	 NETIF_F_GSO_ROBUST | NETIF_F_GSO_ENCAP_ALL)
902 
903 #define ALWAYS_ON_FEATURES ALWAYS_ON_OFFLOADS
904 
905 #define MACVLAN_FEATURES \
906 	(NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
907 	 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_LRO | \
908 	 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
909 	 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
910 
911 #define MACVLAN_STATE_MASK \
912 	((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
913 
macvlan_set_lockdep_class(struct net_device * dev)914 static void macvlan_set_lockdep_class(struct net_device *dev)
915 {
916 	netdev_lockdep_set_classes(dev);
917 	lockdep_set_class(&dev->addr_list_lock,
918 			  &macvlan_netdev_addr_lock_key);
919 }
920 
macvlan_init(struct net_device * dev)921 static int macvlan_init(struct net_device *dev)
922 {
923 	struct macvlan_dev *vlan = netdev_priv(dev);
924 	struct net_device *lowerdev = vlan->lowerdev;
925 	struct macvlan_port *port = vlan->port;
926 
927 	dev->state		= (dev->state & ~MACVLAN_STATE_MASK) |
928 				  (lowerdev->state & MACVLAN_STATE_MASK);
929 	dev->features 		= lowerdev->features & MACVLAN_FEATURES;
930 	dev->features		|= ALWAYS_ON_FEATURES;
931 	dev->hw_features	|= NETIF_F_LRO;
932 	dev->vlan_features	= lowerdev->vlan_features & MACVLAN_FEATURES;
933 	dev->vlan_features	|= ALWAYS_ON_OFFLOADS;
934 	dev->hw_enc_features    |= dev->features;
935 	dev->lltx		= true;
936 	netif_inherit_tso_max(dev, lowerdev);
937 	dev->hard_header_len	= lowerdev->hard_header_len;
938 	macvlan_set_lockdep_class(dev);
939 
940 	vlan->pcpu_stats = netdev_alloc_pcpu_stats(struct vlan_pcpu_stats);
941 	if (!vlan->pcpu_stats)
942 		return -ENOMEM;
943 
944 	port->count += 1;
945 
946 	/* Get macvlan's reference to lowerdev */
947 	netdev_hold(lowerdev, &vlan->dev_tracker, GFP_KERNEL);
948 
949 	return 0;
950 }
951 
macvlan_uninit(struct net_device * dev)952 static void macvlan_uninit(struct net_device *dev)
953 {
954 	struct macvlan_dev *vlan = netdev_priv(dev);
955 	struct macvlan_port *port = vlan->port;
956 
957 	free_percpu(vlan->pcpu_stats);
958 
959 	macvlan_flush_sources(port, vlan);
960 	port->count -= 1;
961 	if (!port->count)
962 		macvlan_port_destroy(port->dev);
963 }
964 
macvlan_dev_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats)965 static void macvlan_dev_get_stats64(struct net_device *dev,
966 				    struct rtnl_link_stats64 *stats)
967 {
968 	struct macvlan_dev *vlan = netdev_priv(dev);
969 
970 	if (vlan->pcpu_stats) {
971 		struct vlan_pcpu_stats *p;
972 		u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
973 		u32 rx_errors = 0, tx_dropped = 0;
974 		unsigned int start;
975 		int i;
976 
977 		for_each_possible_cpu(i) {
978 			p = per_cpu_ptr(vlan->pcpu_stats, i);
979 			do {
980 				start = u64_stats_fetch_begin(&p->syncp);
981 				rx_packets	= u64_stats_read(&p->rx_packets);
982 				rx_bytes	= u64_stats_read(&p->rx_bytes);
983 				rx_multicast	= u64_stats_read(&p->rx_multicast);
984 				tx_packets	= u64_stats_read(&p->tx_packets);
985 				tx_bytes	= u64_stats_read(&p->tx_bytes);
986 			} while (u64_stats_fetch_retry(&p->syncp, start));
987 
988 			stats->rx_packets	+= rx_packets;
989 			stats->rx_bytes		+= rx_bytes;
990 			stats->multicast	+= rx_multicast;
991 			stats->tx_packets	+= tx_packets;
992 			stats->tx_bytes		+= tx_bytes;
993 			/* rx_errors & tx_dropped are u32, updated
994 			 * without syncp protection.
995 			 */
996 			rx_errors	+= READ_ONCE(p->rx_errors);
997 			tx_dropped	+= READ_ONCE(p->tx_dropped);
998 		}
999 		stats->rx_errors	= rx_errors;
1000 		stats->rx_dropped	= rx_errors;
1001 		stats->tx_dropped	= tx_dropped;
1002 	}
1003 }
1004 
macvlan_vlan_rx_add_vid(struct net_device * dev,__be16 proto,u16 vid)1005 static int macvlan_vlan_rx_add_vid(struct net_device *dev,
1006 				   __be16 proto, u16 vid)
1007 {
1008 	struct macvlan_dev *vlan = netdev_priv(dev);
1009 	struct net_device *lowerdev = vlan->lowerdev;
1010 
1011 	return vlan_vid_add(lowerdev, proto, vid);
1012 }
1013 
macvlan_vlan_rx_kill_vid(struct net_device * dev,__be16 proto,u16 vid)1014 static int macvlan_vlan_rx_kill_vid(struct net_device *dev,
1015 				    __be16 proto, u16 vid)
1016 {
1017 	struct macvlan_dev *vlan = netdev_priv(dev);
1018 	struct net_device *lowerdev = vlan->lowerdev;
1019 
1020 	vlan_vid_del(lowerdev, proto, vid);
1021 	return 0;
1022 }
1023 
macvlan_fdb_add(struct ndmsg * ndm,struct nlattr * tb[],struct net_device * dev,const unsigned char * addr,u16 vid,u16 flags,struct netlink_ext_ack * extack)1024 static int macvlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1025 			   struct net_device *dev,
1026 			   const unsigned char *addr, u16 vid,
1027 			   u16 flags,
1028 			   struct netlink_ext_ack *extack)
1029 {
1030 	struct macvlan_dev *vlan = netdev_priv(dev);
1031 	int err = -EINVAL;
1032 
1033 	/* Support unicast filter only on passthru devices.
1034 	 * Multicast filter should be allowed on all devices.
1035 	 */
1036 	if (!macvlan_passthru(vlan->port) && is_unicast_ether_addr(addr))
1037 		return -EOPNOTSUPP;
1038 
1039 	if (flags & NLM_F_REPLACE)
1040 		return -EOPNOTSUPP;
1041 
1042 	if (is_unicast_ether_addr(addr))
1043 		err = dev_uc_add_excl(dev, addr);
1044 	else if (is_multicast_ether_addr(addr))
1045 		err = dev_mc_add_excl(dev, addr);
1046 
1047 	return err;
1048 }
1049 
macvlan_fdb_del(struct ndmsg * ndm,struct nlattr * tb[],struct net_device * dev,const unsigned char * addr,u16 vid,struct netlink_ext_ack * extack)1050 static int macvlan_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
1051 			   struct net_device *dev,
1052 			   const unsigned char *addr, u16 vid,
1053 			   struct netlink_ext_ack *extack)
1054 {
1055 	struct macvlan_dev *vlan = netdev_priv(dev);
1056 	int err = -EINVAL;
1057 
1058 	/* Support unicast filter only on passthru devices.
1059 	 * Multicast filter should be allowed on all devices.
1060 	 */
1061 	if (!macvlan_passthru(vlan->port) && is_unicast_ether_addr(addr))
1062 		return -EOPNOTSUPP;
1063 
1064 	if (is_unicast_ether_addr(addr))
1065 		err = dev_uc_del(dev, addr);
1066 	else if (is_multicast_ether_addr(addr))
1067 		err = dev_mc_del(dev, addr);
1068 
1069 	return err;
1070 }
1071 
macvlan_ethtool_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * drvinfo)1072 static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
1073 					struct ethtool_drvinfo *drvinfo)
1074 {
1075 	strscpy(drvinfo->driver, "macvlan", sizeof(drvinfo->driver));
1076 	strscpy(drvinfo->version, "0.1", sizeof(drvinfo->version));
1077 }
1078 
macvlan_ethtool_get_link_ksettings(struct net_device * dev,struct ethtool_link_ksettings * cmd)1079 static int macvlan_ethtool_get_link_ksettings(struct net_device *dev,
1080 					      struct ethtool_link_ksettings *cmd)
1081 {
1082 	const struct macvlan_dev *vlan = netdev_priv(dev);
1083 
1084 	return __ethtool_get_link_ksettings(vlan->lowerdev, cmd);
1085 }
1086 
macvlan_ethtool_get_ts_info(struct net_device * dev,struct kernel_ethtool_ts_info * info)1087 static int macvlan_ethtool_get_ts_info(struct net_device *dev,
1088 				       struct kernel_ethtool_ts_info *info)
1089 {
1090 	struct net_device *real_dev = macvlan_dev_real_dev(dev);
1091 
1092 	return ethtool_get_ts_info_by_layer(real_dev, info);
1093 }
1094 
macvlan_fix_features(struct net_device * dev,netdev_features_t features)1095 static netdev_features_t macvlan_fix_features(struct net_device *dev,
1096 					      netdev_features_t features)
1097 {
1098 	struct macvlan_dev *vlan = netdev_priv(dev);
1099 	netdev_features_t lowerdev_features = vlan->lowerdev->features;
1100 	netdev_features_t mask;
1101 
1102 	features |= NETIF_F_ALL_FOR_ALL;
1103 	features &= (vlan->set_features | ~MACVLAN_FEATURES);
1104 	mask = features;
1105 
1106 	lowerdev_features &= (features | ~NETIF_F_LRO);
1107 	features = netdev_increment_features(lowerdev_features, features, mask);
1108 	features |= ALWAYS_ON_FEATURES;
1109 	features &= (ALWAYS_ON_FEATURES | MACVLAN_FEATURES);
1110 
1111 	return features;
1112 }
1113 
1114 #ifdef CONFIG_NET_POLL_CONTROLLER
macvlan_dev_poll_controller(struct net_device * dev)1115 static void macvlan_dev_poll_controller(struct net_device *dev)
1116 {
1117 	return;
1118 }
1119 
macvlan_dev_netpoll_setup(struct net_device * dev,struct netpoll_info * npinfo)1120 static int macvlan_dev_netpoll_setup(struct net_device *dev, struct netpoll_info *npinfo)
1121 {
1122 	struct macvlan_dev *vlan = netdev_priv(dev);
1123 	struct net_device *real_dev = vlan->lowerdev;
1124 	struct netpoll *netpoll;
1125 	int err;
1126 
1127 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
1128 	err = -ENOMEM;
1129 	if (!netpoll)
1130 		goto out;
1131 
1132 	err = __netpoll_setup(netpoll, real_dev);
1133 	if (err) {
1134 		kfree(netpoll);
1135 		goto out;
1136 	}
1137 
1138 	vlan->netpoll = netpoll;
1139 
1140 out:
1141 	return err;
1142 }
1143 
macvlan_dev_netpoll_cleanup(struct net_device * dev)1144 static void macvlan_dev_netpoll_cleanup(struct net_device *dev)
1145 {
1146 	struct macvlan_dev *vlan = netdev_priv(dev);
1147 	struct netpoll *netpoll = vlan->netpoll;
1148 
1149 	if (!netpoll)
1150 		return;
1151 
1152 	vlan->netpoll = NULL;
1153 
1154 	__netpoll_free(netpoll);
1155 }
1156 #endif	/* CONFIG_NET_POLL_CONTROLLER */
1157 
macvlan_dev_get_iflink(const struct net_device * dev)1158 static int macvlan_dev_get_iflink(const struct net_device *dev)
1159 {
1160 	struct macvlan_dev *vlan = netdev_priv(dev);
1161 
1162 	return READ_ONCE(vlan->lowerdev->ifindex);
1163 }
1164 
1165 static const struct ethtool_ops macvlan_ethtool_ops = {
1166 	.get_link		= ethtool_op_get_link,
1167 	.get_link_ksettings	= macvlan_ethtool_get_link_ksettings,
1168 	.get_drvinfo		= macvlan_ethtool_get_drvinfo,
1169 	.get_ts_info		= macvlan_ethtool_get_ts_info,
1170 };
1171 
1172 static const struct net_device_ops macvlan_netdev_ops = {
1173 	.ndo_init		= macvlan_init,
1174 	.ndo_uninit		= macvlan_uninit,
1175 	.ndo_open		= macvlan_open,
1176 	.ndo_stop		= macvlan_stop,
1177 	.ndo_start_xmit		= macvlan_start_xmit,
1178 	.ndo_change_mtu		= macvlan_change_mtu,
1179 	.ndo_fix_features	= macvlan_fix_features,
1180 	.ndo_change_rx_flags	= macvlan_change_rx_flags,
1181 	.ndo_set_mac_address	= macvlan_set_mac_address,
1182 	.ndo_set_rx_mode	= macvlan_set_mac_lists,
1183 	.ndo_get_stats64	= macvlan_dev_get_stats64,
1184 	.ndo_validate_addr	= eth_validate_addr,
1185 	.ndo_vlan_rx_add_vid	= macvlan_vlan_rx_add_vid,
1186 	.ndo_vlan_rx_kill_vid	= macvlan_vlan_rx_kill_vid,
1187 	.ndo_fdb_add		= macvlan_fdb_add,
1188 	.ndo_fdb_del		= macvlan_fdb_del,
1189 	.ndo_fdb_dump		= ndo_dflt_fdb_dump,
1190 #ifdef CONFIG_NET_POLL_CONTROLLER
1191 	.ndo_poll_controller	= macvlan_dev_poll_controller,
1192 	.ndo_netpoll_setup	= macvlan_dev_netpoll_setup,
1193 	.ndo_netpoll_cleanup	= macvlan_dev_netpoll_cleanup,
1194 #endif
1195 	.ndo_get_iflink		= macvlan_dev_get_iflink,
1196 	.ndo_features_check	= passthru_features_check,
1197 	.ndo_hwtstamp_get	= macvlan_hwtstamp_get,
1198 	.ndo_hwtstamp_set	= macvlan_hwtstamp_set,
1199 };
1200 
macvlan_dev_free(struct net_device * dev)1201 static void macvlan_dev_free(struct net_device *dev)
1202 {
1203 	struct macvlan_dev *vlan = netdev_priv(dev);
1204 
1205 	/* Get rid of the macvlan's reference to lowerdev */
1206 	netdev_put(vlan->lowerdev, &vlan->dev_tracker);
1207 }
1208 
macvlan_common_setup(struct net_device * dev)1209 void macvlan_common_setup(struct net_device *dev)
1210 {
1211 	ether_setup(dev);
1212 
1213 	/* ether_setup() has set dev->min_mtu to ETH_MIN_MTU. */
1214 	dev->max_mtu		= ETH_MAX_MTU;
1215 	dev->priv_flags	       &= ~IFF_TX_SKB_SHARING;
1216 	netif_keep_dst(dev);
1217 	dev->priv_flags	       |= IFF_UNICAST_FLT;
1218 	dev->change_proto_down	= true;
1219 	dev->netdev_ops		= &macvlan_netdev_ops;
1220 	dev->needs_free_netdev	= true;
1221 	dev->priv_destructor	= macvlan_dev_free;
1222 	dev->header_ops		= &macvlan_hard_header_ops;
1223 	dev->ethtool_ops	= &macvlan_ethtool_ops;
1224 }
1225 EXPORT_SYMBOL_GPL(macvlan_common_setup);
1226 
macvlan_setup(struct net_device * dev)1227 static void macvlan_setup(struct net_device *dev)
1228 {
1229 	macvlan_common_setup(dev);
1230 	dev->priv_flags |= IFF_NO_QUEUE;
1231 }
1232 
macvlan_port_create(struct net_device * dev)1233 static int macvlan_port_create(struct net_device *dev)
1234 {
1235 	struct macvlan_port *port;
1236 	unsigned int i;
1237 	int err;
1238 
1239 	if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
1240 		return -EINVAL;
1241 
1242 	if (netdev_is_rx_handler_busy(dev))
1243 		return -EBUSY;
1244 
1245 	port = kzalloc(sizeof(*port), GFP_KERNEL);
1246 	if (port == NULL)
1247 		return -ENOMEM;
1248 
1249 	port->dev = dev;
1250 	ether_addr_copy(port->perm_addr, dev->dev_addr);
1251 	INIT_LIST_HEAD(&port->vlans);
1252 	for (i = 0; i < MACVLAN_HASH_SIZE; i++)
1253 		INIT_HLIST_HEAD(&port->vlan_hash[i]);
1254 	for (i = 0; i < MACVLAN_HASH_SIZE; i++)
1255 		INIT_HLIST_HEAD(&port->vlan_source_hash[i]);
1256 
1257 	port->bc_queue_len_used = 0;
1258 	port->bc_cutoff = 1;
1259 	skb_queue_head_init(&port->bc_queue);
1260 	INIT_WORK(&port->bc_work, macvlan_process_broadcast);
1261 
1262 	err = netdev_rx_handler_register(dev, macvlan_handle_frame, port);
1263 	if (err)
1264 		kfree(port);
1265 	else
1266 		dev->priv_flags |= IFF_MACVLAN_PORT;
1267 	return err;
1268 }
1269 
macvlan_port_destroy(struct net_device * dev)1270 static void macvlan_port_destroy(struct net_device *dev)
1271 {
1272 	struct macvlan_port *port = macvlan_port_get_rtnl(dev);
1273 	struct sk_buff *skb;
1274 
1275 	dev->priv_flags &= ~IFF_MACVLAN_PORT;
1276 	netdev_rx_handler_unregister(dev);
1277 
1278 	/* After this point, no packet can schedule bc_work anymore,
1279 	 * but we need to cancel it and purge left skbs if any.
1280 	 */
1281 	cancel_work_sync(&port->bc_work);
1282 
1283 	while ((skb = __skb_dequeue(&port->bc_queue))) {
1284 		const struct macvlan_dev *src = MACVLAN_SKB_CB(skb)->src;
1285 
1286 		if (src)
1287 			dev_put(src->dev);
1288 
1289 		kfree_skb(skb);
1290 	}
1291 
1292 	/* If the lower device address has been changed by passthru
1293 	 * macvlan, put it back.
1294 	 */
1295 	if (macvlan_passthru(port) &&
1296 	    !ether_addr_equal(port->dev->dev_addr, port->perm_addr)) {
1297 		struct sockaddr sa;
1298 
1299 		sa.sa_family = port->dev->type;
1300 		memcpy(&sa.sa_data, port->perm_addr, port->dev->addr_len);
1301 		dev_set_mac_address(port->dev, &sa, NULL);
1302 	}
1303 
1304 	kfree(port);
1305 }
1306 
macvlan_validate(struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)1307 static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[],
1308 			    struct netlink_ext_ack *extack)
1309 {
1310 	struct nlattr *nla, *head;
1311 	int rem, len;
1312 
1313 	if (tb[IFLA_ADDRESS]) {
1314 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1315 			return -EINVAL;
1316 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1317 			return -EADDRNOTAVAIL;
1318 	}
1319 
1320 	if (!data)
1321 		return 0;
1322 
1323 	if (data[IFLA_MACVLAN_FLAGS] &&
1324 	    nla_get_u16(data[IFLA_MACVLAN_FLAGS]) & ~(MACVLAN_FLAG_NOPROMISC |
1325 						      MACVLAN_FLAG_NODST))
1326 		return -EINVAL;
1327 
1328 	if (data[IFLA_MACVLAN_MODE]) {
1329 		switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) {
1330 		case MACVLAN_MODE_PRIVATE:
1331 		case MACVLAN_MODE_VEPA:
1332 		case MACVLAN_MODE_BRIDGE:
1333 		case MACVLAN_MODE_PASSTHRU:
1334 		case MACVLAN_MODE_SOURCE:
1335 			break;
1336 		default:
1337 			return -EINVAL;
1338 		}
1339 	}
1340 
1341 	if (data[IFLA_MACVLAN_MACADDR_MODE]) {
1342 		switch (nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE])) {
1343 		case MACVLAN_MACADDR_ADD:
1344 		case MACVLAN_MACADDR_DEL:
1345 		case MACVLAN_MACADDR_FLUSH:
1346 		case MACVLAN_MACADDR_SET:
1347 			break;
1348 		default:
1349 			return -EINVAL;
1350 		}
1351 	}
1352 
1353 	if (data[IFLA_MACVLAN_MACADDR]) {
1354 		if (nla_len(data[IFLA_MACVLAN_MACADDR]) != ETH_ALEN)
1355 			return -EINVAL;
1356 
1357 		if (!is_valid_ether_addr(nla_data(data[IFLA_MACVLAN_MACADDR])))
1358 			return -EADDRNOTAVAIL;
1359 	}
1360 
1361 	if (data[IFLA_MACVLAN_MACADDR_DATA]) {
1362 		head = nla_data(data[IFLA_MACVLAN_MACADDR_DATA]);
1363 		len = nla_len(data[IFLA_MACVLAN_MACADDR_DATA]);
1364 
1365 		nla_for_each_attr(nla, head, len, rem) {
1366 			if (nla_type(nla) != IFLA_MACVLAN_MACADDR ||
1367 			    nla_len(nla) != ETH_ALEN)
1368 				return -EINVAL;
1369 
1370 			if (!is_valid_ether_addr(nla_data(nla)))
1371 				return -EADDRNOTAVAIL;
1372 		}
1373 	}
1374 
1375 	if (data[IFLA_MACVLAN_MACADDR_COUNT])
1376 		return -EINVAL;
1377 
1378 	return 0;
1379 }
1380 
1381 /*
1382  * reconfigure list of remote source mac address
1383  * (only for macvlan devices in source mode)
1384  * Note regarding alignment: all netlink data is aligned to 4 Byte, which
1385  * suffices for both ether_addr_copy and ether_addr_equal_64bits usage.
1386  */
macvlan_changelink_sources(struct macvlan_dev * vlan,u32 mode,struct nlattr * data[])1387 static int macvlan_changelink_sources(struct macvlan_dev *vlan, u32 mode,
1388 				      struct nlattr *data[])
1389 {
1390 	char *addr = NULL;
1391 	int ret, rem, len;
1392 	struct nlattr *nla, *head;
1393 	struct macvlan_source_entry *entry;
1394 
1395 	if (data[IFLA_MACVLAN_MACADDR])
1396 		addr = nla_data(data[IFLA_MACVLAN_MACADDR]);
1397 
1398 	if (mode == MACVLAN_MACADDR_ADD) {
1399 		if (!addr)
1400 			return -EINVAL;
1401 
1402 		return macvlan_hash_add_source(vlan, addr);
1403 
1404 	} else if (mode == MACVLAN_MACADDR_DEL) {
1405 		if (!addr)
1406 			return -EINVAL;
1407 
1408 		entry = macvlan_hash_lookup_source(vlan, addr);
1409 		if (entry) {
1410 			macvlan_hash_del_source(entry);
1411 			vlan->macaddr_count--;
1412 		}
1413 	} else if (mode == MACVLAN_MACADDR_FLUSH) {
1414 		macvlan_flush_sources(vlan->port, vlan);
1415 	} else if (mode == MACVLAN_MACADDR_SET) {
1416 		macvlan_flush_sources(vlan->port, vlan);
1417 
1418 		if (addr) {
1419 			ret = macvlan_hash_add_source(vlan, addr);
1420 			if (ret)
1421 				return ret;
1422 		}
1423 
1424 		if (!data[IFLA_MACVLAN_MACADDR_DATA])
1425 			return 0;
1426 
1427 		head = nla_data(data[IFLA_MACVLAN_MACADDR_DATA]);
1428 		len = nla_len(data[IFLA_MACVLAN_MACADDR_DATA]);
1429 
1430 		nla_for_each_attr(nla, head, len, rem) {
1431 			addr = nla_data(nla);
1432 			ret = macvlan_hash_add_source(vlan, addr);
1433 			if (ret)
1434 				return ret;
1435 		}
1436 	} else {
1437 		return -EINVAL;
1438 	}
1439 
1440 	return 0;
1441 }
1442 
macvlan_common_newlink(struct net * src_net,struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)1443 int macvlan_common_newlink(struct net *src_net, struct net_device *dev,
1444 			   struct nlattr *tb[], struct nlattr *data[],
1445 			   struct netlink_ext_ack *extack)
1446 {
1447 	struct macvlan_dev *vlan = netdev_priv(dev);
1448 	struct macvlan_port *port;
1449 	struct net_device *lowerdev;
1450 	int err;
1451 	int macmode;
1452 	bool create = false;
1453 
1454 	if (!tb[IFLA_LINK])
1455 		return -EINVAL;
1456 
1457 	lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
1458 	if (lowerdev == NULL)
1459 		return -ENODEV;
1460 
1461 	/* When creating macvlans or macvtaps on top of other macvlans - use
1462 	 * the real device as the lowerdev.
1463 	 */
1464 	if (netif_is_macvlan(lowerdev))
1465 		lowerdev = macvlan_dev_real_dev(lowerdev);
1466 
1467 	if (!tb[IFLA_MTU])
1468 		dev->mtu = lowerdev->mtu;
1469 	else if (dev->mtu > lowerdev->mtu)
1470 		return -EINVAL;
1471 
1472 	/* MTU range: 68 - lowerdev->max_mtu */
1473 	dev->min_mtu = ETH_MIN_MTU;
1474 	dev->max_mtu = lowerdev->max_mtu;
1475 
1476 	if (!tb[IFLA_ADDRESS])
1477 		eth_hw_addr_random(dev);
1478 
1479 	if (!netif_is_macvlan_port(lowerdev)) {
1480 		err = macvlan_port_create(lowerdev);
1481 		if (err < 0)
1482 			return err;
1483 		create = true;
1484 	}
1485 	port = macvlan_port_get_rtnl(lowerdev);
1486 
1487 	/* Only 1 macvlan device can be created in passthru mode */
1488 	if (macvlan_passthru(port)) {
1489 		/* The macvlan port must be not created this time,
1490 		 * still goto destroy_macvlan_port for readability.
1491 		 */
1492 		err = -EINVAL;
1493 		goto destroy_macvlan_port;
1494 	}
1495 
1496 	vlan->lowerdev = lowerdev;
1497 	vlan->dev      = dev;
1498 	vlan->port     = port;
1499 	vlan->set_features = MACVLAN_FEATURES;
1500 
1501 	vlan->mode     = MACVLAN_MODE_VEPA;
1502 	if (data && data[IFLA_MACVLAN_MODE])
1503 		vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
1504 
1505 	if (data && data[IFLA_MACVLAN_FLAGS])
1506 		vlan->flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
1507 
1508 	if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
1509 		if (port->count) {
1510 			err = -EINVAL;
1511 			goto destroy_macvlan_port;
1512 		}
1513 		macvlan_set_passthru(port);
1514 		eth_hw_addr_inherit(dev, lowerdev);
1515 	}
1516 
1517 	if (data && data[IFLA_MACVLAN_MACADDR_MODE]) {
1518 		if (vlan->mode != MACVLAN_MODE_SOURCE) {
1519 			err = -EINVAL;
1520 			goto destroy_macvlan_port;
1521 		}
1522 		macmode = nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE]);
1523 		err = macvlan_changelink_sources(vlan, macmode, data);
1524 		if (err)
1525 			goto destroy_macvlan_port;
1526 	}
1527 
1528 	vlan->bc_queue_len_req = MACVLAN_DEFAULT_BC_QUEUE_LEN;
1529 	if (data && data[IFLA_MACVLAN_BC_QUEUE_LEN])
1530 		vlan->bc_queue_len_req = nla_get_u32(data[IFLA_MACVLAN_BC_QUEUE_LEN]);
1531 
1532 	if (data && data[IFLA_MACVLAN_BC_CUTOFF])
1533 		update_port_bc_cutoff(
1534 			vlan, nla_get_s32(data[IFLA_MACVLAN_BC_CUTOFF]));
1535 
1536 	err = register_netdevice(dev);
1537 	if (err < 0)
1538 		goto destroy_macvlan_port;
1539 
1540 	dev->priv_flags |= IFF_MACVLAN;
1541 	err = netdev_upper_dev_link(lowerdev, dev, extack);
1542 	if (err)
1543 		goto unregister_netdev;
1544 
1545 	list_add_tail_rcu(&vlan->list, &port->vlans);
1546 	update_port_bc_queue_len(vlan->port);
1547 	netif_stacked_transfer_operstate(lowerdev, dev);
1548 	linkwatch_fire_event(dev);
1549 
1550 	return 0;
1551 
1552 unregister_netdev:
1553 	/* macvlan_uninit would free the macvlan port */
1554 	unregister_netdevice(dev);
1555 	return err;
1556 destroy_macvlan_port:
1557 	/* the macvlan port may be freed by macvlan_uninit when fail to register.
1558 	 * so we destroy the macvlan port only when it's valid.
1559 	 */
1560 	if (create && macvlan_port_get_rtnl(lowerdev)) {
1561 		macvlan_flush_sources(port, vlan);
1562 		macvlan_port_destroy(port->dev);
1563 	}
1564 	return err;
1565 }
1566 EXPORT_SYMBOL_GPL(macvlan_common_newlink);
1567 
macvlan_newlink(struct net * src_net,struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)1568 static int macvlan_newlink(struct net *src_net, struct net_device *dev,
1569 			   struct nlattr *tb[], struct nlattr *data[],
1570 			   struct netlink_ext_ack *extack)
1571 {
1572 	return macvlan_common_newlink(src_net, dev, tb, data, extack);
1573 }
1574 
macvlan_dellink(struct net_device * dev,struct list_head * head)1575 void macvlan_dellink(struct net_device *dev, struct list_head *head)
1576 {
1577 	struct macvlan_dev *vlan = netdev_priv(dev);
1578 
1579 	if (vlan->mode == MACVLAN_MODE_SOURCE)
1580 		macvlan_flush_sources(vlan->port, vlan);
1581 	list_del_rcu(&vlan->list);
1582 	update_port_bc_queue_len(vlan->port);
1583 	unregister_netdevice_queue(dev, head);
1584 	netdev_upper_dev_unlink(vlan->lowerdev, dev);
1585 }
1586 EXPORT_SYMBOL_GPL(macvlan_dellink);
1587 
macvlan_changelink(struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)1588 static int macvlan_changelink(struct net_device *dev,
1589 			      struct nlattr *tb[], struct nlattr *data[],
1590 			      struct netlink_ext_ack *extack)
1591 {
1592 	struct macvlan_dev *vlan = netdev_priv(dev);
1593 	enum macvlan_mode mode;
1594 	bool set_mode = false;
1595 	enum macvlan_macaddr_mode macmode;
1596 	int ret;
1597 
1598 	/* Validate mode, but don't set yet: setting flags may fail. */
1599 	if (data && data[IFLA_MACVLAN_MODE]) {
1600 		set_mode = true;
1601 		mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
1602 		/* Passthrough mode can't be set or cleared dynamically */
1603 		if ((mode == MACVLAN_MODE_PASSTHRU) !=
1604 		    (vlan->mode == MACVLAN_MODE_PASSTHRU))
1605 			return -EINVAL;
1606 		if (vlan->mode == MACVLAN_MODE_SOURCE &&
1607 		    vlan->mode != mode)
1608 			macvlan_flush_sources(vlan->port, vlan);
1609 	}
1610 
1611 	if (data && data[IFLA_MACVLAN_FLAGS]) {
1612 		__u16 flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
1613 		bool promisc = (flags ^ vlan->flags) & MACVLAN_FLAG_NOPROMISC;
1614 		if (macvlan_passthru(vlan->port) && promisc) {
1615 			int err;
1616 
1617 			if (flags & MACVLAN_FLAG_NOPROMISC)
1618 				err = dev_set_promiscuity(vlan->lowerdev, -1);
1619 			else
1620 				err = dev_set_promiscuity(vlan->lowerdev, 1);
1621 			if (err < 0)
1622 				return err;
1623 		}
1624 		vlan->flags = flags;
1625 	}
1626 
1627 	if (data && data[IFLA_MACVLAN_BC_QUEUE_LEN]) {
1628 		vlan->bc_queue_len_req = nla_get_u32(data[IFLA_MACVLAN_BC_QUEUE_LEN]);
1629 		update_port_bc_queue_len(vlan->port);
1630 	}
1631 
1632 	if (data && data[IFLA_MACVLAN_BC_CUTOFF])
1633 		update_port_bc_cutoff(
1634 			vlan, nla_get_s32(data[IFLA_MACVLAN_BC_CUTOFF]));
1635 
1636 	if (set_mode)
1637 		vlan->mode = mode;
1638 	if (data && data[IFLA_MACVLAN_MACADDR_MODE]) {
1639 		if (vlan->mode != MACVLAN_MODE_SOURCE)
1640 			return -EINVAL;
1641 		macmode = nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE]);
1642 		ret = macvlan_changelink_sources(vlan, macmode, data);
1643 		if (ret)
1644 			return ret;
1645 	}
1646 	return 0;
1647 }
1648 
macvlan_get_size_mac(const struct macvlan_dev * vlan)1649 static size_t macvlan_get_size_mac(const struct macvlan_dev *vlan)
1650 {
1651 	if (vlan->macaddr_count == 0)
1652 		return 0;
1653 	return nla_total_size(0) /* IFLA_MACVLAN_MACADDR_DATA */
1654 		+ vlan->macaddr_count * nla_total_size(sizeof(u8) * ETH_ALEN);
1655 }
1656 
macvlan_get_size(const struct net_device * dev)1657 static size_t macvlan_get_size(const struct net_device *dev)
1658 {
1659 	struct macvlan_dev *vlan = netdev_priv(dev);
1660 
1661 	return (0
1662 		+ nla_total_size(4) /* IFLA_MACVLAN_MODE */
1663 		+ nla_total_size(2) /* IFLA_MACVLAN_FLAGS */
1664 		+ nla_total_size(4) /* IFLA_MACVLAN_MACADDR_COUNT */
1665 		+ macvlan_get_size_mac(vlan) /* IFLA_MACVLAN_MACADDR */
1666 		+ nla_total_size(4) /* IFLA_MACVLAN_BC_QUEUE_LEN */
1667 		+ nla_total_size(4) /* IFLA_MACVLAN_BC_QUEUE_LEN_USED */
1668 		);
1669 }
1670 
macvlan_fill_info_macaddr(struct sk_buff * skb,const struct macvlan_dev * vlan,const int i)1671 static int macvlan_fill_info_macaddr(struct sk_buff *skb,
1672 				     const struct macvlan_dev *vlan,
1673 				     const int i)
1674 {
1675 	struct hlist_head *h = &vlan->port->vlan_source_hash[i];
1676 	struct macvlan_source_entry *entry;
1677 
1678 	hlist_for_each_entry_rcu(entry, h, hlist, lockdep_rtnl_is_held()) {
1679 		if (entry->vlan != vlan)
1680 			continue;
1681 		if (nla_put(skb, IFLA_MACVLAN_MACADDR, ETH_ALEN, entry->addr))
1682 			return 1;
1683 	}
1684 	return 0;
1685 }
1686 
macvlan_fill_info(struct sk_buff * skb,const struct net_device * dev)1687 static int macvlan_fill_info(struct sk_buff *skb,
1688 				const struct net_device *dev)
1689 {
1690 	struct macvlan_dev *vlan = netdev_priv(dev);
1691 	struct macvlan_port *port = vlan->port;
1692 	int i;
1693 	struct nlattr *nest;
1694 
1695 	if (nla_put_u32(skb, IFLA_MACVLAN_MODE, vlan->mode))
1696 		goto nla_put_failure;
1697 	if (nla_put_u16(skb, IFLA_MACVLAN_FLAGS, vlan->flags))
1698 		goto nla_put_failure;
1699 	if (nla_put_u32(skb, IFLA_MACVLAN_MACADDR_COUNT, vlan->macaddr_count))
1700 		goto nla_put_failure;
1701 	if (vlan->macaddr_count > 0) {
1702 		nest = nla_nest_start_noflag(skb, IFLA_MACVLAN_MACADDR_DATA);
1703 		if (nest == NULL)
1704 			goto nla_put_failure;
1705 
1706 		for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
1707 			if (macvlan_fill_info_macaddr(skb, vlan, i))
1708 				goto nla_put_failure;
1709 		}
1710 		nla_nest_end(skb, nest);
1711 	}
1712 	if (nla_put_u32(skb, IFLA_MACVLAN_BC_QUEUE_LEN, vlan->bc_queue_len_req))
1713 		goto nla_put_failure;
1714 	if (nla_put_u32(skb, IFLA_MACVLAN_BC_QUEUE_LEN_USED, port->bc_queue_len_used))
1715 		goto nla_put_failure;
1716 	if (port->bc_cutoff != 1 &&
1717 	    nla_put_s32(skb, IFLA_MACVLAN_BC_CUTOFF, port->bc_cutoff))
1718 		goto nla_put_failure;
1719 	return 0;
1720 
1721 nla_put_failure:
1722 	return -EMSGSIZE;
1723 }
1724 
1725 static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = {
1726 	[IFLA_MACVLAN_MODE]  = { .type = NLA_U32 },
1727 	[IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 },
1728 	[IFLA_MACVLAN_MACADDR_MODE] = { .type = NLA_U32 },
1729 	[IFLA_MACVLAN_MACADDR] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1730 	[IFLA_MACVLAN_MACADDR_DATA] = { .type = NLA_NESTED },
1731 	[IFLA_MACVLAN_MACADDR_COUNT] = { .type = NLA_U32 },
1732 	[IFLA_MACVLAN_BC_QUEUE_LEN] = { .type = NLA_U32 },
1733 	[IFLA_MACVLAN_BC_QUEUE_LEN_USED] = { .type = NLA_REJECT },
1734 	[IFLA_MACVLAN_BC_CUTOFF] = { .type = NLA_S32 },
1735 };
1736 
macvlan_link_register(struct rtnl_link_ops * ops)1737 int macvlan_link_register(struct rtnl_link_ops *ops)
1738 {
1739 	/* common fields */
1740 	ops->validate		= macvlan_validate;
1741 	ops->maxtype		= IFLA_MACVLAN_MAX;
1742 	ops->policy		= macvlan_policy;
1743 	ops->changelink		= macvlan_changelink;
1744 	ops->get_size		= macvlan_get_size;
1745 	ops->fill_info		= macvlan_fill_info;
1746 
1747 	return rtnl_link_register(ops);
1748 };
1749 EXPORT_SYMBOL_GPL(macvlan_link_register);
1750 
macvlan_get_link_net(const struct net_device * dev)1751 static struct net *macvlan_get_link_net(const struct net_device *dev)
1752 {
1753 	return dev_net(macvlan_dev_real_dev(dev));
1754 }
1755 
1756 static struct rtnl_link_ops macvlan_link_ops = {
1757 	.kind		= "macvlan",
1758 	.setup		= macvlan_setup,
1759 	.newlink	= macvlan_newlink,
1760 	.dellink	= macvlan_dellink,
1761 	.get_link_net	= macvlan_get_link_net,
1762 	.priv_size      = sizeof(struct macvlan_dev),
1763 };
1764 
update_port_bc_queue_len(struct macvlan_port * port)1765 static void update_port_bc_queue_len(struct macvlan_port *port)
1766 {
1767 	u32 max_bc_queue_len_req = 0;
1768 	struct macvlan_dev *vlan;
1769 
1770 	list_for_each_entry(vlan, &port->vlans, list) {
1771 		if (vlan->bc_queue_len_req > max_bc_queue_len_req)
1772 			max_bc_queue_len_req = vlan->bc_queue_len_req;
1773 	}
1774 	port->bc_queue_len_used = max_bc_queue_len_req;
1775 }
1776 
macvlan_device_event(struct notifier_block * unused,unsigned long event,void * ptr)1777 static int macvlan_device_event(struct notifier_block *unused,
1778 				unsigned long event, void *ptr)
1779 {
1780 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1781 	struct macvlan_dev *vlan, *next;
1782 	struct macvlan_port *port;
1783 	LIST_HEAD(list_kill);
1784 
1785 	if (!netif_is_macvlan_port(dev))
1786 		return NOTIFY_DONE;
1787 
1788 	port = macvlan_port_get_rtnl(dev);
1789 
1790 	switch (event) {
1791 	case NETDEV_UP:
1792 	case NETDEV_DOWN:
1793 	case NETDEV_CHANGE:
1794 		list_for_each_entry(vlan, &port->vlans, list)
1795 			netif_stacked_transfer_operstate(vlan->lowerdev,
1796 							 vlan->dev);
1797 		break;
1798 	case NETDEV_FEAT_CHANGE:
1799 		list_for_each_entry(vlan, &port->vlans, list) {
1800 			netif_inherit_tso_max(vlan->dev, dev);
1801 			netdev_update_features(vlan->dev);
1802 		}
1803 		break;
1804 	case NETDEV_CHANGEMTU:
1805 		list_for_each_entry(vlan, &port->vlans, list) {
1806 			if (vlan->dev->mtu <= dev->mtu)
1807 				continue;
1808 			dev_set_mtu(vlan->dev, dev->mtu);
1809 		}
1810 		break;
1811 	case NETDEV_CHANGEADDR:
1812 		if (!macvlan_passthru(port))
1813 			return NOTIFY_DONE;
1814 
1815 		vlan = list_first_entry_or_null(&port->vlans,
1816 						struct macvlan_dev,
1817 						list);
1818 
1819 		if (vlan && macvlan_sync_address(vlan->dev, dev->dev_addr))
1820 			return NOTIFY_BAD;
1821 
1822 		break;
1823 	case NETDEV_UNREGISTER:
1824 		/* twiddle thumbs on netns device moves */
1825 		if (dev->reg_state != NETREG_UNREGISTERING)
1826 			break;
1827 
1828 		list_for_each_entry_safe(vlan, next, &port->vlans, list)
1829 			vlan->dev->rtnl_link_ops->dellink(vlan->dev, &list_kill);
1830 		unregister_netdevice_many(&list_kill);
1831 		break;
1832 	case NETDEV_PRE_TYPE_CHANGE:
1833 		/* Forbid underlying device to change its type. */
1834 		return NOTIFY_BAD;
1835 
1836 	case NETDEV_NOTIFY_PEERS:
1837 	case NETDEV_BONDING_FAILOVER:
1838 	case NETDEV_RESEND_IGMP:
1839 		/* Propagate to all vlans */
1840 		list_for_each_entry(vlan, &port->vlans, list)
1841 			call_netdevice_notifiers(event, vlan->dev);
1842 	}
1843 	return NOTIFY_DONE;
1844 }
1845 
1846 static struct notifier_block macvlan_notifier_block __read_mostly = {
1847 	.notifier_call	= macvlan_device_event,
1848 };
1849 
macvlan_init_module(void)1850 static int __init macvlan_init_module(void)
1851 {
1852 	int err;
1853 
1854 	register_netdevice_notifier(&macvlan_notifier_block);
1855 
1856 	err = macvlan_link_register(&macvlan_link_ops);
1857 	if (err < 0)
1858 		goto err1;
1859 	return 0;
1860 err1:
1861 	unregister_netdevice_notifier(&macvlan_notifier_block);
1862 	return err;
1863 }
1864 
macvlan_cleanup_module(void)1865 static void __exit macvlan_cleanup_module(void)
1866 {
1867 	rtnl_link_unregister(&macvlan_link_ops);
1868 	unregister_netdevice_notifier(&macvlan_notifier_block);
1869 }
1870 
1871 module_init(macvlan_init_module);
1872 module_exit(macvlan_cleanup_module);
1873 
1874 MODULE_LICENSE("GPL");
1875 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
1876 MODULE_DESCRIPTION("Driver for MAC address based VLANs");
1877 MODULE_ALIAS_RTNL_LINK("macvlan");
1878