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:
11 * (C) Copyright 2001-2006
12 * Alex Zeffertt, Cambridge Broadband Ltd, ajz@cambridgebroadband.com
13 * Re-worked by Ben Greear <greearb@candelatech.com>
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 <net/rtnetlink.h>
36 #define MACVLAN_HASH_SIZE (1 << BITS_PER_BYTE)
39 struct net_device *dev;
40 struct hlist_head vlan_hash[MACVLAN_HASH_SIZE];
41 struct list_head vlans;
47 static void macvlan_port_destroy(struct net_device *dev);
49 #define macvlan_port_get_rcu(dev) \
50 ((struct macvlan_port *) rcu_dereference(dev->rx_handler_data))
51 #define macvlan_port_get(dev) ((struct macvlan_port *) dev->rx_handler_data)
52 #define macvlan_port_exists(dev) (dev->priv_flags & IFF_MACVLAN_PORT)
54 static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
55 const unsigned char *addr)
57 struct macvlan_dev *vlan;
60 hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[addr[5]], hlist) {
61 if (ether_addr_equal_64bits(vlan->dev->dev_addr, addr))
67 static void macvlan_hash_add(struct macvlan_dev *vlan)
69 struct macvlan_port *port = vlan->port;
70 const unsigned char *addr = vlan->dev->dev_addr;
72 hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]);
75 static void macvlan_hash_del(struct macvlan_dev *vlan, bool sync)
77 hlist_del_rcu(&vlan->hlist);
82 static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
83 const unsigned char *addr)
85 macvlan_hash_del(vlan, true);
86 /* Now that we are unhashed it is safe to change the device
87 * address without confusing packet delivery.
89 memcpy(vlan->dev->dev_addr, addr, ETH_ALEN);
90 macvlan_hash_add(vlan);
93 static int macvlan_addr_busy(const struct macvlan_port *port,
94 const unsigned char *addr)
96 /* Test to see if the specified multicast address is
97 * currently in use by the underlying device or
100 if (ether_addr_equal_64bits(port->dev->dev_addr, addr))
103 if (macvlan_hash_lookup(port, addr))
110 static int macvlan_broadcast_one(struct sk_buff *skb,
111 const struct macvlan_dev *vlan,
112 const struct ethhdr *eth, bool local)
114 struct net_device *dev = vlan->dev;
119 return vlan->forward(dev, skb);
122 if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast))
123 skb->pkt_type = PACKET_BROADCAST;
125 skb->pkt_type = PACKET_MULTICAST;
127 return vlan->receive(skb);
130 static u32 macvlan_hash_mix(const struct macvlan_dev *vlan)
132 return (u32)(((unsigned long)vlan) >> L1_CACHE_SHIFT);
136 static unsigned int mc_hash(const struct macvlan_dev *vlan,
137 const unsigned char *addr)
139 u32 val = __get_unaligned_cpu32(addr + 2);
141 val ^= macvlan_hash_mix(vlan);
142 return hash_32(val, MACVLAN_MC_FILTER_BITS);
145 static void macvlan_broadcast(struct sk_buff *skb,
146 const struct macvlan_port *port,
147 struct net_device *src,
148 enum macvlan_mode mode)
150 const struct ethhdr *eth = eth_hdr(skb);
151 const struct macvlan_dev *vlan;
152 struct hlist_node *n;
153 struct sk_buff *nskb;
158 if (skb->protocol == htons(ETH_P_PAUSE))
161 for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
162 hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[i], hlist) {
163 if (vlan->dev == src || !(vlan->mode & mode))
166 hash = mc_hash(vlan, eth->h_dest);
167 if (!test_bit(hash, vlan->mc_filter))
169 nskb = skb_clone(skb, GFP_ATOMIC);
170 err = macvlan_broadcast_one(nskb, vlan, eth,
171 mode == MACVLAN_MODE_BRIDGE);
172 macvlan_count_rx(vlan, skb->len + ETH_HLEN,
173 err == NET_RX_SUCCESS, 1);
178 /* called under rcu_read_lock() from netif_receive_skb */
179 static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb)
181 struct macvlan_port *port;
182 struct sk_buff *skb = *pskb;
183 const struct ethhdr *eth = eth_hdr(skb);
184 const struct macvlan_dev *vlan;
185 const struct macvlan_dev *src;
186 struct net_device *dev;
187 unsigned int len = 0;
188 int ret = NET_RX_DROP;
190 port = macvlan_port_get_rcu(skb->dev);
191 if (is_multicast_ether_addr(eth->h_dest)) {
192 skb = ip_check_defrag(skb, IP_DEFRAG_MACVLAN);
194 return RX_HANDLER_CONSUMED;
196 src = macvlan_hash_lookup(port, eth->h_source);
198 /* frame comes from an external address */
199 macvlan_broadcast(skb, port, NULL,
200 MACVLAN_MODE_PRIVATE |
202 MACVLAN_MODE_PASSTHRU|
203 MACVLAN_MODE_BRIDGE);
204 else if (src->mode == MACVLAN_MODE_VEPA)
205 /* flood to everyone except source */
206 macvlan_broadcast(skb, port, src->dev,
208 MACVLAN_MODE_BRIDGE);
209 else if (src->mode == MACVLAN_MODE_BRIDGE)
211 * flood only to VEPA ports, bridge ports
212 * already saw the frame on the way out.
214 macvlan_broadcast(skb, port, src->dev,
217 /* forward to original port. */
219 ret = macvlan_broadcast_one(skb, vlan, eth, 0);
223 return RX_HANDLER_PASS;
227 vlan = list_first_entry(&port->vlans, struct macvlan_dev, list);
229 vlan = macvlan_hash_lookup(port, eth->h_dest);
231 return RX_HANDLER_PASS;
234 if (unlikely(!(dev->flags & IFF_UP))) {
236 return RX_HANDLER_CONSUMED;
238 len = skb->len + ETH_HLEN;
239 skb = skb_share_check(skb, GFP_ATOMIC);
244 skb->pkt_type = PACKET_HOST;
246 ret = vlan->receive(skb);
249 macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, 0);
250 return RX_HANDLER_CONSUMED;
253 static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
255 const struct macvlan_dev *vlan = netdev_priv(dev);
256 const struct macvlan_port *port = vlan->port;
257 const struct macvlan_dev *dest;
258 __u8 ip_summed = skb->ip_summed;
260 if (vlan->mode == MACVLAN_MODE_BRIDGE) {
261 const struct ethhdr *eth = (void *)skb->data;
262 skb->ip_summed = CHECKSUM_UNNECESSARY;
264 /* send to other bridge ports directly */
265 if (is_multicast_ether_addr(eth->h_dest)) {
266 macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE);
270 dest = macvlan_hash_lookup(port, eth->h_dest);
271 if (dest && dest->mode == MACVLAN_MODE_BRIDGE) {
272 /* send to lowerdev first for its network taps */
273 dev_forward_skb(vlan->lowerdev, skb);
275 return NET_XMIT_SUCCESS;
280 skb->ip_summed = ip_summed;
281 skb->dev = vlan->lowerdev;
282 return dev_queue_xmit(skb);
285 netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
286 struct net_device *dev)
288 unsigned int len = skb->len;
290 const struct macvlan_dev *vlan = netdev_priv(dev);
292 ret = macvlan_queue_xmit(skb, dev);
293 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
294 struct macvlan_pcpu_stats *pcpu_stats;
296 pcpu_stats = this_cpu_ptr(vlan->pcpu_stats);
297 u64_stats_update_begin(&pcpu_stats->syncp);
298 pcpu_stats->tx_packets++;
299 pcpu_stats->tx_bytes += len;
300 u64_stats_update_end(&pcpu_stats->syncp);
302 this_cpu_inc(vlan->pcpu_stats->tx_dropped);
306 EXPORT_SYMBOL_GPL(macvlan_start_xmit);
308 static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
309 unsigned short type, const void *daddr,
310 const void *saddr, unsigned len)
312 const struct macvlan_dev *vlan = netdev_priv(dev);
313 struct net_device *lowerdev = vlan->lowerdev;
315 return dev_hard_header(skb, lowerdev, type, daddr,
316 saddr ? : dev->dev_addr, len);
319 static const struct header_ops macvlan_hard_header_ops = {
320 .create = macvlan_hard_header,
321 .rebuild = eth_rebuild_header,
322 .parse = eth_header_parse,
323 .cache = eth_header_cache,
324 .cache_update = eth_header_cache_update,
327 static int macvlan_open(struct net_device *dev)
329 struct macvlan_dev *vlan = netdev_priv(dev);
330 struct net_device *lowerdev = vlan->lowerdev;
333 if (vlan->port->passthru) {
334 if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC))
335 dev_set_promiscuity(lowerdev, 1);
340 if (macvlan_addr_busy(vlan->port, dev->dev_addr))
343 err = dev_uc_add(lowerdev, dev->dev_addr);
346 if (dev->flags & IFF_ALLMULTI) {
347 err = dev_set_allmulti(lowerdev, 1);
353 macvlan_hash_add(vlan);
357 dev_uc_del(lowerdev, dev->dev_addr);
362 static int macvlan_stop(struct net_device *dev)
364 struct macvlan_dev *vlan = netdev_priv(dev);
365 struct net_device *lowerdev = vlan->lowerdev;
367 dev_uc_unsync(lowerdev, dev);
368 dev_mc_unsync(lowerdev, dev);
370 if (vlan->port->passthru) {
371 if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC))
372 dev_set_promiscuity(lowerdev, -1);
376 if (dev->flags & IFF_ALLMULTI)
377 dev_set_allmulti(lowerdev, -1);
379 dev_uc_del(lowerdev, dev->dev_addr);
382 macvlan_hash_del(vlan, !dev->dismantle);
386 static int macvlan_set_mac_address(struct net_device *dev, void *p)
388 struct macvlan_dev *vlan = netdev_priv(dev);
389 struct net_device *lowerdev = vlan->lowerdev;
390 struct sockaddr *addr = p;
393 if (!is_valid_ether_addr(addr->sa_data))
394 return -EADDRNOTAVAIL;
396 if (!(dev->flags & IFF_UP)) {
397 /* Just copy in the new address */
398 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
400 /* Rehash and update the device filters */
401 if (macvlan_addr_busy(vlan->port, addr->sa_data))
404 err = dev_uc_add(lowerdev, addr->sa_data);
408 dev_uc_del(lowerdev, dev->dev_addr);
410 macvlan_hash_change_addr(vlan, addr->sa_data);
415 static void macvlan_change_rx_flags(struct net_device *dev, int change)
417 struct macvlan_dev *vlan = netdev_priv(dev);
418 struct net_device *lowerdev = vlan->lowerdev;
420 if (change & IFF_ALLMULTI)
421 dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
424 static void macvlan_set_mac_lists(struct net_device *dev)
426 struct macvlan_dev *vlan = netdev_priv(dev);
428 if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
429 bitmap_fill(vlan->mc_filter, MACVLAN_MC_FILTER_SZ);
431 struct netdev_hw_addr *ha;
432 DECLARE_BITMAP(filter, MACVLAN_MC_FILTER_SZ);
434 bitmap_zero(filter, MACVLAN_MC_FILTER_SZ);
435 netdev_for_each_mc_addr(ha, dev) {
436 __set_bit(mc_hash(vlan, ha->addr), filter);
439 __set_bit(mc_hash(vlan, dev->broadcast), filter);
441 bitmap_copy(vlan->mc_filter, filter, MACVLAN_MC_FILTER_SZ);
443 dev_uc_sync(vlan->lowerdev, dev);
444 dev_mc_sync(vlan->lowerdev, dev);
447 static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
449 struct macvlan_dev *vlan = netdev_priv(dev);
451 if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
458 * macvlan network devices have devices nesting below it and are a special
459 * "super class" of normal network devices; split their locks off into a
460 * separate class since they always nest.
462 static struct lock_class_key macvlan_netdev_xmit_lock_key;
463 static struct lock_class_key macvlan_netdev_addr_lock_key;
465 #define MACVLAN_FEATURES \
466 (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
467 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
468 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
469 NETIF_F_HW_VLAN_FILTER)
471 #define MACVLAN_STATE_MASK \
472 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
474 static void macvlan_set_lockdep_class_one(struct net_device *dev,
475 struct netdev_queue *txq,
478 lockdep_set_class(&txq->_xmit_lock,
479 &macvlan_netdev_xmit_lock_key);
482 static void macvlan_set_lockdep_class(struct net_device *dev)
484 lockdep_set_class(&dev->addr_list_lock,
485 &macvlan_netdev_addr_lock_key);
486 netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL);
489 static int macvlan_init(struct net_device *dev)
491 struct macvlan_dev *vlan = netdev_priv(dev);
492 const struct net_device *lowerdev = vlan->lowerdev;
494 dev->state = (dev->state & ~MACVLAN_STATE_MASK) |
495 (lowerdev->state & MACVLAN_STATE_MASK);
496 dev->features = lowerdev->features & MACVLAN_FEATURES;
497 dev->features |= NETIF_F_LLTX;
498 dev->gso_max_size = lowerdev->gso_max_size;
499 dev->iflink = lowerdev->ifindex;
500 dev->hard_header_len = lowerdev->hard_header_len;
502 macvlan_set_lockdep_class(dev);
504 vlan->pcpu_stats = alloc_percpu(struct macvlan_pcpu_stats);
505 if (!vlan->pcpu_stats)
511 static void macvlan_uninit(struct net_device *dev)
513 struct macvlan_dev *vlan = netdev_priv(dev);
514 struct macvlan_port *port = vlan->port;
516 free_percpu(vlan->pcpu_stats);
520 macvlan_port_destroy(port->dev);
523 static struct rtnl_link_stats64 *macvlan_dev_get_stats64(struct net_device *dev,
524 struct rtnl_link_stats64 *stats)
526 struct macvlan_dev *vlan = netdev_priv(dev);
528 if (vlan->pcpu_stats) {
529 struct macvlan_pcpu_stats *p;
530 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
531 u32 rx_errors = 0, tx_dropped = 0;
535 for_each_possible_cpu(i) {
536 p = per_cpu_ptr(vlan->pcpu_stats, i);
538 start = u64_stats_fetch_begin_bh(&p->syncp);
539 rx_packets = p->rx_packets;
540 rx_bytes = p->rx_bytes;
541 rx_multicast = p->rx_multicast;
542 tx_packets = p->tx_packets;
543 tx_bytes = p->tx_bytes;
544 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
546 stats->rx_packets += rx_packets;
547 stats->rx_bytes += rx_bytes;
548 stats->multicast += rx_multicast;
549 stats->tx_packets += tx_packets;
550 stats->tx_bytes += tx_bytes;
551 /* rx_errors & tx_dropped are u32, updated
552 * without syncp protection.
554 rx_errors += p->rx_errors;
555 tx_dropped += p->tx_dropped;
557 stats->rx_errors = rx_errors;
558 stats->rx_dropped = rx_errors;
559 stats->tx_dropped = tx_dropped;
564 static int macvlan_vlan_rx_add_vid(struct net_device *dev,
567 struct macvlan_dev *vlan = netdev_priv(dev);
568 struct net_device *lowerdev = vlan->lowerdev;
570 return vlan_vid_add(lowerdev, vid);
573 static int macvlan_vlan_rx_kill_vid(struct net_device *dev,
576 struct macvlan_dev *vlan = netdev_priv(dev);
577 struct net_device *lowerdev = vlan->lowerdev;
579 vlan_vid_del(lowerdev, vid);
583 static int macvlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
584 struct net_device *dev,
585 const unsigned char *addr,
588 struct macvlan_dev *vlan = netdev_priv(dev);
591 if (!vlan->port->passthru)
594 if (is_unicast_ether_addr(addr))
595 err = dev_uc_add_excl(dev, addr);
596 else if (is_multicast_ether_addr(addr))
597 err = dev_mc_add_excl(dev, addr);
602 static int macvlan_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
603 struct net_device *dev,
604 const unsigned char *addr)
606 struct macvlan_dev *vlan = netdev_priv(dev);
609 if (!vlan->port->passthru)
612 if (is_unicast_ether_addr(addr))
613 err = dev_uc_del(dev, addr);
614 else if (is_multicast_ether_addr(addr))
615 err = dev_mc_del(dev, addr);
620 static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
621 struct ethtool_drvinfo *drvinfo)
623 strlcpy(drvinfo->driver, "macvlan", sizeof(drvinfo->driver));
624 strlcpy(drvinfo->version, "0.1", sizeof(drvinfo->version));
627 static int macvlan_ethtool_get_settings(struct net_device *dev,
628 struct ethtool_cmd *cmd)
630 const struct macvlan_dev *vlan = netdev_priv(dev);
632 return __ethtool_get_settings(vlan->lowerdev, cmd);
635 static const struct ethtool_ops macvlan_ethtool_ops = {
636 .get_link = ethtool_op_get_link,
637 .get_settings = macvlan_ethtool_get_settings,
638 .get_drvinfo = macvlan_ethtool_get_drvinfo,
641 static const struct net_device_ops macvlan_netdev_ops = {
642 .ndo_init = macvlan_init,
643 .ndo_uninit = macvlan_uninit,
644 .ndo_open = macvlan_open,
645 .ndo_stop = macvlan_stop,
646 .ndo_start_xmit = macvlan_start_xmit,
647 .ndo_change_mtu = macvlan_change_mtu,
648 .ndo_change_rx_flags = macvlan_change_rx_flags,
649 .ndo_set_mac_address = macvlan_set_mac_address,
650 .ndo_set_rx_mode = macvlan_set_mac_lists,
651 .ndo_get_stats64 = macvlan_dev_get_stats64,
652 .ndo_validate_addr = eth_validate_addr,
653 .ndo_vlan_rx_add_vid = macvlan_vlan_rx_add_vid,
654 .ndo_vlan_rx_kill_vid = macvlan_vlan_rx_kill_vid,
655 .ndo_fdb_add = macvlan_fdb_add,
656 .ndo_fdb_del = macvlan_fdb_del,
657 .ndo_fdb_dump = ndo_dflt_fdb_dump,
660 void macvlan_common_setup(struct net_device *dev)
664 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
665 dev->netdev_ops = &macvlan_netdev_ops;
666 dev->destructor = free_netdev;
667 dev->header_ops = &macvlan_hard_header_ops,
668 dev->ethtool_ops = &macvlan_ethtool_ops;
670 EXPORT_SYMBOL_GPL(macvlan_common_setup);
672 static void macvlan_setup(struct net_device *dev)
674 macvlan_common_setup(dev);
675 dev->tx_queue_len = 0;
678 static int macvlan_port_create(struct net_device *dev)
680 struct macvlan_port *port;
684 if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
687 port = kzalloc(sizeof(*port), GFP_KERNEL);
691 port->passthru = false;
693 INIT_LIST_HEAD(&port->vlans);
694 for (i = 0; i < MACVLAN_HASH_SIZE; i++)
695 INIT_HLIST_HEAD(&port->vlan_hash[i]);
697 err = netdev_rx_handler_register(dev, macvlan_handle_frame, port);
701 dev->priv_flags |= IFF_MACVLAN_PORT;
705 static void macvlan_port_destroy(struct net_device *dev)
707 struct macvlan_port *port = macvlan_port_get(dev);
709 dev->priv_flags &= ~IFF_MACVLAN_PORT;
710 netdev_rx_handler_unregister(dev);
711 kfree_rcu(port, rcu);
714 static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
716 if (tb[IFLA_ADDRESS]) {
717 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
719 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
720 return -EADDRNOTAVAIL;
723 if (data && data[IFLA_MACVLAN_MODE]) {
724 switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) {
725 case MACVLAN_MODE_PRIVATE:
726 case MACVLAN_MODE_VEPA:
727 case MACVLAN_MODE_BRIDGE:
728 case MACVLAN_MODE_PASSTHRU:
737 int macvlan_common_newlink(struct net *src_net, struct net_device *dev,
738 struct nlattr *tb[], struct nlattr *data[],
739 int (*receive)(struct sk_buff *skb),
740 int (*forward)(struct net_device *dev,
741 struct sk_buff *skb))
743 struct macvlan_dev *vlan = netdev_priv(dev);
744 struct macvlan_port *port;
745 struct net_device *lowerdev;
751 lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
752 if (lowerdev == NULL)
755 /* When creating macvlans on top of other macvlans - use
756 * the real device as the lowerdev.
758 if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) {
759 struct macvlan_dev *lowervlan = netdev_priv(lowerdev);
760 lowerdev = lowervlan->lowerdev;
764 dev->mtu = lowerdev->mtu;
765 else if (dev->mtu > lowerdev->mtu)
768 if (!tb[IFLA_ADDRESS])
769 eth_hw_addr_random(dev);
771 if (!macvlan_port_exists(lowerdev)) {
772 err = macvlan_port_create(lowerdev);
776 port = macvlan_port_get(lowerdev);
778 /* Only 1 macvlan device can be created in passthru mode */
782 vlan->lowerdev = lowerdev;
785 vlan->receive = receive;
786 vlan->forward = forward;
788 vlan->mode = MACVLAN_MODE_VEPA;
789 if (data && data[IFLA_MACVLAN_MODE])
790 vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
792 if (data && data[IFLA_MACVLAN_FLAGS])
793 vlan->flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
795 if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
798 port->passthru = true;
799 memcpy(dev->dev_addr, lowerdev->dev_addr, ETH_ALEN);
802 err = netdev_upper_dev_link(lowerdev, dev);
807 err = register_netdevice(dev);
809 goto upper_dev_unlink;
811 list_add_tail(&vlan->list, &port->vlans);
812 netif_stacked_transfer_operstate(lowerdev, dev);
817 netdev_upper_dev_unlink(lowerdev, dev);
821 macvlan_port_destroy(lowerdev);
825 EXPORT_SYMBOL_GPL(macvlan_common_newlink);
827 static int macvlan_newlink(struct net *src_net, struct net_device *dev,
828 struct nlattr *tb[], struct nlattr *data[])
830 return macvlan_common_newlink(src_net, dev, tb, data,
835 void macvlan_dellink(struct net_device *dev, struct list_head *head)
837 struct macvlan_dev *vlan = netdev_priv(dev);
839 list_del(&vlan->list);
840 unregister_netdevice_queue(dev, head);
841 netdev_upper_dev_unlink(vlan->lowerdev, dev);
843 EXPORT_SYMBOL_GPL(macvlan_dellink);
845 static int macvlan_changelink(struct net_device *dev,
846 struct nlattr *tb[], struct nlattr *data[])
848 struct macvlan_dev *vlan = netdev_priv(dev);
849 if (data && data[IFLA_MACVLAN_MODE])
850 vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
851 if (data && data[IFLA_MACVLAN_FLAGS]) {
852 __u16 flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
853 bool promisc = (flags ^ vlan->flags) & MACVLAN_FLAG_NOPROMISC;
855 if (promisc && (flags & MACVLAN_FLAG_NOPROMISC))
856 dev_set_promiscuity(vlan->lowerdev, -1);
857 else if (promisc && !(flags & MACVLAN_FLAG_NOPROMISC))
858 dev_set_promiscuity(vlan->lowerdev, 1);
864 static size_t macvlan_get_size(const struct net_device *dev)
867 + nla_total_size(4) /* IFLA_MACVLAN_MODE */
868 + nla_total_size(2) /* IFLA_MACVLAN_FLAGS */
872 static int macvlan_fill_info(struct sk_buff *skb,
873 const struct net_device *dev)
875 struct macvlan_dev *vlan = netdev_priv(dev);
877 if (nla_put_u32(skb, IFLA_MACVLAN_MODE, vlan->mode))
878 goto nla_put_failure;
879 if (nla_put_u16(skb, IFLA_MACVLAN_FLAGS, vlan->flags))
880 goto nla_put_failure;
887 static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = {
888 [IFLA_MACVLAN_MODE] = { .type = NLA_U32 },
889 [IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 },
892 int macvlan_link_register(struct rtnl_link_ops *ops)
895 ops->priv_size = sizeof(struct macvlan_dev);
896 ops->validate = macvlan_validate;
897 ops->maxtype = IFLA_MACVLAN_MAX;
898 ops->policy = macvlan_policy;
899 ops->changelink = macvlan_changelink;
900 ops->get_size = macvlan_get_size;
901 ops->fill_info = macvlan_fill_info;
903 return rtnl_link_register(ops);
905 EXPORT_SYMBOL_GPL(macvlan_link_register);
907 static struct rtnl_link_ops macvlan_link_ops = {
909 .setup = macvlan_setup,
910 .newlink = macvlan_newlink,
911 .dellink = macvlan_dellink,
914 static int macvlan_device_event(struct notifier_block *unused,
915 unsigned long event, void *ptr)
917 struct net_device *dev = ptr;
918 struct macvlan_dev *vlan, *next;
919 struct macvlan_port *port;
920 LIST_HEAD(list_kill);
922 if (!macvlan_port_exists(dev))
925 port = macvlan_port_get(dev);
929 list_for_each_entry(vlan, &port->vlans, list)
930 netif_stacked_transfer_operstate(vlan->lowerdev,
933 case NETDEV_FEAT_CHANGE:
934 list_for_each_entry(vlan, &port->vlans, list) {
935 vlan->dev->features = dev->features & MACVLAN_FEATURES;
936 vlan->dev->gso_max_size = dev->gso_max_size;
937 netdev_features_change(vlan->dev);
940 case NETDEV_UNREGISTER:
941 /* twiddle thumbs on netns device moves */
942 if (dev->reg_state != NETREG_UNREGISTERING)
945 list_for_each_entry_safe(vlan, next, &port->vlans, list)
946 vlan->dev->rtnl_link_ops->dellink(vlan->dev, &list_kill);
947 unregister_netdevice_many(&list_kill);
948 list_del(&list_kill);
950 case NETDEV_PRE_TYPE_CHANGE:
951 /* Forbid underlaying device to change its type. */
957 static struct notifier_block macvlan_notifier_block __read_mostly = {
958 .notifier_call = macvlan_device_event,
961 static int __init macvlan_init_module(void)
965 register_netdevice_notifier(&macvlan_notifier_block);
967 err = macvlan_link_register(&macvlan_link_ops);
972 unregister_netdevice_notifier(&macvlan_notifier_block);
976 static void __exit macvlan_cleanup_module(void)
978 rtnl_link_unregister(&macvlan_link_ops);
979 unregister_netdevice_notifier(&macvlan_notifier_block);
982 module_init(macvlan_init_module);
983 module_exit(macvlan_cleanup_module);
985 MODULE_LICENSE("GPL");
986 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
987 MODULE_DESCRIPTION("Driver for MAC address based VLANs");
988 MODULE_ALIAS_RTNL_LINK("macvlan");