1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2007 Patrick McHardy <kaber@trash.net>
5 * The code this is based on carried the following copyright notice:
7 * (C) Copyright 2001-2006
8 * Alex Zeffertt, Cambridge Broadband Ltd, ajz@cambridgebroadband.com
9 * Re-worked by Ben Greear <greearb@candelatech.com>
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>
33 #include <linux/netpoll.h>
34 #include <linux/phy.h>
36 #define MACVLAN_HASH_BITS 8
37 #define MACVLAN_HASH_SIZE (1<<MACVLAN_HASH_BITS)
38 #define MACVLAN_DEFAULT_BC_QUEUE_LEN 1000
40 #define MACVLAN_F_PASSTHRU 1
41 #define MACVLAN_F_ADDRCHANGE 2
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;
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];
59 struct macvlan_source_entry {
60 struct hlist_node hlist;
61 struct macvlan_dev *vlan;
62 unsigned char addr[6+2] __aligned(sizeof(u16));
66 struct macvlan_skb_cb {
67 const struct macvlan_dev *src;
70 #define MACVLAN_SKB_CB(__skb) ((struct macvlan_skb_cb *)&((__skb)->cb[0]))
72 static void macvlan_port_destroy(struct net_device *dev);
73 static void update_port_bc_queue_len(struct macvlan_port *port);
75 static inline bool macvlan_passthru(const struct macvlan_port *port)
77 return port->flags & MACVLAN_F_PASSTHRU;
80 static inline void macvlan_set_passthru(struct macvlan_port *port)
82 port->flags |= MACVLAN_F_PASSTHRU;
85 static inline bool macvlan_addr_change(const struct macvlan_port *port)
87 return port->flags & MACVLAN_F_ADDRCHANGE;
90 static inline void macvlan_set_addr_change(struct macvlan_port *port)
92 port->flags |= MACVLAN_F_ADDRCHANGE;
95 static inline void macvlan_clear_addr_change(struct macvlan_port *port)
97 port->flags &= ~MACVLAN_F_ADDRCHANGE;
100 /* Hash Ethernet address */
101 static u32 macvlan_eth_hash(const unsigned char *addr)
103 u64 value = get_unaligned((u64 *)addr);
105 /* only want 6 bytes */
111 return hash_64(value, MACVLAN_HASH_BITS);
114 static struct macvlan_port *macvlan_port_get_rcu(const struct net_device *dev)
116 return rcu_dereference(dev->rx_handler_data);
119 static struct macvlan_port *macvlan_port_get_rtnl(const struct net_device *dev)
121 return rtnl_dereference(dev->rx_handler_data);
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 lockdep_rtnl_is_held()) {
132 if (ether_addr_equal_64bits(vlan->dev->dev_addr, addr))
138 static struct macvlan_source_entry *macvlan_hash_lookup_source(
139 const struct macvlan_dev *vlan,
140 const unsigned char *addr)
142 struct macvlan_source_entry *entry;
143 u32 idx = macvlan_eth_hash(addr);
144 struct hlist_head *h = &vlan->port->vlan_source_hash[idx];
146 hlist_for_each_entry_rcu(entry, h, hlist, lockdep_rtnl_is_held()) {
147 if (ether_addr_equal_64bits(entry->addr, addr) &&
154 static int macvlan_hash_add_source(struct macvlan_dev *vlan,
155 const unsigned char *addr)
157 struct macvlan_port *port = vlan->port;
158 struct macvlan_source_entry *entry;
159 struct hlist_head *h;
161 entry = macvlan_hash_lookup_source(vlan, addr);
165 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
169 ether_addr_copy(entry->addr, addr);
171 h = &port->vlan_source_hash[macvlan_eth_hash(addr)];
172 hlist_add_head_rcu(&entry->hlist, h);
173 vlan->macaddr_count++;
178 static void macvlan_hash_add(struct macvlan_dev *vlan)
180 struct macvlan_port *port = vlan->port;
181 const unsigned char *addr = vlan->dev->dev_addr;
182 u32 idx = macvlan_eth_hash(addr);
184 hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[idx]);
187 static void macvlan_hash_del_source(struct macvlan_source_entry *entry)
189 hlist_del_rcu(&entry->hlist);
190 kfree_rcu(entry, rcu);
193 static void macvlan_hash_del(struct macvlan_dev *vlan, bool sync)
195 hlist_del_rcu(&vlan->hlist);
200 static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
201 const unsigned char *addr)
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.
207 eth_hw_addr_set(vlan->dev, addr);
208 macvlan_hash_add(vlan);
211 static bool macvlan_addr_busy(const struct macvlan_port *port,
212 const unsigned char *addr)
214 /* Test to see if the specified address is
215 * currently in use by the underlying device or
218 if (!macvlan_passthru(port) && !macvlan_addr_change(port) &&
219 ether_addr_equal_64bits(port->dev->dev_addr, addr))
222 if (macvlan_hash_lookup(port, addr))
229 static int macvlan_broadcast_one(struct sk_buff *skb,
230 const struct macvlan_dev *vlan,
231 const struct ethhdr *eth, bool local)
233 struct net_device *dev = vlan->dev;
236 return __dev_forward_skb(dev, skb);
239 if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast))
240 skb->pkt_type = PACKET_BROADCAST;
242 skb->pkt_type = PACKET_MULTICAST;
247 static u32 macvlan_hash_mix(const struct macvlan_dev *vlan)
249 return (u32)(((unsigned long)vlan) >> L1_CACHE_SHIFT);
253 static unsigned int mc_hash(const struct macvlan_dev *vlan,
254 const unsigned char *addr)
256 u32 val = __get_unaligned_cpu32(addr + 2);
258 val ^= macvlan_hash_mix(vlan);
259 return hash_32(val, MACVLAN_MC_FILTER_BITS);
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)
267 const struct ethhdr *eth = eth_hdr(skb);
268 const struct macvlan_dev *vlan;
269 struct sk_buff *nskb;
274 if (skb->protocol == htons(ETH_P_PAUSE))
277 hash_for_each_rcu(port->vlan_hash, i, vlan, hlist) {
278 if (vlan->dev == src || !(vlan->mode & mode))
281 hash = mc_hash(vlan, eth->h_dest);
282 if (!test_bit(hash, vlan->mc_filter))
286 nskb = skb_clone(skb, GFP_ATOMIC);
288 err = macvlan_broadcast_one(nskb, vlan, eth,
289 mode == MACVLAN_MODE_BRIDGE) ?:
291 macvlan_count_rx(vlan, skb->len + ETH_HLEN,
292 err == NET_RX_SUCCESS, true);
296 static void macvlan_multicast_rx(const struct macvlan_port *port,
297 const struct macvlan_dev *src,
301 /* frame comes from an external address */
302 macvlan_broadcast(skb, port, NULL,
303 MACVLAN_MODE_PRIVATE |
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,
311 MACVLAN_MODE_BRIDGE);
314 * flood only to VEPA ports, bridge ports
315 * already saw the frame on the way out.
317 macvlan_broadcast(skb, port, src->dev,
321 static void macvlan_process_broadcast(struct work_struct *w)
323 struct macvlan_port *port = container_of(w, struct macvlan_port,
326 struct sk_buff_head list;
328 __skb_queue_head_init(&list);
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);
334 while ((skb = __skb_dequeue(&list))) {
335 const struct macvlan_dev *src = MACVLAN_SKB_CB(skb)->src;
338 macvlan_multicast_rx(port, src, skb);
349 static void macvlan_broadcast_enqueue(struct macvlan_port *port,
350 const struct macvlan_dev *src,
353 struct sk_buff *nskb;
356 nskb = skb_clone(skb, GFP_ATOMIC);
360 MACVLAN_SKB_CB(nskb)->src = src;
362 spin_lock(&port->bc_queue.lock);
363 if (skb_queue_len(&port->bc_queue) < port->bc_queue_len_used) {
366 __skb_queue_tail(&port->bc_queue, nskb);
369 spin_unlock(&port->bc_queue.lock);
371 queue_work(system_unbound_wq, &port->bc_work);
381 dev_core_stats_rx_dropped_inc(skb->dev);
384 static void macvlan_flush_sources(struct macvlan_port *port,
385 struct macvlan_dev *vlan)
387 struct macvlan_source_entry *entry;
388 struct hlist_node *next;
391 hash_for_each_safe(port->vlan_source_hash, i, next, entry, hlist)
392 if (entry->vlan == vlan)
393 macvlan_hash_del_source(entry);
395 vlan->macaddr_count = 0;
398 static void macvlan_forward_source_one(struct sk_buff *skb,
399 struct macvlan_dev *vlan)
401 struct sk_buff *nskb;
402 struct net_device *dev;
407 if (unlikely(!(dev->flags & IFF_UP)))
410 nskb = skb_clone(skb, GFP_ATOMIC);
414 len = nskb->len + ETH_HLEN;
417 if (ether_addr_equal_64bits(eth_hdr(skb)->h_dest, dev->dev_addr))
418 nskb->pkt_type = PACKET_HOST;
420 ret = __netif_rx(nskb);
421 macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, false);
424 static bool macvlan_forward_source(struct sk_buff *skb,
425 struct macvlan_port *port,
426 const unsigned char *addr)
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;
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)
437 macvlan_forward_source_one(skb, entry->vlan);
444 /* called under rcu_read_lock() from netif_receive_skb */
445 static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb)
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;
455 rx_handler_result_t handle_res;
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;
461 port = macvlan_port_get_rcu(skb->dev);
462 if (is_multicast_ether_addr(eth->h_dest)) {
465 skb = ip_check_defrag(dev_net(skb->dev), skb, IP_DEFRAG_MACVLAN);
467 return RX_HANDLER_CONSUMED;
470 if (macvlan_forward_source(skb, port, eth->h_source)) {
472 return RX_HANDLER_CONSUMED;
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. */
479 ret = macvlan_broadcast_one(skb, vlan, eth, 0) ?:
481 handle_res = RX_HANDLER_CONSUMED;
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);
491 return RX_HANDLER_PASS;
494 if (macvlan_forward_source(skb, port, eth->h_source)) {
496 return RX_HANDLER_CONSUMED;
498 if (macvlan_passthru(port))
499 vlan = list_first_or_null_rcu(&port->vlans,
500 struct macvlan_dev, list);
502 vlan = macvlan_hash_lookup(port, eth->h_dest);
503 if (!vlan || vlan->mode == MACVLAN_MODE_SOURCE)
504 return RX_HANDLER_PASS;
507 if (unlikely(!(dev->flags & IFF_UP))) {
509 return RX_HANDLER_CONSUMED;
511 len = skb->len + ETH_HLEN;
512 skb = skb_share_check(skb, GFP_ATOMIC);
515 handle_res = RX_HANDLER_CONSUMED;
521 skb->pkt_type = PACKET_HOST;
523 ret = NET_RX_SUCCESS;
524 handle_res = RX_HANDLER_ANOTHER;
526 macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, false);
530 static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
532 const struct macvlan_dev *vlan = netdev_priv(dev);
533 const struct macvlan_port *port = vlan->port;
534 const struct macvlan_dev *dest;
536 if (vlan->mode == MACVLAN_MODE_BRIDGE) {
537 const struct ethhdr *eth = skb_eth_hdr(skb);
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);
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);
551 return NET_XMIT_SUCCESS;
555 skb->dev = vlan->lowerdev;
556 return dev_queue_xmit_accel(skb,
557 netdev_get_sb_channel(dev) ? dev : NULL);
560 static inline netdev_tx_t macvlan_netpoll_send_skb(struct macvlan_dev *vlan, struct sk_buff *skb)
562 #ifdef CONFIG_NET_POLL_CONTROLLER
563 return netpoll_send_skb(vlan->netpoll, skb);
570 static netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
571 struct net_device *dev)
573 struct macvlan_dev *vlan = netdev_priv(dev);
574 unsigned int len = skb->len;
577 if (unlikely(netpoll_tx_running(dev)))
578 return macvlan_netpoll_send_skb(vlan, skb);
580 ret = macvlan_queue_xmit(skb, dev);
582 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
583 struct vlan_pcpu_stats *pcpu_stats;
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);
591 this_cpu_inc(vlan->pcpu_stats->tx_dropped);
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)
600 const struct macvlan_dev *vlan = netdev_priv(dev);
601 struct net_device *lowerdev = vlan->lowerdev;
603 return dev_hard_header(skb, lowerdev, type, daddr,
604 saddr ? : dev->dev_addr, len);
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,
614 static int macvlan_open(struct net_device *dev)
616 struct macvlan_dev *vlan = netdev_priv(dev);
617 struct net_device *lowerdev = vlan->lowerdev;
620 if (macvlan_passthru(vlan->port)) {
621 if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) {
622 err = dev_set_promiscuity(lowerdev, 1);
630 if (macvlan_addr_busy(vlan->port, dev->dev_addr))
633 /* Attempt to populate accel_priv which is used to offload the L2
634 * forwarding requests for unicast packets.
636 if (lowerdev->features & NETIF_F_HW_L2FW_DOFFLOAD)
638 lowerdev->netdev_ops->ndo_dfwd_add_station(lowerdev, dev);
640 /* If earlier attempt to offload failed, or accel_priv is not
641 * populated we must add the unicast address to the lower device.
643 if (IS_ERR_OR_NULL(vlan->accel_priv)) {
644 vlan->accel_priv = NULL;
645 err = dev_uc_add(lowerdev, dev->dev_addr);
650 if (dev->flags & IFF_ALLMULTI) {
651 err = dev_set_allmulti(lowerdev, 1);
656 if (dev->flags & IFF_PROMISC) {
657 err = dev_set_promiscuity(lowerdev, 1);
663 macvlan_hash_add(vlan);
667 if (dev->flags & IFF_ALLMULTI)
668 dev_set_allmulti(lowerdev, -1);
670 if (vlan->accel_priv) {
671 lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev,
673 vlan->accel_priv = NULL;
675 dev_uc_del(lowerdev, dev->dev_addr);
681 static int macvlan_stop(struct net_device *dev)
683 struct macvlan_dev *vlan = netdev_priv(dev);
684 struct net_device *lowerdev = vlan->lowerdev;
686 if (vlan->accel_priv) {
687 lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev,
689 vlan->accel_priv = NULL;
692 dev_uc_unsync(lowerdev, dev);
693 dev_mc_unsync(lowerdev, dev);
695 if (macvlan_passthru(vlan->port)) {
696 if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC))
697 dev_set_promiscuity(lowerdev, -1);
701 if (dev->flags & IFF_ALLMULTI)
702 dev_set_allmulti(lowerdev, -1);
704 if (dev->flags & IFF_PROMISC)
705 dev_set_promiscuity(lowerdev, -1);
707 dev_uc_del(lowerdev, dev->dev_addr);
710 macvlan_hash_del(vlan, !dev->dismantle);
714 static int macvlan_sync_address(struct net_device *dev,
715 const unsigned char *addr)
717 struct macvlan_dev *vlan = netdev_priv(dev);
718 struct net_device *lowerdev = vlan->lowerdev;
719 struct macvlan_port *port = vlan->port;
722 if (!(dev->flags & IFF_UP)) {
723 /* Just copy in the new address */
724 eth_hw_addr_set(dev, addr);
726 /* Rehash and update the device filters */
727 if (macvlan_addr_busy(vlan->port, addr))
730 if (!macvlan_passthru(port)) {
731 err = dev_uc_add(lowerdev, addr);
735 dev_uc_del(lowerdev, dev->dev_addr);
738 macvlan_hash_change_addr(vlan, addr);
740 if (macvlan_passthru(port) && !macvlan_addr_change(port)) {
741 /* Since addr_change isn't set, we are here due to lower
742 * device change. Save the lower-dev address so we can
745 ether_addr_copy(vlan->port->perm_addr,
748 macvlan_clear_addr_change(port);
752 static int macvlan_set_mac_address(struct net_device *dev, void *p)
754 struct macvlan_dev *vlan = netdev_priv(dev);
755 struct sockaddr *addr = p;
757 if (!is_valid_ether_addr(addr->sa_data))
758 return -EADDRNOTAVAIL;
760 /* If the addresses are the same, this is a no-op */
761 if (ether_addr_equal(dev->dev_addr, addr->sa_data))
764 if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
765 macvlan_set_addr_change(vlan->port);
766 return dev_set_mac_address(vlan->lowerdev, addr, NULL);
769 if (macvlan_addr_busy(vlan->port, addr->sa_data))
772 return macvlan_sync_address(dev, addr->sa_data);
775 static void macvlan_change_rx_flags(struct net_device *dev, int change)
777 struct macvlan_dev *vlan = netdev_priv(dev);
778 struct net_device *lowerdev = vlan->lowerdev;
780 if (dev->flags & IFF_UP) {
781 if (change & IFF_ALLMULTI)
782 dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
783 if (change & IFF_PROMISC)
784 dev_set_promiscuity(lowerdev,
785 dev->flags & IFF_PROMISC ? 1 : -1);
790 static void macvlan_compute_filter(unsigned long *mc_filter,
791 struct net_device *dev,
792 struct macvlan_dev *vlan, int cutoff)
794 if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
795 bitmap_fill(mc_filter, MACVLAN_MC_FILTER_SZ);
797 DECLARE_BITMAP(filter, MACVLAN_MC_FILTER_SZ);
798 struct netdev_hw_addr *ha;
800 bitmap_zero(filter, MACVLAN_MC_FILTER_SZ);
801 netdev_for_each_mc_addr(ha, dev) {
802 if (!vlan && ha->synced <= cutoff)
805 __set_bit(mc_hash(vlan, ha->addr), filter);
808 __set_bit(mc_hash(vlan, dev->broadcast), filter);
810 bitmap_copy(mc_filter, filter, MACVLAN_MC_FILTER_SZ);
814 static void macvlan_recompute_bc_filter(struct macvlan_dev *vlan)
816 if (vlan->port->bc_cutoff < 0) {
817 bitmap_zero(vlan->port->bc_filter, MACVLAN_MC_FILTER_SZ);
821 macvlan_compute_filter(vlan->port->bc_filter, vlan->lowerdev, NULL,
822 vlan->port->bc_cutoff);
825 static void macvlan_set_mac_lists(struct net_device *dev)
827 struct macvlan_dev *vlan = netdev_priv(dev);
829 macvlan_compute_filter(vlan->mc_filter, dev, vlan, 0);
831 dev_uc_sync(vlan->lowerdev, dev);
832 dev_mc_sync(vlan->lowerdev, dev);
834 /* This is slightly inaccurate as we're including the subscription
835 * list of vlan->lowerdev too.
837 * Bug alert: This only works if everyone has the same broadcast
838 * address as lowerdev. As soon as someone changes theirs this
841 * However, this is already broken as when you change your broadcast
842 * address we don't get called.
844 * The solution is to maintain a list of broadcast addresses like
845 * we do for uc/mc, if you care.
847 macvlan_compute_filter(vlan->port->mc_filter, vlan->lowerdev, NULL,
849 macvlan_recompute_bc_filter(vlan);
852 static void update_port_bc_cutoff(struct macvlan_dev *vlan, int cutoff)
854 if (vlan->port->bc_cutoff == cutoff)
857 vlan->port->bc_cutoff = cutoff;
858 macvlan_recompute_bc_filter(vlan);
861 static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
863 struct macvlan_dev *vlan = netdev_priv(dev);
865 if (vlan->lowerdev->mtu < new_mtu)
871 static int macvlan_hwtstamp_get(struct net_device *dev,
872 struct kernel_hwtstamp_config *cfg)
874 struct net_device *real_dev = macvlan_dev_real_dev(dev);
876 return generic_hwtstamp_get_lower(real_dev, cfg);
879 static int macvlan_hwtstamp_set(struct net_device *dev,
880 struct kernel_hwtstamp_config *cfg,
881 struct netlink_ext_ack *extack)
883 struct net_device *real_dev = macvlan_dev_real_dev(dev);
885 if (!net_eq(dev_net(dev), &init_net))
888 return generic_hwtstamp_set_lower(real_dev, cfg, extack);
892 * macvlan network devices have devices nesting below it and are a special
893 * "super class" of normal network devices; split their locks off into a
894 * separate class since they always nest.
896 static struct lock_class_key macvlan_netdev_addr_lock_key;
898 #define ALWAYS_ON_OFFLOADS \
899 (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE | \
900 NETIF_F_GSO_ROBUST | NETIF_F_GSO_ENCAP_ALL)
902 #define ALWAYS_ON_FEATURES (ALWAYS_ON_OFFLOADS | NETIF_F_LLTX)
904 #define MACVLAN_FEATURES \
905 (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
906 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_LRO | \
907 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
908 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
910 #define MACVLAN_STATE_MASK \
911 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
913 static void macvlan_set_lockdep_class(struct net_device *dev)
915 netdev_lockdep_set_classes(dev);
916 lockdep_set_class(&dev->addr_list_lock,
917 &macvlan_netdev_addr_lock_key);
920 static int macvlan_init(struct net_device *dev)
922 struct macvlan_dev *vlan = netdev_priv(dev);
923 struct net_device *lowerdev = vlan->lowerdev;
924 struct macvlan_port *port = vlan->port;
926 dev->state = (dev->state & ~MACVLAN_STATE_MASK) |
927 (lowerdev->state & MACVLAN_STATE_MASK);
928 dev->features = lowerdev->features & MACVLAN_FEATURES;
929 dev->features |= ALWAYS_ON_FEATURES;
930 dev->hw_features |= NETIF_F_LRO;
931 dev->vlan_features = lowerdev->vlan_features & MACVLAN_FEATURES;
932 dev->vlan_features |= ALWAYS_ON_OFFLOADS;
933 dev->hw_enc_features |= dev->features;
934 netif_inherit_tso_max(dev, lowerdev);
935 dev->hard_header_len = lowerdev->hard_header_len;
936 macvlan_set_lockdep_class(dev);
938 vlan->pcpu_stats = netdev_alloc_pcpu_stats(struct vlan_pcpu_stats);
939 if (!vlan->pcpu_stats)
944 /* Get macvlan's reference to lowerdev */
945 netdev_hold(lowerdev, &vlan->dev_tracker, GFP_KERNEL);
950 static void macvlan_uninit(struct net_device *dev)
952 struct macvlan_dev *vlan = netdev_priv(dev);
953 struct macvlan_port *port = vlan->port;
955 free_percpu(vlan->pcpu_stats);
957 macvlan_flush_sources(port, vlan);
960 macvlan_port_destroy(port->dev);
963 static void macvlan_dev_get_stats64(struct net_device *dev,
964 struct rtnl_link_stats64 *stats)
966 struct macvlan_dev *vlan = netdev_priv(dev);
968 if (vlan->pcpu_stats) {
969 struct vlan_pcpu_stats *p;
970 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
971 u32 rx_errors = 0, tx_dropped = 0;
975 for_each_possible_cpu(i) {
976 p = per_cpu_ptr(vlan->pcpu_stats, i);
978 start = u64_stats_fetch_begin(&p->syncp);
979 rx_packets = u64_stats_read(&p->rx_packets);
980 rx_bytes = u64_stats_read(&p->rx_bytes);
981 rx_multicast = u64_stats_read(&p->rx_multicast);
982 tx_packets = u64_stats_read(&p->tx_packets);
983 tx_bytes = u64_stats_read(&p->tx_bytes);
984 } while (u64_stats_fetch_retry(&p->syncp, start));
986 stats->rx_packets += rx_packets;
987 stats->rx_bytes += rx_bytes;
988 stats->multicast += rx_multicast;
989 stats->tx_packets += tx_packets;
990 stats->tx_bytes += tx_bytes;
991 /* rx_errors & tx_dropped are u32, updated
992 * without syncp protection.
994 rx_errors += READ_ONCE(p->rx_errors);
995 tx_dropped += READ_ONCE(p->tx_dropped);
997 stats->rx_errors = rx_errors;
998 stats->rx_dropped = rx_errors;
999 stats->tx_dropped = tx_dropped;
1003 static int macvlan_vlan_rx_add_vid(struct net_device *dev,
1004 __be16 proto, u16 vid)
1006 struct macvlan_dev *vlan = netdev_priv(dev);
1007 struct net_device *lowerdev = vlan->lowerdev;
1009 return vlan_vid_add(lowerdev, proto, vid);
1012 static int macvlan_vlan_rx_kill_vid(struct net_device *dev,
1013 __be16 proto, u16 vid)
1015 struct macvlan_dev *vlan = netdev_priv(dev);
1016 struct net_device *lowerdev = vlan->lowerdev;
1018 vlan_vid_del(lowerdev, proto, vid);
1022 static int macvlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1023 struct net_device *dev,
1024 const unsigned char *addr, u16 vid,
1026 struct netlink_ext_ack *extack)
1028 struct macvlan_dev *vlan = netdev_priv(dev);
1031 /* Support unicast filter only on passthru devices.
1032 * Multicast filter should be allowed on all devices.
1034 if (!macvlan_passthru(vlan->port) && is_unicast_ether_addr(addr))
1037 if (flags & NLM_F_REPLACE)
1040 if (is_unicast_ether_addr(addr))
1041 err = dev_uc_add_excl(dev, addr);
1042 else if (is_multicast_ether_addr(addr))
1043 err = dev_mc_add_excl(dev, addr);
1048 static int macvlan_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
1049 struct net_device *dev,
1050 const unsigned char *addr, u16 vid,
1051 struct netlink_ext_ack *extack)
1053 struct macvlan_dev *vlan = netdev_priv(dev);
1056 /* Support unicast filter only on passthru devices.
1057 * Multicast filter should be allowed on all devices.
1059 if (!macvlan_passthru(vlan->port) && is_unicast_ether_addr(addr))
1062 if (is_unicast_ether_addr(addr))
1063 err = dev_uc_del(dev, addr);
1064 else if (is_multicast_ether_addr(addr))
1065 err = dev_mc_del(dev, addr);
1070 static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
1071 struct ethtool_drvinfo *drvinfo)
1073 strscpy(drvinfo->driver, "macvlan", sizeof(drvinfo->driver));
1074 strscpy(drvinfo->version, "0.1", sizeof(drvinfo->version));
1077 static int macvlan_ethtool_get_link_ksettings(struct net_device *dev,
1078 struct ethtool_link_ksettings *cmd)
1080 const struct macvlan_dev *vlan = netdev_priv(dev);
1082 return __ethtool_get_link_ksettings(vlan->lowerdev, cmd);
1085 static int macvlan_ethtool_get_ts_info(struct net_device *dev,
1086 struct ethtool_ts_info *info)
1088 struct net_device *real_dev = macvlan_dev_real_dev(dev);
1089 const struct ethtool_ops *ops = real_dev->ethtool_ops;
1090 struct phy_device *phydev = real_dev->phydev;
1092 if (phy_has_tsinfo(phydev)) {
1093 return phy_ts_info(phydev, info);
1094 } else if (ops->get_ts_info) {
1095 return ops->get_ts_info(real_dev, info);
1097 info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE |
1098 SOF_TIMESTAMPING_SOFTWARE;
1099 info->phc_index = -1;
1105 static netdev_features_t macvlan_fix_features(struct net_device *dev,
1106 netdev_features_t features)
1108 struct macvlan_dev *vlan = netdev_priv(dev);
1109 netdev_features_t lowerdev_features = vlan->lowerdev->features;
1110 netdev_features_t mask;
1112 features |= NETIF_F_ALL_FOR_ALL;
1113 features &= (vlan->set_features | ~MACVLAN_FEATURES);
1116 lowerdev_features &= (features | ~NETIF_F_LRO);
1117 features = netdev_increment_features(lowerdev_features, features, mask);
1118 features |= ALWAYS_ON_FEATURES;
1119 features &= (ALWAYS_ON_FEATURES | MACVLAN_FEATURES);
1124 #ifdef CONFIG_NET_POLL_CONTROLLER
1125 static void macvlan_dev_poll_controller(struct net_device *dev)
1130 static int macvlan_dev_netpoll_setup(struct net_device *dev, struct netpoll_info *npinfo)
1132 struct macvlan_dev *vlan = netdev_priv(dev);
1133 struct net_device *real_dev = vlan->lowerdev;
1134 struct netpoll *netpoll;
1137 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
1142 err = __netpoll_setup(netpoll, real_dev);
1148 vlan->netpoll = netpoll;
1154 static void macvlan_dev_netpoll_cleanup(struct net_device *dev)
1156 struct macvlan_dev *vlan = netdev_priv(dev);
1157 struct netpoll *netpoll = vlan->netpoll;
1162 vlan->netpoll = NULL;
1164 __netpoll_free(netpoll);
1166 #endif /* CONFIG_NET_POLL_CONTROLLER */
1168 static int macvlan_dev_get_iflink(const struct net_device *dev)
1170 struct macvlan_dev *vlan = netdev_priv(dev);
1172 return vlan->lowerdev->ifindex;
1175 static const struct ethtool_ops macvlan_ethtool_ops = {
1176 .get_link = ethtool_op_get_link,
1177 .get_link_ksettings = macvlan_ethtool_get_link_ksettings,
1178 .get_drvinfo = macvlan_ethtool_get_drvinfo,
1179 .get_ts_info = macvlan_ethtool_get_ts_info,
1182 static const struct net_device_ops macvlan_netdev_ops = {
1183 .ndo_init = macvlan_init,
1184 .ndo_uninit = macvlan_uninit,
1185 .ndo_open = macvlan_open,
1186 .ndo_stop = macvlan_stop,
1187 .ndo_start_xmit = macvlan_start_xmit,
1188 .ndo_change_mtu = macvlan_change_mtu,
1189 .ndo_fix_features = macvlan_fix_features,
1190 .ndo_change_rx_flags = macvlan_change_rx_flags,
1191 .ndo_set_mac_address = macvlan_set_mac_address,
1192 .ndo_set_rx_mode = macvlan_set_mac_lists,
1193 .ndo_get_stats64 = macvlan_dev_get_stats64,
1194 .ndo_validate_addr = eth_validate_addr,
1195 .ndo_vlan_rx_add_vid = macvlan_vlan_rx_add_vid,
1196 .ndo_vlan_rx_kill_vid = macvlan_vlan_rx_kill_vid,
1197 .ndo_fdb_add = macvlan_fdb_add,
1198 .ndo_fdb_del = macvlan_fdb_del,
1199 .ndo_fdb_dump = ndo_dflt_fdb_dump,
1200 #ifdef CONFIG_NET_POLL_CONTROLLER
1201 .ndo_poll_controller = macvlan_dev_poll_controller,
1202 .ndo_netpoll_setup = macvlan_dev_netpoll_setup,
1203 .ndo_netpoll_cleanup = macvlan_dev_netpoll_cleanup,
1205 .ndo_get_iflink = macvlan_dev_get_iflink,
1206 .ndo_features_check = passthru_features_check,
1207 .ndo_hwtstamp_get = macvlan_hwtstamp_get,
1208 .ndo_hwtstamp_set = macvlan_hwtstamp_set,
1211 static void macvlan_dev_free(struct net_device *dev)
1213 struct macvlan_dev *vlan = netdev_priv(dev);
1215 /* Get rid of the macvlan's reference to lowerdev */
1216 netdev_put(vlan->lowerdev, &vlan->dev_tracker);
1219 void macvlan_common_setup(struct net_device *dev)
1223 /* ether_setup() has set dev->min_mtu to ETH_MIN_MTU. */
1224 dev->max_mtu = ETH_MAX_MTU;
1225 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1226 netif_keep_dst(dev);
1227 dev->priv_flags |= IFF_UNICAST_FLT | IFF_CHANGE_PROTO_DOWN;
1228 dev->netdev_ops = &macvlan_netdev_ops;
1229 dev->needs_free_netdev = true;
1230 dev->priv_destructor = macvlan_dev_free;
1231 dev->header_ops = &macvlan_hard_header_ops;
1232 dev->ethtool_ops = &macvlan_ethtool_ops;
1234 EXPORT_SYMBOL_GPL(macvlan_common_setup);
1236 static void macvlan_setup(struct net_device *dev)
1238 macvlan_common_setup(dev);
1239 dev->priv_flags |= IFF_NO_QUEUE;
1242 static int macvlan_port_create(struct net_device *dev)
1244 struct macvlan_port *port;
1248 if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
1251 if (netdev_is_rx_handler_busy(dev))
1254 port = kzalloc(sizeof(*port), GFP_KERNEL);
1259 ether_addr_copy(port->perm_addr, dev->dev_addr);
1260 INIT_LIST_HEAD(&port->vlans);
1261 for (i = 0; i < MACVLAN_HASH_SIZE; i++)
1262 INIT_HLIST_HEAD(&port->vlan_hash[i]);
1263 for (i = 0; i < MACVLAN_HASH_SIZE; i++)
1264 INIT_HLIST_HEAD(&port->vlan_source_hash[i]);
1266 port->bc_queue_len_used = 0;
1267 port->bc_cutoff = 1;
1268 skb_queue_head_init(&port->bc_queue);
1269 INIT_WORK(&port->bc_work, macvlan_process_broadcast);
1271 err = netdev_rx_handler_register(dev, macvlan_handle_frame, port);
1275 dev->priv_flags |= IFF_MACVLAN_PORT;
1279 static void macvlan_port_destroy(struct net_device *dev)
1281 struct macvlan_port *port = macvlan_port_get_rtnl(dev);
1282 struct sk_buff *skb;
1284 dev->priv_flags &= ~IFF_MACVLAN_PORT;
1285 netdev_rx_handler_unregister(dev);
1287 /* After this point, no packet can schedule bc_work anymore,
1288 * but we need to cancel it and purge left skbs if any.
1290 cancel_work_sync(&port->bc_work);
1292 while ((skb = __skb_dequeue(&port->bc_queue))) {
1293 const struct macvlan_dev *src = MACVLAN_SKB_CB(skb)->src;
1301 /* If the lower device address has been changed by passthru
1302 * macvlan, put it back.
1304 if (macvlan_passthru(port) &&
1305 !ether_addr_equal(port->dev->dev_addr, port->perm_addr)) {
1308 sa.sa_family = port->dev->type;
1309 memcpy(&sa.sa_data, port->perm_addr, port->dev->addr_len);
1310 dev_set_mac_address(port->dev, &sa, NULL);
1316 static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[],
1317 struct netlink_ext_ack *extack)
1319 struct nlattr *nla, *head;
1322 if (tb[IFLA_ADDRESS]) {
1323 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1325 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1326 return -EADDRNOTAVAIL;
1332 if (data[IFLA_MACVLAN_FLAGS] &&
1333 nla_get_u16(data[IFLA_MACVLAN_FLAGS]) & ~(MACVLAN_FLAG_NOPROMISC |
1334 MACVLAN_FLAG_NODST))
1337 if (data[IFLA_MACVLAN_MODE]) {
1338 switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) {
1339 case MACVLAN_MODE_PRIVATE:
1340 case MACVLAN_MODE_VEPA:
1341 case MACVLAN_MODE_BRIDGE:
1342 case MACVLAN_MODE_PASSTHRU:
1343 case MACVLAN_MODE_SOURCE:
1350 if (data[IFLA_MACVLAN_MACADDR_MODE]) {
1351 switch (nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE])) {
1352 case MACVLAN_MACADDR_ADD:
1353 case MACVLAN_MACADDR_DEL:
1354 case MACVLAN_MACADDR_FLUSH:
1355 case MACVLAN_MACADDR_SET:
1362 if (data[IFLA_MACVLAN_MACADDR]) {
1363 if (nla_len(data[IFLA_MACVLAN_MACADDR]) != ETH_ALEN)
1366 if (!is_valid_ether_addr(nla_data(data[IFLA_MACVLAN_MACADDR])))
1367 return -EADDRNOTAVAIL;
1370 if (data[IFLA_MACVLAN_MACADDR_DATA]) {
1371 head = nla_data(data[IFLA_MACVLAN_MACADDR_DATA]);
1372 len = nla_len(data[IFLA_MACVLAN_MACADDR_DATA]);
1374 nla_for_each_attr(nla, head, len, rem) {
1375 if (nla_type(nla) != IFLA_MACVLAN_MACADDR ||
1376 nla_len(nla) != ETH_ALEN)
1379 if (!is_valid_ether_addr(nla_data(nla)))
1380 return -EADDRNOTAVAIL;
1384 if (data[IFLA_MACVLAN_MACADDR_COUNT])
1391 * reconfigure list of remote source mac address
1392 * (only for macvlan devices in source mode)
1393 * Note regarding alignment: all netlink data is aligned to 4 Byte, which
1394 * suffices for both ether_addr_copy and ether_addr_equal_64bits usage.
1396 static int macvlan_changelink_sources(struct macvlan_dev *vlan, u32 mode,
1397 struct nlattr *data[])
1401 struct nlattr *nla, *head;
1402 struct macvlan_source_entry *entry;
1404 if (data[IFLA_MACVLAN_MACADDR])
1405 addr = nla_data(data[IFLA_MACVLAN_MACADDR]);
1407 if (mode == MACVLAN_MACADDR_ADD) {
1411 return macvlan_hash_add_source(vlan, addr);
1413 } else if (mode == MACVLAN_MACADDR_DEL) {
1417 entry = macvlan_hash_lookup_source(vlan, addr);
1419 macvlan_hash_del_source(entry);
1420 vlan->macaddr_count--;
1422 } else if (mode == MACVLAN_MACADDR_FLUSH) {
1423 macvlan_flush_sources(vlan->port, vlan);
1424 } else if (mode == MACVLAN_MACADDR_SET) {
1425 macvlan_flush_sources(vlan->port, vlan);
1428 ret = macvlan_hash_add_source(vlan, addr);
1433 if (!data[IFLA_MACVLAN_MACADDR_DATA])
1436 head = nla_data(data[IFLA_MACVLAN_MACADDR_DATA]);
1437 len = nla_len(data[IFLA_MACVLAN_MACADDR_DATA]);
1439 nla_for_each_attr(nla, head, len, rem) {
1440 addr = nla_data(nla);
1441 ret = macvlan_hash_add_source(vlan, addr);
1452 int macvlan_common_newlink(struct net *src_net, struct net_device *dev,
1453 struct nlattr *tb[], struct nlattr *data[],
1454 struct netlink_ext_ack *extack)
1456 struct macvlan_dev *vlan = netdev_priv(dev);
1457 struct macvlan_port *port;
1458 struct net_device *lowerdev;
1461 bool create = false;
1466 lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
1467 if (lowerdev == NULL)
1470 /* When creating macvlans or macvtaps on top of other macvlans - use
1471 * the real device as the lowerdev.
1473 if (netif_is_macvlan(lowerdev))
1474 lowerdev = macvlan_dev_real_dev(lowerdev);
1477 dev->mtu = lowerdev->mtu;
1478 else if (dev->mtu > lowerdev->mtu)
1481 /* MTU range: 68 - lowerdev->max_mtu */
1482 dev->min_mtu = ETH_MIN_MTU;
1483 dev->max_mtu = lowerdev->max_mtu;
1485 if (!tb[IFLA_ADDRESS])
1486 eth_hw_addr_random(dev);
1488 if (!netif_is_macvlan_port(lowerdev)) {
1489 err = macvlan_port_create(lowerdev);
1494 port = macvlan_port_get_rtnl(lowerdev);
1496 /* Only 1 macvlan device can be created in passthru mode */
1497 if (macvlan_passthru(port)) {
1498 /* The macvlan port must be not created this time,
1499 * still goto destroy_macvlan_port for readability.
1502 goto destroy_macvlan_port;
1505 vlan->lowerdev = lowerdev;
1508 vlan->set_features = MACVLAN_FEATURES;
1510 vlan->mode = MACVLAN_MODE_VEPA;
1511 if (data && data[IFLA_MACVLAN_MODE])
1512 vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
1514 if (data && data[IFLA_MACVLAN_FLAGS])
1515 vlan->flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
1517 if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
1520 goto destroy_macvlan_port;
1522 macvlan_set_passthru(port);
1523 eth_hw_addr_inherit(dev, lowerdev);
1526 if (data && data[IFLA_MACVLAN_MACADDR_MODE]) {
1527 if (vlan->mode != MACVLAN_MODE_SOURCE) {
1529 goto destroy_macvlan_port;
1531 macmode = nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE]);
1532 err = macvlan_changelink_sources(vlan, macmode, data);
1534 goto destroy_macvlan_port;
1537 vlan->bc_queue_len_req = MACVLAN_DEFAULT_BC_QUEUE_LEN;
1538 if (data && data[IFLA_MACVLAN_BC_QUEUE_LEN])
1539 vlan->bc_queue_len_req = nla_get_u32(data[IFLA_MACVLAN_BC_QUEUE_LEN]);
1541 if (data && data[IFLA_MACVLAN_BC_CUTOFF])
1542 update_port_bc_cutoff(
1543 vlan, nla_get_s32(data[IFLA_MACVLAN_BC_CUTOFF]));
1545 err = register_netdevice(dev);
1547 goto destroy_macvlan_port;
1549 dev->priv_flags |= IFF_MACVLAN;
1550 err = netdev_upper_dev_link(lowerdev, dev, extack);
1552 goto unregister_netdev;
1554 list_add_tail_rcu(&vlan->list, &port->vlans);
1555 update_port_bc_queue_len(vlan->port);
1556 netif_stacked_transfer_operstate(lowerdev, dev);
1557 linkwatch_fire_event(dev);
1562 /* macvlan_uninit would free the macvlan port */
1563 unregister_netdevice(dev);
1565 destroy_macvlan_port:
1566 /* the macvlan port may be freed by macvlan_uninit when fail to register.
1567 * so we destroy the macvlan port only when it's valid.
1569 if (create && macvlan_port_get_rtnl(lowerdev)) {
1570 macvlan_flush_sources(port, vlan);
1571 macvlan_port_destroy(port->dev);
1575 EXPORT_SYMBOL_GPL(macvlan_common_newlink);
1577 static int macvlan_newlink(struct net *src_net, struct net_device *dev,
1578 struct nlattr *tb[], struct nlattr *data[],
1579 struct netlink_ext_ack *extack)
1581 return macvlan_common_newlink(src_net, dev, tb, data, extack);
1584 void macvlan_dellink(struct net_device *dev, struct list_head *head)
1586 struct macvlan_dev *vlan = netdev_priv(dev);
1588 if (vlan->mode == MACVLAN_MODE_SOURCE)
1589 macvlan_flush_sources(vlan->port, vlan);
1590 list_del_rcu(&vlan->list);
1591 update_port_bc_queue_len(vlan->port);
1592 unregister_netdevice_queue(dev, head);
1593 netdev_upper_dev_unlink(vlan->lowerdev, dev);
1595 EXPORT_SYMBOL_GPL(macvlan_dellink);
1597 static int macvlan_changelink(struct net_device *dev,
1598 struct nlattr *tb[], struct nlattr *data[],
1599 struct netlink_ext_ack *extack)
1601 struct macvlan_dev *vlan = netdev_priv(dev);
1602 enum macvlan_mode mode;
1603 bool set_mode = false;
1604 enum macvlan_macaddr_mode macmode;
1607 /* Validate mode, but don't set yet: setting flags may fail. */
1608 if (data && data[IFLA_MACVLAN_MODE]) {
1610 mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
1611 /* Passthrough mode can't be set or cleared dynamically */
1612 if ((mode == MACVLAN_MODE_PASSTHRU) !=
1613 (vlan->mode == MACVLAN_MODE_PASSTHRU))
1615 if (vlan->mode == MACVLAN_MODE_SOURCE &&
1617 macvlan_flush_sources(vlan->port, vlan);
1620 if (data && data[IFLA_MACVLAN_FLAGS]) {
1621 __u16 flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
1622 bool promisc = (flags ^ vlan->flags) & MACVLAN_FLAG_NOPROMISC;
1623 if (macvlan_passthru(vlan->port) && promisc) {
1626 if (flags & MACVLAN_FLAG_NOPROMISC)
1627 err = dev_set_promiscuity(vlan->lowerdev, -1);
1629 err = dev_set_promiscuity(vlan->lowerdev, 1);
1633 vlan->flags = flags;
1636 if (data && data[IFLA_MACVLAN_BC_QUEUE_LEN]) {
1637 vlan->bc_queue_len_req = nla_get_u32(data[IFLA_MACVLAN_BC_QUEUE_LEN]);
1638 update_port_bc_queue_len(vlan->port);
1641 if (data && data[IFLA_MACVLAN_BC_CUTOFF])
1642 update_port_bc_cutoff(
1643 vlan, nla_get_s32(data[IFLA_MACVLAN_BC_CUTOFF]));
1647 if (data && data[IFLA_MACVLAN_MACADDR_MODE]) {
1648 if (vlan->mode != MACVLAN_MODE_SOURCE)
1650 macmode = nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE]);
1651 ret = macvlan_changelink_sources(vlan, macmode, data);
1658 static size_t macvlan_get_size_mac(const struct macvlan_dev *vlan)
1660 if (vlan->macaddr_count == 0)
1662 return nla_total_size(0) /* IFLA_MACVLAN_MACADDR_DATA */
1663 + vlan->macaddr_count * nla_total_size(sizeof(u8) * ETH_ALEN);
1666 static size_t macvlan_get_size(const struct net_device *dev)
1668 struct macvlan_dev *vlan = netdev_priv(dev);
1671 + nla_total_size(4) /* IFLA_MACVLAN_MODE */
1672 + nla_total_size(2) /* IFLA_MACVLAN_FLAGS */
1673 + nla_total_size(4) /* IFLA_MACVLAN_MACADDR_COUNT */
1674 + macvlan_get_size_mac(vlan) /* IFLA_MACVLAN_MACADDR */
1675 + nla_total_size(4) /* IFLA_MACVLAN_BC_QUEUE_LEN */
1676 + nla_total_size(4) /* IFLA_MACVLAN_BC_QUEUE_LEN_USED */
1680 static int macvlan_fill_info_macaddr(struct sk_buff *skb,
1681 const struct macvlan_dev *vlan,
1684 struct hlist_head *h = &vlan->port->vlan_source_hash[i];
1685 struct macvlan_source_entry *entry;
1687 hlist_for_each_entry_rcu(entry, h, hlist, lockdep_rtnl_is_held()) {
1688 if (entry->vlan != vlan)
1690 if (nla_put(skb, IFLA_MACVLAN_MACADDR, ETH_ALEN, entry->addr))
1696 static int macvlan_fill_info(struct sk_buff *skb,
1697 const struct net_device *dev)
1699 struct macvlan_dev *vlan = netdev_priv(dev);
1700 struct macvlan_port *port = vlan->port;
1702 struct nlattr *nest;
1704 if (nla_put_u32(skb, IFLA_MACVLAN_MODE, vlan->mode))
1705 goto nla_put_failure;
1706 if (nla_put_u16(skb, IFLA_MACVLAN_FLAGS, vlan->flags))
1707 goto nla_put_failure;
1708 if (nla_put_u32(skb, IFLA_MACVLAN_MACADDR_COUNT, vlan->macaddr_count))
1709 goto nla_put_failure;
1710 if (vlan->macaddr_count > 0) {
1711 nest = nla_nest_start_noflag(skb, IFLA_MACVLAN_MACADDR_DATA);
1713 goto nla_put_failure;
1715 for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
1716 if (macvlan_fill_info_macaddr(skb, vlan, i))
1717 goto nla_put_failure;
1719 nla_nest_end(skb, nest);
1721 if (nla_put_u32(skb, IFLA_MACVLAN_BC_QUEUE_LEN, vlan->bc_queue_len_req))
1722 goto nla_put_failure;
1723 if (nla_put_u32(skb, IFLA_MACVLAN_BC_QUEUE_LEN_USED, port->bc_queue_len_used))
1724 goto nla_put_failure;
1725 if (port->bc_cutoff != 1 &&
1726 nla_put_s32(skb, IFLA_MACVLAN_BC_CUTOFF, port->bc_cutoff))
1727 goto nla_put_failure;
1734 static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = {
1735 [IFLA_MACVLAN_MODE] = { .type = NLA_U32 },
1736 [IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 },
1737 [IFLA_MACVLAN_MACADDR_MODE] = { .type = NLA_U32 },
1738 [IFLA_MACVLAN_MACADDR] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1739 [IFLA_MACVLAN_MACADDR_DATA] = { .type = NLA_NESTED },
1740 [IFLA_MACVLAN_MACADDR_COUNT] = { .type = NLA_U32 },
1741 [IFLA_MACVLAN_BC_QUEUE_LEN] = { .type = NLA_U32 },
1742 [IFLA_MACVLAN_BC_QUEUE_LEN_USED] = { .type = NLA_REJECT },
1743 [IFLA_MACVLAN_BC_CUTOFF] = { .type = NLA_S32 },
1746 int macvlan_link_register(struct rtnl_link_ops *ops)
1749 ops->validate = macvlan_validate;
1750 ops->maxtype = IFLA_MACVLAN_MAX;
1751 ops->policy = macvlan_policy;
1752 ops->changelink = macvlan_changelink;
1753 ops->get_size = macvlan_get_size;
1754 ops->fill_info = macvlan_fill_info;
1756 return rtnl_link_register(ops);
1758 EXPORT_SYMBOL_GPL(macvlan_link_register);
1760 static struct net *macvlan_get_link_net(const struct net_device *dev)
1762 return dev_net(macvlan_dev_real_dev(dev));
1765 static struct rtnl_link_ops macvlan_link_ops = {
1767 .setup = macvlan_setup,
1768 .newlink = macvlan_newlink,
1769 .dellink = macvlan_dellink,
1770 .get_link_net = macvlan_get_link_net,
1771 .priv_size = sizeof(struct macvlan_dev),
1774 static void update_port_bc_queue_len(struct macvlan_port *port)
1776 u32 max_bc_queue_len_req = 0;
1777 struct macvlan_dev *vlan;
1779 list_for_each_entry(vlan, &port->vlans, list) {
1780 if (vlan->bc_queue_len_req > max_bc_queue_len_req)
1781 max_bc_queue_len_req = vlan->bc_queue_len_req;
1783 port->bc_queue_len_used = max_bc_queue_len_req;
1786 static int macvlan_device_event(struct notifier_block *unused,
1787 unsigned long event, void *ptr)
1789 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1790 struct macvlan_dev *vlan, *next;
1791 struct macvlan_port *port;
1792 LIST_HEAD(list_kill);
1794 if (!netif_is_macvlan_port(dev))
1797 port = macvlan_port_get_rtnl(dev);
1803 list_for_each_entry(vlan, &port->vlans, list)
1804 netif_stacked_transfer_operstate(vlan->lowerdev,
1807 case NETDEV_FEAT_CHANGE:
1808 list_for_each_entry(vlan, &port->vlans, list) {
1809 netif_inherit_tso_max(vlan->dev, dev);
1810 netdev_update_features(vlan->dev);
1813 case NETDEV_CHANGEMTU:
1814 list_for_each_entry(vlan, &port->vlans, list) {
1815 if (vlan->dev->mtu <= dev->mtu)
1817 dev_set_mtu(vlan->dev, dev->mtu);
1820 case NETDEV_CHANGEADDR:
1821 if (!macvlan_passthru(port))
1824 vlan = list_first_entry_or_null(&port->vlans,
1828 if (vlan && macvlan_sync_address(vlan->dev, dev->dev_addr))
1832 case NETDEV_UNREGISTER:
1833 /* twiddle thumbs on netns device moves */
1834 if (dev->reg_state != NETREG_UNREGISTERING)
1837 list_for_each_entry_safe(vlan, next, &port->vlans, list)
1838 vlan->dev->rtnl_link_ops->dellink(vlan->dev, &list_kill);
1839 unregister_netdevice_many(&list_kill);
1841 case NETDEV_PRE_TYPE_CHANGE:
1842 /* Forbid underlying device to change its type. */
1845 case NETDEV_NOTIFY_PEERS:
1846 case NETDEV_BONDING_FAILOVER:
1847 case NETDEV_RESEND_IGMP:
1848 /* Propagate to all vlans */
1849 list_for_each_entry(vlan, &port->vlans, list)
1850 call_netdevice_notifiers(event, vlan->dev);
1855 static struct notifier_block macvlan_notifier_block __read_mostly = {
1856 .notifier_call = macvlan_device_event,
1859 static int __init macvlan_init_module(void)
1863 register_netdevice_notifier(&macvlan_notifier_block);
1865 err = macvlan_link_register(&macvlan_link_ops);
1870 unregister_netdevice_notifier(&macvlan_notifier_block);
1874 static void __exit macvlan_cleanup_module(void)
1876 rtnl_link_unregister(&macvlan_link_ops);
1877 unregister_netdevice_notifier(&macvlan_notifier_block);
1880 module_init(macvlan_init_module);
1881 module_exit(macvlan_cleanup_module);
1883 MODULE_LICENSE("GPL");
1884 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
1885 MODULE_DESCRIPTION("Driver for MAC address based VLANs");
1886 MODULE_ALIAS_RTNL_LINK("macvlan");