Linux 4.19.133
[linux/fpc-iii.git] / drivers / net / macvlan.c
blob349123592af0f5387f3dd8aec32eb7c5fdcce9bd
1 /*
2 * Copyright (c) 2007 Patrick McHardy <kaber@trash.net>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
9 * The code this is based on carried the following copyright notice:
10 * ---
11 * (C) Copyright 2001-2006
12 * Alex Zeffertt, Cambridge Broadband Ltd, ajz@cambridgebroadband.com
13 * Re-worked by Ben Greear <greearb@candelatech.com>
14 * ---
16 #include <linux/kernel.h>
17 #include <linux/types.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/errno.h>
21 #include <linux/slab.h>
22 #include <linux/string.h>
23 #include <linux/rculist.h>
24 #include <linux/notifier.h>
25 #include <linux/netdevice.h>
26 #include <linux/etherdevice.h>
27 #include <linux/ethtool.h>
28 #include <linux/if_arp.h>
29 #include <linux/if_vlan.h>
30 #include <linux/if_link.h>
31 #include <linux/if_macvlan.h>
32 #include <linux/hash.h>
33 #include <linux/workqueue.h>
34 #include <net/rtnetlink.h>
35 #include <net/xfrm.h>
36 #include <linux/netpoll.h>
38 #define MACVLAN_HASH_BITS 8
39 #define MACVLAN_HASH_SIZE (1<<MACVLAN_HASH_BITS)
40 #define MACVLAN_BC_QUEUE_LEN 1000
42 #define MACVLAN_F_PASSTHRU 1
43 #define MACVLAN_F_ADDRCHANGE 2
45 struct macvlan_port {
46 struct net_device *dev;
47 struct hlist_head vlan_hash[MACVLAN_HASH_SIZE];
48 struct list_head vlans;
49 struct sk_buff_head bc_queue;
50 struct work_struct bc_work;
51 u32 flags;
52 int count;
53 struct hlist_head vlan_source_hash[MACVLAN_HASH_SIZE];
54 DECLARE_BITMAP(mc_filter, MACVLAN_MC_FILTER_SZ);
55 unsigned char perm_addr[ETH_ALEN];
58 struct macvlan_source_entry {
59 struct hlist_node hlist;
60 struct macvlan_dev *vlan;
61 unsigned char addr[6+2] __aligned(sizeof(u16));
62 struct rcu_head rcu;
65 struct macvlan_skb_cb {
66 const struct macvlan_dev *src;
69 #define MACVLAN_SKB_CB(__skb) ((struct macvlan_skb_cb *)&((__skb)->cb[0]))
71 static void macvlan_port_destroy(struct net_device *dev);
73 static inline bool macvlan_passthru(const struct macvlan_port *port)
75 return port->flags & MACVLAN_F_PASSTHRU;
78 static inline void macvlan_set_passthru(struct macvlan_port *port)
80 port->flags |= MACVLAN_F_PASSTHRU;
83 static inline bool macvlan_addr_change(const struct macvlan_port *port)
85 return port->flags & MACVLAN_F_ADDRCHANGE;
88 static inline void macvlan_set_addr_change(struct macvlan_port *port)
90 port->flags |= MACVLAN_F_ADDRCHANGE;
93 static inline void macvlan_clear_addr_change(struct macvlan_port *port)
95 port->flags &= ~MACVLAN_F_ADDRCHANGE;
98 /* Hash Ethernet address */
99 static u32 macvlan_eth_hash(const unsigned char *addr)
101 u64 value = get_unaligned((u64 *)addr);
103 /* only want 6 bytes */
104 #ifdef __BIG_ENDIAN
105 value >>= 16;
106 #else
107 value <<= 16;
108 #endif
109 return hash_64(value, MACVLAN_HASH_BITS);
112 static struct macvlan_port *macvlan_port_get_rcu(const struct net_device *dev)
114 return rcu_dereference(dev->rx_handler_data);
117 static struct macvlan_port *macvlan_port_get_rtnl(const struct net_device *dev)
119 return rtnl_dereference(dev->rx_handler_data);
122 #define macvlan_port_exists(dev) (dev->priv_flags & IFF_MACVLAN_PORT)
124 static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
125 const unsigned char *addr)
127 struct macvlan_dev *vlan;
128 u32 idx = macvlan_eth_hash(addr);
130 hlist_for_each_entry_rcu(vlan, &port->vlan_hash[idx], hlist) {
131 if (ether_addr_equal_64bits(vlan->dev->dev_addr, addr))
132 return vlan;
134 return NULL;
137 static struct macvlan_source_entry *macvlan_hash_lookup_source(
138 const struct macvlan_dev *vlan,
139 const unsigned char *addr)
141 struct macvlan_source_entry *entry;
142 u32 idx = macvlan_eth_hash(addr);
143 struct hlist_head *h = &vlan->port->vlan_source_hash[idx];
145 hlist_for_each_entry_rcu(entry, h, hlist) {
146 if (ether_addr_equal_64bits(entry->addr, addr) &&
147 entry->vlan == vlan)
148 return entry;
150 return NULL;
153 static int macvlan_hash_add_source(struct macvlan_dev *vlan,
154 const unsigned char *addr)
156 struct macvlan_port *port = vlan->port;
157 struct macvlan_source_entry *entry;
158 struct hlist_head *h;
160 entry = macvlan_hash_lookup_source(vlan, addr);
161 if (entry)
162 return 0;
164 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
165 if (!entry)
166 return -ENOMEM;
168 ether_addr_copy(entry->addr, addr);
169 entry->vlan = vlan;
170 h = &port->vlan_source_hash[macvlan_eth_hash(addr)];
171 hlist_add_head_rcu(&entry->hlist, h);
172 vlan->macaddr_count++;
174 return 0;
177 static void macvlan_hash_add(struct macvlan_dev *vlan)
179 struct macvlan_port *port = vlan->port;
180 const unsigned char *addr = vlan->dev->dev_addr;
181 u32 idx = macvlan_eth_hash(addr);
183 hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[idx]);
186 static void macvlan_hash_del_source(struct macvlan_source_entry *entry)
188 hlist_del_rcu(&entry->hlist);
189 kfree_rcu(entry, rcu);
192 static void macvlan_hash_del(struct macvlan_dev *vlan, bool sync)
194 hlist_del_rcu(&vlan->hlist);
195 if (sync)
196 synchronize_rcu();
199 static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
200 const unsigned char *addr)
202 macvlan_hash_del(vlan, true);
203 /* Now that we are unhashed it is safe to change the device
204 * address without confusing packet delivery.
206 memcpy(vlan->dev->dev_addr, addr, ETH_ALEN);
207 macvlan_hash_add(vlan);
210 static bool macvlan_addr_busy(const struct macvlan_port *port,
211 const unsigned char *addr)
213 /* Test to see if the specified address is
214 * currently in use by the underlying device or
215 * another macvlan.
217 if (!macvlan_passthru(port) && !macvlan_addr_change(port) &&
218 ether_addr_equal_64bits(port->dev->dev_addr, addr))
219 return true;
221 if (macvlan_hash_lookup(port, addr))
222 return true;
224 return false;
228 static int macvlan_broadcast_one(struct sk_buff *skb,
229 const struct macvlan_dev *vlan,
230 const struct ethhdr *eth, bool local)
232 struct net_device *dev = vlan->dev;
234 if (local)
235 return __dev_forward_skb(dev, skb);
237 skb->dev = dev;
238 if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast))
239 skb->pkt_type = PACKET_BROADCAST;
240 else
241 skb->pkt_type = PACKET_MULTICAST;
243 return 0;
246 static u32 macvlan_hash_mix(const struct macvlan_dev *vlan)
248 return (u32)(((unsigned long)vlan) >> L1_CACHE_SHIFT);
252 static unsigned int mc_hash(const struct macvlan_dev *vlan,
253 const unsigned char *addr)
255 u32 val = __get_unaligned_cpu32(addr + 2);
257 val ^= macvlan_hash_mix(vlan);
258 return hash_32(val, MACVLAN_MC_FILTER_BITS);
261 static void macvlan_broadcast(struct sk_buff *skb,
262 const struct macvlan_port *port,
263 struct net_device *src,
264 enum macvlan_mode mode)
266 const struct ethhdr *eth = eth_hdr(skb);
267 const struct macvlan_dev *vlan;
268 struct sk_buff *nskb;
269 unsigned int i;
270 int err;
271 unsigned int hash;
273 if (skb->protocol == htons(ETH_P_PAUSE))
274 return;
276 for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
277 hlist_for_each_entry_rcu(vlan, &port->vlan_hash[i], hlist) {
278 if (vlan->dev == src || !(vlan->mode & mode))
279 continue;
281 hash = mc_hash(vlan, eth->h_dest);
282 if (!test_bit(hash, vlan->mc_filter))
283 continue;
285 err = NET_RX_DROP;
286 nskb = skb_clone(skb, GFP_ATOMIC);
287 if (likely(nskb))
288 err = macvlan_broadcast_one(
289 nskb, vlan, eth,
290 mode == MACVLAN_MODE_BRIDGE) ?:
291 netif_rx_ni(nskb);
292 macvlan_count_rx(vlan, skb->len + ETH_HLEN,
293 err == NET_RX_SUCCESS, true);
298 static void macvlan_process_broadcast(struct work_struct *w)
300 struct macvlan_port *port = container_of(w, struct macvlan_port,
301 bc_work);
302 struct sk_buff *skb;
303 struct sk_buff_head list;
305 __skb_queue_head_init(&list);
307 spin_lock_bh(&port->bc_queue.lock);
308 skb_queue_splice_tail_init(&port->bc_queue, &list);
309 spin_unlock_bh(&port->bc_queue.lock);
311 while ((skb = __skb_dequeue(&list))) {
312 const struct macvlan_dev *src = MACVLAN_SKB_CB(skb)->src;
314 rcu_read_lock();
316 if (!src)
317 /* frame comes from an external address */
318 macvlan_broadcast(skb, port, NULL,
319 MACVLAN_MODE_PRIVATE |
320 MACVLAN_MODE_VEPA |
321 MACVLAN_MODE_PASSTHRU|
322 MACVLAN_MODE_BRIDGE);
323 else if (src->mode == MACVLAN_MODE_VEPA)
324 /* flood to everyone except source */
325 macvlan_broadcast(skb, port, src->dev,
326 MACVLAN_MODE_VEPA |
327 MACVLAN_MODE_BRIDGE);
328 else
330 * flood only to VEPA ports, bridge ports
331 * already saw the frame on the way out.
333 macvlan_broadcast(skb, port, src->dev,
334 MACVLAN_MODE_VEPA);
336 rcu_read_unlock();
338 if (src)
339 dev_put(src->dev);
340 kfree_skb(skb);
342 cond_resched();
346 static void macvlan_broadcast_enqueue(struct macvlan_port *port,
347 const struct macvlan_dev *src,
348 struct sk_buff *skb)
350 struct sk_buff *nskb;
351 int err = -ENOMEM;
353 nskb = skb_clone(skb, GFP_ATOMIC);
354 if (!nskb)
355 goto err;
357 MACVLAN_SKB_CB(nskb)->src = src;
359 spin_lock(&port->bc_queue.lock);
360 if (skb_queue_len(&port->bc_queue) < MACVLAN_BC_QUEUE_LEN) {
361 if (src)
362 dev_hold(src->dev);
363 __skb_queue_tail(&port->bc_queue, nskb);
364 err = 0;
366 spin_unlock(&port->bc_queue.lock);
368 schedule_work(&port->bc_work);
370 if (err)
371 goto free_nskb;
373 return;
375 free_nskb:
376 kfree_skb(nskb);
377 err:
378 atomic_long_inc(&skb->dev->rx_dropped);
381 static void macvlan_flush_sources(struct macvlan_port *port,
382 struct macvlan_dev *vlan)
384 int i;
386 for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
387 struct hlist_node *h, *n;
389 hlist_for_each_safe(h, n, &port->vlan_source_hash[i]) {
390 struct macvlan_source_entry *entry;
392 entry = hlist_entry(h, struct macvlan_source_entry,
393 hlist);
394 if (entry->vlan == vlan)
395 macvlan_hash_del_source(entry);
398 vlan->macaddr_count = 0;
401 static void macvlan_forward_source_one(struct sk_buff *skb,
402 struct macvlan_dev *vlan)
404 struct sk_buff *nskb;
405 struct net_device *dev;
406 int len;
407 int ret;
409 dev = vlan->dev;
410 if (unlikely(!(dev->flags & IFF_UP)))
411 return;
413 nskb = skb_clone(skb, GFP_ATOMIC);
414 if (!nskb)
415 return;
417 len = nskb->len + ETH_HLEN;
418 nskb->dev = dev;
420 if (ether_addr_equal_64bits(eth_hdr(skb)->h_dest, dev->dev_addr))
421 nskb->pkt_type = PACKET_HOST;
423 ret = netif_rx(nskb);
424 macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, false);
427 static void macvlan_forward_source(struct sk_buff *skb,
428 struct macvlan_port *port,
429 const unsigned char *addr)
431 struct macvlan_source_entry *entry;
432 u32 idx = macvlan_eth_hash(addr);
433 struct hlist_head *h = &port->vlan_source_hash[idx];
435 hlist_for_each_entry_rcu(entry, h, hlist) {
436 if (ether_addr_equal_64bits(entry->addr, addr))
437 macvlan_forward_source_one(skb, entry->vlan);
441 /* called under rcu_read_lock() from netif_receive_skb */
442 static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb)
444 struct macvlan_port *port;
445 struct sk_buff *skb = *pskb;
446 const struct ethhdr *eth = eth_hdr(skb);
447 const struct macvlan_dev *vlan;
448 const struct macvlan_dev *src;
449 struct net_device *dev;
450 unsigned int len = 0;
451 int ret;
452 rx_handler_result_t handle_res;
454 /* Packets from dev_loopback_xmit() do not have L2 header, bail out */
455 if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
456 return RX_HANDLER_PASS;
458 port = macvlan_port_get_rcu(skb->dev);
459 if (is_multicast_ether_addr(eth->h_dest)) {
460 unsigned int hash;
462 skb = ip_check_defrag(dev_net(skb->dev), skb, IP_DEFRAG_MACVLAN);
463 if (!skb)
464 return RX_HANDLER_CONSUMED;
465 *pskb = skb;
466 eth = eth_hdr(skb);
467 macvlan_forward_source(skb, port, eth->h_source);
468 src = macvlan_hash_lookup(port, eth->h_source);
469 if (src && src->mode != MACVLAN_MODE_VEPA &&
470 src->mode != MACVLAN_MODE_BRIDGE) {
471 /* forward to original port. */
472 vlan = src;
473 ret = macvlan_broadcast_one(skb, vlan, eth, 0) ?:
474 netif_rx(skb);
475 handle_res = RX_HANDLER_CONSUMED;
476 goto out;
479 hash = mc_hash(NULL, eth->h_dest);
480 if (test_bit(hash, port->mc_filter))
481 macvlan_broadcast_enqueue(port, src, skb);
483 return RX_HANDLER_PASS;
486 macvlan_forward_source(skb, port, eth->h_source);
487 if (macvlan_passthru(port))
488 vlan = list_first_or_null_rcu(&port->vlans,
489 struct macvlan_dev, list);
490 else
491 vlan = macvlan_hash_lookup(port, eth->h_dest);
492 if (!vlan || vlan->mode == MACVLAN_MODE_SOURCE)
493 return RX_HANDLER_PASS;
495 dev = vlan->dev;
496 if (unlikely(!(dev->flags & IFF_UP))) {
497 kfree_skb(skb);
498 return RX_HANDLER_CONSUMED;
500 len = skb->len + ETH_HLEN;
501 skb = skb_share_check(skb, GFP_ATOMIC);
502 if (!skb) {
503 ret = NET_RX_DROP;
504 handle_res = RX_HANDLER_CONSUMED;
505 goto out;
508 *pskb = skb;
509 skb->dev = dev;
510 skb->pkt_type = PACKET_HOST;
512 ret = NET_RX_SUCCESS;
513 handle_res = RX_HANDLER_ANOTHER;
514 out:
515 macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, false);
516 return handle_res;
519 static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
521 const struct macvlan_dev *vlan = netdev_priv(dev);
522 const struct macvlan_port *port = vlan->port;
523 const struct macvlan_dev *dest;
525 if (vlan->mode == MACVLAN_MODE_BRIDGE) {
526 const struct ethhdr *eth = skb_eth_hdr(skb);
528 /* send to other bridge ports directly */
529 if (is_multicast_ether_addr(eth->h_dest)) {
530 skb_reset_mac_header(skb);
531 macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE);
532 goto xmit_world;
535 dest = macvlan_hash_lookup(port, eth->h_dest);
536 if (dest && dest->mode == MACVLAN_MODE_BRIDGE) {
537 /* send to lowerdev first for its network taps */
538 dev_forward_skb(vlan->lowerdev, skb);
540 return NET_XMIT_SUCCESS;
543 xmit_world:
544 skb->dev = vlan->lowerdev;
545 return dev_queue_xmit_accel(skb,
546 netdev_get_sb_channel(dev) ? dev : NULL);
549 static inline netdev_tx_t macvlan_netpoll_send_skb(struct macvlan_dev *vlan, struct sk_buff *skb)
551 #ifdef CONFIG_NET_POLL_CONTROLLER
552 if (vlan->netpoll)
553 netpoll_send_skb(vlan->netpoll, skb);
554 #else
555 BUG();
556 #endif
557 return NETDEV_TX_OK;
560 static netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
561 struct net_device *dev)
563 struct macvlan_dev *vlan = netdev_priv(dev);
564 unsigned int len = skb->len;
565 int ret;
567 if (unlikely(netpoll_tx_running(dev)))
568 return macvlan_netpoll_send_skb(vlan, skb);
570 ret = macvlan_queue_xmit(skb, dev);
572 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
573 struct vlan_pcpu_stats *pcpu_stats;
575 pcpu_stats = this_cpu_ptr(vlan->pcpu_stats);
576 u64_stats_update_begin(&pcpu_stats->syncp);
577 pcpu_stats->tx_packets++;
578 pcpu_stats->tx_bytes += len;
579 u64_stats_update_end(&pcpu_stats->syncp);
580 } else {
581 this_cpu_inc(vlan->pcpu_stats->tx_dropped);
583 return ret;
586 static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
587 unsigned short type, const void *daddr,
588 const void *saddr, unsigned len)
590 const struct macvlan_dev *vlan = netdev_priv(dev);
591 struct net_device *lowerdev = vlan->lowerdev;
593 return dev_hard_header(skb, lowerdev, type, daddr,
594 saddr ? : dev->dev_addr, len);
597 static const struct header_ops macvlan_hard_header_ops = {
598 .create = macvlan_hard_header,
599 .parse = eth_header_parse,
600 .cache = eth_header_cache,
601 .cache_update = eth_header_cache_update,
604 static int macvlan_open(struct net_device *dev)
606 struct macvlan_dev *vlan = netdev_priv(dev);
607 struct net_device *lowerdev = vlan->lowerdev;
608 int err;
610 if (macvlan_passthru(vlan->port)) {
611 if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) {
612 err = dev_set_promiscuity(lowerdev, 1);
613 if (err < 0)
614 goto out;
616 goto hash_add;
619 err = -EADDRINUSE;
620 if (macvlan_addr_busy(vlan->port, dev->dev_addr))
621 goto out;
623 /* Attempt to populate accel_priv which is used to offload the L2
624 * forwarding requests for unicast packets.
626 if (lowerdev->features & NETIF_F_HW_L2FW_DOFFLOAD)
627 vlan->accel_priv =
628 lowerdev->netdev_ops->ndo_dfwd_add_station(lowerdev, dev);
630 /* If earlier attempt to offload failed, or accel_priv is not
631 * populated we must add the unicast address to the lower device.
633 if (IS_ERR_OR_NULL(vlan->accel_priv)) {
634 vlan->accel_priv = NULL;
635 err = dev_uc_add(lowerdev, dev->dev_addr);
636 if (err < 0)
637 goto out;
640 if (dev->flags & IFF_ALLMULTI) {
641 err = dev_set_allmulti(lowerdev, 1);
642 if (err < 0)
643 goto del_unicast;
646 if (dev->flags & IFF_PROMISC) {
647 err = dev_set_promiscuity(lowerdev, 1);
648 if (err < 0)
649 goto clear_multi;
652 hash_add:
653 macvlan_hash_add(vlan);
654 return 0;
656 clear_multi:
657 if (dev->flags & IFF_ALLMULTI)
658 dev_set_allmulti(lowerdev, -1);
659 del_unicast:
660 if (vlan->accel_priv) {
661 lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev,
662 vlan->accel_priv);
663 vlan->accel_priv = NULL;
664 } else {
665 dev_uc_del(lowerdev, dev->dev_addr);
667 out:
668 return err;
671 static int macvlan_stop(struct net_device *dev)
673 struct macvlan_dev *vlan = netdev_priv(dev);
674 struct net_device *lowerdev = vlan->lowerdev;
676 if (vlan->accel_priv) {
677 lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev,
678 vlan->accel_priv);
679 vlan->accel_priv = NULL;
682 dev_uc_unsync(lowerdev, dev);
683 dev_mc_unsync(lowerdev, dev);
685 if (macvlan_passthru(vlan->port)) {
686 if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC))
687 dev_set_promiscuity(lowerdev, -1);
688 goto hash_del;
691 if (dev->flags & IFF_ALLMULTI)
692 dev_set_allmulti(lowerdev, -1);
694 if (dev->flags & IFF_PROMISC)
695 dev_set_promiscuity(lowerdev, -1);
697 dev_uc_del(lowerdev, dev->dev_addr);
699 hash_del:
700 macvlan_hash_del(vlan, !dev->dismantle);
701 return 0;
704 static int macvlan_sync_address(struct net_device *dev, unsigned char *addr)
706 struct macvlan_dev *vlan = netdev_priv(dev);
707 struct net_device *lowerdev = vlan->lowerdev;
708 struct macvlan_port *port = vlan->port;
709 int err;
711 if (!(dev->flags & IFF_UP)) {
712 /* Just copy in the new address */
713 ether_addr_copy(dev->dev_addr, addr);
714 } else {
715 /* Rehash and update the device filters */
716 if (macvlan_addr_busy(vlan->port, addr))
717 return -EADDRINUSE;
719 if (!macvlan_passthru(port)) {
720 err = dev_uc_add(lowerdev, addr);
721 if (err)
722 return err;
724 dev_uc_del(lowerdev, dev->dev_addr);
727 macvlan_hash_change_addr(vlan, addr);
729 if (macvlan_passthru(port) && !macvlan_addr_change(port)) {
730 /* Since addr_change isn't set, we are here due to lower
731 * device change. Save the lower-dev address so we can
732 * restore it later.
734 ether_addr_copy(vlan->port->perm_addr,
735 lowerdev->dev_addr);
737 macvlan_clear_addr_change(port);
738 return 0;
741 static int macvlan_set_mac_address(struct net_device *dev, void *p)
743 struct macvlan_dev *vlan = netdev_priv(dev);
744 struct sockaddr *addr = p;
746 if (!is_valid_ether_addr(addr->sa_data))
747 return -EADDRNOTAVAIL;
749 /* If the addresses are the same, this is a no-op */
750 if (ether_addr_equal(dev->dev_addr, addr->sa_data))
751 return 0;
753 if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
754 macvlan_set_addr_change(vlan->port);
755 return dev_set_mac_address(vlan->lowerdev, addr);
758 if (macvlan_addr_busy(vlan->port, addr->sa_data))
759 return -EADDRINUSE;
761 return macvlan_sync_address(dev, addr->sa_data);
764 static void macvlan_change_rx_flags(struct net_device *dev, int change)
766 struct macvlan_dev *vlan = netdev_priv(dev);
767 struct net_device *lowerdev = vlan->lowerdev;
769 if (dev->flags & IFF_UP) {
770 if (change & IFF_ALLMULTI)
771 dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
772 if (change & IFF_PROMISC)
773 dev_set_promiscuity(lowerdev,
774 dev->flags & IFF_PROMISC ? 1 : -1);
779 static void macvlan_compute_filter(unsigned long *mc_filter,
780 struct net_device *dev,
781 struct macvlan_dev *vlan)
783 if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
784 bitmap_fill(mc_filter, MACVLAN_MC_FILTER_SZ);
785 } else {
786 struct netdev_hw_addr *ha;
787 DECLARE_BITMAP(filter, MACVLAN_MC_FILTER_SZ);
789 bitmap_zero(filter, MACVLAN_MC_FILTER_SZ);
790 netdev_for_each_mc_addr(ha, dev) {
791 __set_bit(mc_hash(vlan, ha->addr), filter);
794 __set_bit(mc_hash(vlan, dev->broadcast), filter);
796 bitmap_copy(mc_filter, filter, MACVLAN_MC_FILTER_SZ);
800 static void macvlan_set_mac_lists(struct net_device *dev)
802 struct macvlan_dev *vlan = netdev_priv(dev);
804 macvlan_compute_filter(vlan->mc_filter, dev, vlan);
806 dev_uc_sync(vlan->lowerdev, dev);
807 dev_mc_sync(vlan->lowerdev, dev);
809 /* This is slightly inaccurate as we're including the subscription
810 * list of vlan->lowerdev too.
812 * Bug alert: This only works if everyone has the same broadcast
813 * address as lowerdev. As soon as someone changes theirs this
814 * will break.
816 * However, this is already broken as when you change your broadcast
817 * address we don't get called.
819 * The solution is to maintain a list of broadcast addresses like
820 * we do for uc/mc, if you care.
822 macvlan_compute_filter(vlan->port->mc_filter, vlan->lowerdev, NULL);
825 static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
827 struct macvlan_dev *vlan = netdev_priv(dev);
829 if (vlan->lowerdev->mtu < new_mtu)
830 return -EINVAL;
831 dev->mtu = new_mtu;
832 return 0;
836 * macvlan network devices have devices nesting below it and are a special
837 * "super class" of normal network devices; split their locks off into a
838 * separate class since they always nest.
840 static struct lock_class_key macvlan_netdev_addr_lock_key;
842 #define ALWAYS_ON_OFFLOADS \
843 (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE | \
844 NETIF_F_GSO_ROBUST | NETIF_F_GSO_ENCAP_ALL)
846 #define ALWAYS_ON_FEATURES (ALWAYS_ON_OFFLOADS | NETIF_F_LLTX)
848 #define MACVLAN_FEATURES \
849 (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
850 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_LRO | \
851 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
852 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
854 #define MACVLAN_STATE_MASK \
855 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
857 static int macvlan_get_nest_level(struct net_device *dev)
859 return ((struct macvlan_dev *)netdev_priv(dev))->nest_level;
862 static void macvlan_set_lockdep_class(struct net_device *dev)
864 netdev_lockdep_set_classes(dev);
865 lockdep_set_class_and_subclass(&dev->addr_list_lock,
866 &macvlan_netdev_addr_lock_key,
867 macvlan_get_nest_level(dev));
870 static int macvlan_init(struct net_device *dev)
872 struct macvlan_dev *vlan = netdev_priv(dev);
873 const struct net_device *lowerdev = vlan->lowerdev;
874 struct macvlan_port *port = vlan->port;
876 dev->state = (dev->state & ~MACVLAN_STATE_MASK) |
877 (lowerdev->state & MACVLAN_STATE_MASK);
878 dev->features = lowerdev->features & MACVLAN_FEATURES;
879 dev->features |= ALWAYS_ON_FEATURES;
880 dev->hw_features |= NETIF_F_LRO;
881 dev->vlan_features = lowerdev->vlan_features & MACVLAN_FEATURES;
882 dev->vlan_features |= ALWAYS_ON_OFFLOADS;
883 dev->hw_enc_features |= dev->features;
884 dev->gso_max_size = lowerdev->gso_max_size;
885 dev->gso_max_segs = lowerdev->gso_max_segs;
886 dev->hard_header_len = lowerdev->hard_header_len;
888 macvlan_set_lockdep_class(dev);
890 vlan->pcpu_stats = netdev_alloc_pcpu_stats(struct vlan_pcpu_stats);
891 if (!vlan->pcpu_stats)
892 return -ENOMEM;
894 port->count += 1;
896 return 0;
899 static void macvlan_uninit(struct net_device *dev)
901 struct macvlan_dev *vlan = netdev_priv(dev);
902 struct macvlan_port *port = vlan->port;
904 free_percpu(vlan->pcpu_stats);
906 macvlan_flush_sources(port, vlan);
907 port->count -= 1;
908 if (!port->count)
909 macvlan_port_destroy(port->dev);
912 static void macvlan_dev_get_stats64(struct net_device *dev,
913 struct rtnl_link_stats64 *stats)
915 struct macvlan_dev *vlan = netdev_priv(dev);
917 if (vlan->pcpu_stats) {
918 struct vlan_pcpu_stats *p;
919 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
920 u32 rx_errors = 0, tx_dropped = 0;
921 unsigned int start;
922 int i;
924 for_each_possible_cpu(i) {
925 p = per_cpu_ptr(vlan->pcpu_stats, i);
926 do {
927 start = u64_stats_fetch_begin_irq(&p->syncp);
928 rx_packets = p->rx_packets;
929 rx_bytes = p->rx_bytes;
930 rx_multicast = p->rx_multicast;
931 tx_packets = p->tx_packets;
932 tx_bytes = p->tx_bytes;
933 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
935 stats->rx_packets += rx_packets;
936 stats->rx_bytes += rx_bytes;
937 stats->multicast += rx_multicast;
938 stats->tx_packets += tx_packets;
939 stats->tx_bytes += tx_bytes;
940 /* rx_errors & tx_dropped are u32, updated
941 * without syncp protection.
943 rx_errors += p->rx_errors;
944 tx_dropped += p->tx_dropped;
946 stats->rx_errors = rx_errors;
947 stats->rx_dropped = rx_errors;
948 stats->tx_dropped = tx_dropped;
952 static int macvlan_vlan_rx_add_vid(struct net_device *dev,
953 __be16 proto, u16 vid)
955 struct macvlan_dev *vlan = netdev_priv(dev);
956 struct net_device *lowerdev = vlan->lowerdev;
958 return vlan_vid_add(lowerdev, proto, vid);
961 static int macvlan_vlan_rx_kill_vid(struct net_device *dev,
962 __be16 proto, u16 vid)
964 struct macvlan_dev *vlan = netdev_priv(dev);
965 struct net_device *lowerdev = vlan->lowerdev;
967 vlan_vid_del(lowerdev, proto, vid);
968 return 0;
971 static int macvlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
972 struct net_device *dev,
973 const unsigned char *addr, u16 vid,
974 u16 flags)
976 struct macvlan_dev *vlan = netdev_priv(dev);
977 int err = -EINVAL;
979 /* Support unicast filter only on passthru devices.
980 * Multicast filter should be allowed on all devices.
982 if (!macvlan_passthru(vlan->port) && is_unicast_ether_addr(addr))
983 return -EOPNOTSUPP;
985 if (flags & NLM_F_REPLACE)
986 return -EOPNOTSUPP;
988 if (is_unicast_ether_addr(addr))
989 err = dev_uc_add_excl(dev, addr);
990 else if (is_multicast_ether_addr(addr))
991 err = dev_mc_add_excl(dev, addr);
993 return err;
996 static int macvlan_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
997 struct net_device *dev,
998 const unsigned char *addr, u16 vid)
1000 struct macvlan_dev *vlan = netdev_priv(dev);
1001 int err = -EINVAL;
1003 /* Support unicast filter only on passthru devices.
1004 * Multicast filter should be allowed on all devices.
1006 if (!macvlan_passthru(vlan->port) && is_unicast_ether_addr(addr))
1007 return -EOPNOTSUPP;
1009 if (is_unicast_ether_addr(addr))
1010 err = dev_uc_del(dev, addr);
1011 else if (is_multicast_ether_addr(addr))
1012 err = dev_mc_del(dev, addr);
1014 return err;
1017 static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
1018 struct ethtool_drvinfo *drvinfo)
1020 strlcpy(drvinfo->driver, "macvlan", sizeof(drvinfo->driver));
1021 strlcpy(drvinfo->version, "0.1", sizeof(drvinfo->version));
1024 static int macvlan_ethtool_get_link_ksettings(struct net_device *dev,
1025 struct ethtool_link_ksettings *cmd)
1027 const struct macvlan_dev *vlan = netdev_priv(dev);
1029 return __ethtool_get_link_ksettings(vlan->lowerdev, cmd);
1032 static netdev_features_t macvlan_fix_features(struct net_device *dev,
1033 netdev_features_t features)
1035 struct macvlan_dev *vlan = netdev_priv(dev);
1036 netdev_features_t lowerdev_features = vlan->lowerdev->features;
1037 netdev_features_t mask;
1039 features |= NETIF_F_ALL_FOR_ALL;
1040 features &= (vlan->set_features | ~MACVLAN_FEATURES);
1041 mask = features;
1043 lowerdev_features &= (features | ~NETIF_F_LRO);
1044 features = netdev_increment_features(lowerdev_features, features, mask);
1045 features |= ALWAYS_ON_FEATURES;
1046 features &= (ALWAYS_ON_FEATURES | MACVLAN_FEATURES);
1048 return features;
1051 #ifdef CONFIG_NET_POLL_CONTROLLER
1052 static void macvlan_dev_poll_controller(struct net_device *dev)
1054 return;
1057 static int macvlan_dev_netpoll_setup(struct net_device *dev, struct netpoll_info *npinfo)
1059 struct macvlan_dev *vlan = netdev_priv(dev);
1060 struct net_device *real_dev = vlan->lowerdev;
1061 struct netpoll *netpoll;
1062 int err = 0;
1064 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
1065 err = -ENOMEM;
1066 if (!netpoll)
1067 goto out;
1069 err = __netpoll_setup(netpoll, real_dev);
1070 if (err) {
1071 kfree(netpoll);
1072 goto out;
1075 vlan->netpoll = netpoll;
1077 out:
1078 return err;
1081 static void macvlan_dev_netpoll_cleanup(struct net_device *dev)
1083 struct macvlan_dev *vlan = netdev_priv(dev);
1084 struct netpoll *netpoll = vlan->netpoll;
1086 if (!netpoll)
1087 return;
1089 vlan->netpoll = NULL;
1091 __netpoll_free_async(netpoll);
1093 #endif /* CONFIG_NET_POLL_CONTROLLER */
1095 static int macvlan_dev_get_iflink(const struct net_device *dev)
1097 struct macvlan_dev *vlan = netdev_priv(dev);
1099 return vlan->lowerdev->ifindex;
1102 static const struct ethtool_ops macvlan_ethtool_ops = {
1103 .get_link = ethtool_op_get_link,
1104 .get_link_ksettings = macvlan_ethtool_get_link_ksettings,
1105 .get_drvinfo = macvlan_ethtool_get_drvinfo,
1108 static const struct net_device_ops macvlan_netdev_ops = {
1109 .ndo_init = macvlan_init,
1110 .ndo_uninit = macvlan_uninit,
1111 .ndo_open = macvlan_open,
1112 .ndo_stop = macvlan_stop,
1113 .ndo_start_xmit = macvlan_start_xmit,
1114 .ndo_change_mtu = macvlan_change_mtu,
1115 .ndo_fix_features = macvlan_fix_features,
1116 .ndo_change_rx_flags = macvlan_change_rx_flags,
1117 .ndo_set_mac_address = macvlan_set_mac_address,
1118 .ndo_set_rx_mode = macvlan_set_mac_lists,
1119 .ndo_get_stats64 = macvlan_dev_get_stats64,
1120 .ndo_validate_addr = eth_validate_addr,
1121 .ndo_vlan_rx_add_vid = macvlan_vlan_rx_add_vid,
1122 .ndo_vlan_rx_kill_vid = macvlan_vlan_rx_kill_vid,
1123 .ndo_fdb_add = macvlan_fdb_add,
1124 .ndo_fdb_del = macvlan_fdb_del,
1125 .ndo_fdb_dump = ndo_dflt_fdb_dump,
1126 .ndo_get_lock_subclass = macvlan_get_nest_level,
1127 #ifdef CONFIG_NET_POLL_CONTROLLER
1128 .ndo_poll_controller = macvlan_dev_poll_controller,
1129 .ndo_netpoll_setup = macvlan_dev_netpoll_setup,
1130 .ndo_netpoll_cleanup = macvlan_dev_netpoll_cleanup,
1131 #endif
1132 .ndo_get_iflink = macvlan_dev_get_iflink,
1133 .ndo_features_check = passthru_features_check,
1136 void macvlan_common_setup(struct net_device *dev)
1138 ether_setup(dev);
1140 dev->min_mtu = 0;
1141 dev->max_mtu = ETH_MAX_MTU;
1142 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1143 netif_keep_dst(dev);
1144 dev->priv_flags |= IFF_UNICAST_FLT;
1145 dev->netdev_ops = &macvlan_netdev_ops;
1146 dev->needs_free_netdev = true;
1147 dev->header_ops = &macvlan_hard_header_ops;
1148 dev->ethtool_ops = &macvlan_ethtool_ops;
1150 EXPORT_SYMBOL_GPL(macvlan_common_setup);
1152 static void macvlan_setup(struct net_device *dev)
1154 macvlan_common_setup(dev);
1155 dev->priv_flags |= IFF_NO_QUEUE;
1158 static int macvlan_port_create(struct net_device *dev)
1160 struct macvlan_port *port;
1161 unsigned int i;
1162 int err;
1164 if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
1165 return -EINVAL;
1167 if (netdev_is_rx_handler_busy(dev))
1168 return -EBUSY;
1170 port = kzalloc(sizeof(*port), GFP_KERNEL);
1171 if (port == NULL)
1172 return -ENOMEM;
1174 port->dev = dev;
1175 ether_addr_copy(port->perm_addr, dev->dev_addr);
1176 INIT_LIST_HEAD(&port->vlans);
1177 for (i = 0; i < MACVLAN_HASH_SIZE; i++)
1178 INIT_HLIST_HEAD(&port->vlan_hash[i]);
1179 for (i = 0; i < MACVLAN_HASH_SIZE; i++)
1180 INIT_HLIST_HEAD(&port->vlan_source_hash[i]);
1182 skb_queue_head_init(&port->bc_queue);
1183 INIT_WORK(&port->bc_work, macvlan_process_broadcast);
1185 err = netdev_rx_handler_register(dev, macvlan_handle_frame, port);
1186 if (err)
1187 kfree(port);
1188 else
1189 dev->priv_flags |= IFF_MACVLAN_PORT;
1190 return err;
1193 static void macvlan_port_destroy(struct net_device *dev)
1195 struct macvlan_port *port = macvlan_port_get_rtnl(dev);
1196 struct sk_buff *skb;
1198 dev->priv_flags &= ~IFF_MACVLAN_PORT;
1199 netdev_rx_handler_unregister(dev);
1201 /* After this point, no packet can schedule bc_work anymore,
1202 * but we need to cancel it and purge left skbs if any.
1204 cancel_work_sync(&port->bc_work);
1206 while ((skb = __skb_dequeue(&port->bc_queue))) {
1207 const struct macvlan_dev *src = MACVLAN_SKB_CB(skb)->src;
1209 if (src)
1210 dev_put(src->dev);
1212 kfree_skb(skb);
1215 /* If the lower device address has been changed by passthru
1216 * macvlan, put it back.
1218 if (macvlan_passthru(port) &&
1219 !ether_addr_equal(port->dev->dev_addr, port->perm_addr)) {
1220 struct sockaddr sa;
1222 sa.sa_family = port->dev->type;
1223 memcpy(&sa.sa_data, port->perm_addr, port->dev->addr_len);
1224 dev_set_mac_address(port->dev, &sa);
1227 kfree(port);
1230 static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[],
1231 struct netlink_ext_ack *extack)
1233 if (tb[IFLA_ADDRESS]) {
1234 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1235 return -EINVAL;
1236 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1237 return -EADDRNOTAVAIL;
1240 if (!data)
1241 return 0;
1243 if (data[IFLA_MACVLAN_FLAGS] &&
1244 nla_get_u16(data[IFLA_MACVLAN_FLAGS]) & ~MACVLAN_FLAG_NOPROMISC)
1245 return -EINVAL;
1247 if (data[IFLA_MACVLAN_MODE]) {
1248 switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) {
1249 case MACVLAN_MODE_PRIVATE:
1250 case MACVLAN_MODE_VEPA:
1251 case MACVLAN_MODE_BRIDGE:
1252 case MACVLAN_MODE_PASSTHRU:
1253 case MACVLAN_MODE_SOURCE:
1254 break;
1255 default:
1256 return -EINVAL;
1260 if (data[IFLA_MACVLAN_MACADDR_MODE]) {
1261 switch (nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE])) {
1262 case MACVLAN_MACADDR_ADD:
1263 case MACVLAN_MACADDR_DEL:
1264 case MACVLAN_MACADDR_FLUSH:
1265 case MACVLAN_MACADDR_SET:
1266 break;
1267 default:
1268 return -EINVAL;
1272 if (data[IFLA_MACVLAN_MACADDR]) {
1273 if (nla_len(data[IFLA_MACVLAN_MACADDR]) != ETH_ALEN)
1274 return -EINVAL;
1276 if (!is_valid_ether_addr(nla_data(data[IFLA_MACVLAN_MACADDR])))
1277 return -EADDRNOTAVAIL;
1280 if (data[IFLA_MACVLAN_MACADDR_COUNT])
1281 return -EINVAL;
1283 return 0;
1287 * reconfigure list of remote source mac address
1288 * (only for macvlan devices in source mode)
1289 * Note regarding alignment: all netlink data is aligned to 4 Byte, which
1290 * suffices for both ether_addr_copy and ether_addr_equal_64bits usage.
1292 static int macvlan_changelink_sources(struct macvlan_dev *vlan, u32 mode,
1293 struct nlattr *data[])
1295 char *addr = NULL;
1296 int ret, rem, len;
1297 struct nlattr *nla, *head;
1298 struct macvlan_source_entry *entry;
1300 if (data[IFLA_MACVLAN_MACADDR])
1301 addr = nla_data(data[IFLA_MACVLAN_MACADDR]);
1303 if (mode == MACVLAN_MACADDR_ADD) {
1304 if (!addr)
1305 return -EINVAL;
1307 return macvlan_hash_add_source(vlan, addr);
1309 } else if (mode == MACVLAN_MACADDR_DEL) {
1310 if (!addr)
1311 return -EINVAL;
1313 entry = macvlan_hash_lookup_source(vlan, addr);
1314 if (entry) {
1315 macvlan_hash_del_source(entry);
1316 vlan->macaddr_count--;
1318 } else if (mode == MACVLAN_MACADDR_FLUSH) {
1319 macvlan_flush_sources(vlan->port, vlan);
1320 } else if (mode == MACVLAN_MACADDR_SET) {
1321 macvlan_flush_sources(vlan->port, vlan);
1323 if (addr) {
1324 ret = macvlan_hash_add_source(vlan, addr);
1325 if (ret)
1326 return ret;
1329 if (!data || !data[IFLA_MACVLAN_MACADDR_DATA])
1330 return 0;
1332 head = nla_data(data[IFLA_MACVLAN_MACADDR_DATA]);
1333 len = nla_len(data[IFLA_MACVLAN_MACADDR_DATA]);
1335 nla_for_each_attr(nla, head, len, rem) {
1336 if (nla_type(nla) != IFLA_MACVLAN_MACADDR ||
1337 nla_len(nla) != ETH_ALEN)
1338 continue;
1340 addr = nla_data(nla);
1341 ret = macvlan_hash_add_source(vlan, addr);
1342 if (ret)
1343 return ret;
1345 } else {
1346 return -EINVAL;
1349 return 0;
1352 int macvlan_common_newlink(struct net *src_net, struct net_device *dev,
1353 struct nlattr *tb[], struct nlattr *data[],
1354 struct netlink_ext_ack *extack)
1356 struct macvlan_dev *vlan = netdev_priv(dev);
1357 struct macvlan_port *port;
1358 struct net_device *lowerdev;
1359 int err;
1360 int macmode;
1361 bool create = false;
1363 if (!tb[IFLA_LINK])
1364 return -EINVAL;
1366 lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
1367 if (lowerdev == NULL)
1368 return -ENODEV;
1370 /* When creating macvlans or macvtaps on top of other macvlans - use
1371 * the real device as the lowerdev.
1373 if (netif_is_macvlan(lowerdev))
1374 lowerdev = macvlan_dev_real_dev(lowerdev);
1376 if (!tb[IFLA_MTU])
1377 dev->mtu = lowerdev->mtu;
1378 else if (dev->mtu > lowerdev->mtu)
1379 return -EINVAL;
1381 /* MTU range: 68 - lowerdev->max_mtu */
1382 dev->min_mtu = ETH_MIN_MTU;
1383 dev->max_mtu = lowerdev->max_mtu;
1385 if (!tb[IFLA_ADDRESS])
1386 eth_hw_addr_random(dev);
1388 if (!macvlan_port_exists(lowerdev)) {
1389 err = macvlan_port_create(lowerdev);
1390 if (err < 0)
1391 return err;
1392 create = true;
1394 port = macvlan_port_get_rtnl(lowerdev);
1396 /* Only 1 macvlan device can be created in passthru mode */
1397 if (macvlan_passthru(port)) {
1398 /* The macvlan port must be not created this time,
1399 * still goto destroy_macvlan_port for readability.
1401 err = -EINVAL;
1402 goto destroy_macvlan_port;
1405 vlan->lowerdev = lowerdev;
1406 vlan->dev = dev;
1407 vlan->port = port;
1408 vlan->set_features = MACVLAN_FEATURES;
1409 vlan->nest_level = dev_get_nest_level(lowerdev) + 1;
1411 vlan->mode = MACVLAN_MODE_VEPA;
1412 if (data && data[IFLA_MACVLAN_MODE])
1413 vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
1415 if (data && data[IFLA_MACVLAN_FLAGS])
1416 vlan->flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
1418 if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
1419 if (port->count) {
1420 err = -EINVAL;
1421 goto destroy_macvlan_port;
1423 macvlan_set_passthru(port);
1424 eth_hw_addr_inherit(dev, lowerdev);
1427 if (data && data[IFLA_MACVLAN_MACADDR_MODE]) {
1428 if (vlan->mode != MACVLAN_MODE_SOURCE) {
1429 err = -EINVAL;
1430 goto destroy_macvlan_port;
1432 macmode = nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE]);
1433 err = macvlan_changelink_sources(vlan, macmode, data);
1434 if (err)
1435 goto destroy_macvlan_port;
1438 err = register_netdevice(dev);
1439 if (err < 0)
1440 goto destroy_macvlan_port;
1442 dev->priv_flags |= IFF_MACVLAN;
1443 err = netdev_upper_dev_link(lowerdev, dev, extack);
1444 if (err)
1445 goto unregister_netdev;
1447 list_add_tail_rcu(&vlan->list, &port->vlans);
1448 netif_stacked_transfer_operstate(lowerdev, dev);
1449 linkwatch_fire_event(dev);
1451 return 0;
1453 unregister_netdev:
1454 /* macvlan_uninit would free the macvlan port */
1455 unregister_netdevice(dev);
1456 return err;
1457 destroy_macvlan_port:
1458 /* the macvlan port may be freed by macvlan_uninit when fail to register.
1459 * so we destroy the macvlan port only when it's valid.
1461 if (create && macvlan_port_get_rtnl(lowerdev))
1462 macvlan_port_destroy(port->dev);
1463 return err;
1465 EXPORT_SYMBOL_GPL(macvlan_common_newlink);
1467 static int macvlan_newlink(struct net *src_net, struct net_device *dev,
1468 struct nlattr *tb[], struct nlattr *data[],
1469 struct netlink_ext_ack *extack)
1471 return macvlan_common_newlink(src_net, dev, tb, data, extack);
1474 void macvlan_dellink(struct net_device *dev, struct list_head *head)
1476 struct macvlan_dev *vlan = netdev_priv(dev);
1478 if (vlan->mode == MACVLAN_MODE_SOURCE)
1479 macvlan_flush_sources(vlan->port, vlan);
1480 list_del_rcu(&vlan->list);
1481 unregister_netdevice_queue(dev, head);
1482 netdev_upper_dev_unlink(vlan->lowerdev, dev);
1484 EXPORT_SYMBOL_GPL(macvlan_dellink);
1486 static int macvlan_changelink(struct net_device *dev,
1487 struct nlattr *tb[], struct nlattr *data[],
1488 struct netlink_ext_ack *extack)
1490 struct macvlan_dev *vlan = netdev_priv(dev);
1491 enum macvlan_mode mode;
1492 bool set_mode = false;
1493 enum macvlan_macaddr_mode macmode;
1494 int ret;
1496 /* Validate mode, but don't set yet: setting flags may fail. */
1497 if (data && data[IFLA_MACVLAN_MODE]) {
1498 set_mode = true;
1499 mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
1500 /* Passthrough mode can't be set or cleared dynamically */
1501 if ((mode == MACVLAN_MODE_PASSTHRU) !=
1502 (vlan->mode == MACVLAN_MODE_PASSTHRU))
1503 return -EINVAL;
1504 if (vlan->mode == MACVLAN_MODE_SOURCE &&
1505 vlan->mode != mode)
1506 macvlan_flush_sources(vlan->port, vlan);
1509 if (data && data[IFLA_MACVLAN_FLAGS]) {
1510 __u16 flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
1511 bool promisc = (flags ^ vlan->flags) & MACVLAN_FLAG_NOPROMISC;
1512 if (macvlan_passthru(vlan->port) && promisc) {
1513 int err;
1515 if (flags & MACVLAN_FLAG_NOPROMISC)
1516 err = dev_set_promiscuity(vlan->lowerdev, -1);
1517 else
1518 err = dev_set_promiscuity(vlan->lowerdev, 1);
1519 if (err < 0)
1520 return err;
1522 vlan->flags = flags;
1524 if (set_mode)
1525 vlan->mode = mode;
1526 if (data && data[IFLA_MACVLAN_MACADDR_MODE]) {
1527 if (vlan->mode != MACVLAN_MODE_SOURCE)
1528 return -EINVAL;
1529 macmode = nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE]);
1530 ret = macvlan_changelink_sources(vlan, macmode, data);
1531 if (ret)
1532 return ret;
1534 return 0;
1537 static size_t macvlan_get_size_mac(const struct macvlan_dev *vlan)
1539 if (vlan->macaddr_count == 0)
1540 return 0;
1541 return nla_total_size(0) /* IFLA_MACVLAN_MACADDR_DATA */
1542 + vlan->macaddr_count * nla_total_size(sizeof(u8) * ETH_ALEN);
1545 static size_t macvlan_get_size(const struct net_device *dev)
1547 struct macvlan_dev *vlan = netdev_priv(dev);
1549 return (0
1550 + nla_total_size(4) /* IFLA_MACVLAN_MODE */
1551 + nla_total_size(2) /* IFLA_MACVLAN_FLAGS */
1552 + nla_total_size(4) /* IFLA_MACVLAN_MACADDR_COUNT */
1553 + macvlan_get_size_mac(vlan) /* IFLA_MACVLAN_MACADDR */
1557 static int macvlan_fill_info_macaddr(struct sk_buff *skb,
1558 const struct macvlan_dev *vlan,
1559 const int i)
1561 struct hlist_head *h = &vlan->port->vlan_source_hash[i];
1562 struct macvlan_source_entry *entry;
1564 hlist_for_each_entry_rcu(entry, h, hlist) {
1565 if (entry->vlan != vlan)
1566 continue;
1567 if (nla_put(skb, IFLA_MACVLAN_MACADDR, ETH_ALEN, entry->addr))
1568 return 1;
1570 return 0;
1573 static int macvlan_fill_info(struct sk_buff *skb,
1574 const struct net_device *dev)
1576 struct macvlan_dev *vlan = netdev_priv(dev);
1577 int i;
1578 struct nlattr *nest;
1580 if (nla_put_u32(skb, IFLA_MACVLAN_MODE, vlan->mode))
1581 goto nla_put_failure;
1582 if (nla_put_u16(skb, IFLA_MACVLAN_FLAGS, vlan->flags))
1583 goto nla_put_failure;
1584 if (nla_put_u32(skb, IFLA_MACVLAN_MACADDR_COUNT, vlan->macaddr_count))
1585 goto nla_put_failure;
1586 if (vlan->macaddr_count > 0) {
1587 nest = nla_nest_start(skb, IFLA_MACVLAN_MACADDR_DATA);
1588 if (nest == NULL)
1589 goto nla_put_failure;
1591 for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
1592 if (macvlan_fill_info_macaddr(skb, vlan, i))
1593 goto nla_put_failure;
1595 nla_nest_end(skb, nest);
1597 return 0;
1599 nla_put_failure:
1600 return -EMSGSIZE;
1603 static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = {
1604 [IFLA_MACVLAN_MODE] = { .type = NLA_U32 },
1605 [IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 },
1606 [IFLA_MACVLAN_MACADDR_MODE] = { .type = NLA_U32 },
1607 [IFLA_MACVLAN_MACADDR] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1608 [IFLA_MACVLAN_MACADDR_DATA] = { .type = NLA_NESTED },
1609 [IFLA_MACVLAN_MACADDR_COUNT] = { .type = NLA_U32 },
1612 int macvlan_link_register(struct rtnl_link_ops *ops)
1614 /* common fields */
1615 ops->validate = macvlan_validate;
1616 ops->maxtype = IFLA_MACVLAN_MAX;
1617 ops->policy = macvlan_policy;
1618 ops->changelink = macvlan_changelink;
1619 ops->get_size = macvlan_get_size;
1620 ops->fill_info = macvlan_fill_info;
1622 return rtnl_link_register(ops);
1624 EXPORT_SYMBOL_GPL(macvlan_link_register);
1626 static struct net *macvlan_get_link_net(const struct net_device *dev)
1628 return dev_net(macvlan_dev_real_dev(dev));
1631 static struct rtnl_link_ops macvlan_link_ops = {
1632 .kind = "macvlan",
1633 .setup = macvlan_setup,
1634 .newlink = macvlan_newlink,
1635 .dellink = macvlan_dellink,
1636 .get_link_net = macvlan_get_link_net,
1637 .priv_size = sizeof(struct macvlan_dev),
1640 static int macvlan_device_event(struct notifier_block *unused,
1641 unsigned long event, void *ptr)
1643 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1644 struct macvlan_dev *vlan, *next;
1645 struct macvlan_port *port;
1646 LIST_HEAD(list_kill);
1648 if (!macvlan_port_exists(dev))
1649 return NOTIFY_DONE;
1651 port = macvlan_port_get_rtnl(dev);
1653 switch (event) {
1654 case NETDEV_UP:
1655 case NETDEV_DOWN:
1656 case NETDEV_CHANGE:
1657 list_for_each_entry(vlan, &port->vlans, list)
1658 netif_stacked_transfer_operstate(vlan->lowerdev,
1659 vlan->dev);
1660 break;
1661 case NETDEV_FEAT_CHANGE:
1662 list_for_each_entry(vlan, &port->vlans, list) {
1663 vlan->dev->gso_max_size = dev->gso_max_size;
1664 vlan->dev->gso_max_segs = dev->gso_max_segs;
1665 netdev_update_features(vlan->dev);
1667 break;
1668 case NETDEV_CHANGEMTU:
1669 list_for_each_entry(vlan, &port->vlans, list) {
1670 if (vlan->dev->mtu <= dev->mtu)
1671 continue;
1672 dev_set_mtu(vlan->dev, dev->mtu);
1674 break;
1675 case NETDEV_CHANGEADDR:
1676 if (!macvlan_passthru(port))
1677 return NOTIFY_DONE;
1679 vlan = list_first_entry_or_null(&port->vlans,
1680 struct macvlan_dev,
1681 list);
1683 if (vlan && macvlan_sync_address(vlan->dev, dev->dev_addr))
1684 return NOTIFY_BAD;
1686 break;
1687 case NETDEV_UNREGISTER:
1688 /* twiddle thumbs on netns device moves */
1689 if (dev->reg_state != NETREG_UNREGISTERING)
1690 break;
1692 list_for_each_entry_safe(vlan, next, &port->vlans, list)
1693 vlan->dev->rtnl_link_ops->dellink(vlan->dev, &list_kill);
1694 unregister_netdevice_many(&list_kill);
1695 break;
1696 case NETDEV_PRE_TYPE_CHANGE:
1697 /* Forbid underlaying device to change its type. */
1698 return NOTIFY_BAD;
1700 case NETDEV_NOTIFY_PEERS:
1701 case NETDEV_BONDING_FAILOVER:
1702 case NETDEV_RESEND_IGMP:
1703 /* Propagate to all vlans */
1704 list_for_each_entry(vlan, &port->vlans, list)
1705 call_netdevice_notifiers(event, vlan->dev);
1707 return NOTIFY_DONE;
1710 static struct notifier_block macvlan_notifier_block __read_mostly = {
1711 .notifier_call = macvlan_device_event,
1714 static int __init macvlan_init_module(void)
1716 int err;
1718 register_netdevice_notifier(&macvlan_notifier_block);
1720 err = macvlan_link_register(&macvlan_link_ops);
1721 if (err < 0)
1722 goto err1;
1723 return 0;
1724 err1:
1725 unregister_netdevice_notifier(&macvlan_notifier_block);
1726 return err;
1729 static void __exit macvlan_cleanup_module(void)
1731 rtnl_link_unregister(&macvlan_link_ops);
1732 unregister_netdevice_notifier(&macvlan_notifier_block);
1735 module_init(macvlan_init_module);
1736 module_exit(macvlan_cleanup_module);
1738 MODULE_LICENSE("GPL");
1739 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
1740 MODULE_DESCRIPTION("Driver for MAC address based VLANs");
1741 MODULE_ALIAS_RTNL_LINK("macvlan");