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_link.h>
30 #include <linux/if_macvlan.h>
31 #include <net/rtnetlink.h>
34 #define MACVLAN_HASH_SIZE (1 << BITS_PER_BYTE)
37 struct net_device *dev;
38 struct hlist_head vlan_hash[MACVLAN_HASH_SIZE];
39 struct list_head vlans;
44 #define macvlan_port_get_rcu(dev) \
45 ((struct macvlan_port *) rcu_dereference(dev->rx_handler_data))
46 #define macvlan_port_get(dev) ((struct macvlan_port *) dev->rx_handler_data)
47 #define macvlan_port_exists(dev) (dev->priv_flags & IFF_MACVLAN_PORT)
49 static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
50 const unsigned char *addr)
52 struct macvlan_dev *vlan;
55 hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[addr[5]], hlist) {
56 if (!compare_ether_addr_64bits(vlan->dev->dev_addr, addr))
62 static void macvlan_hash_add(struct macvlan_dev *vlan)
64 struct macvlan_port *port = vlan->port;
65 const unsigned char *addr = vlan->dev->dev_addr;
67 hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]);
70 static void macvlan_hash_del(struct macvlan_dev *vlan)
72 hlist_del_rcu(&vlan->hlist);
76 static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
77 const unsigned char *addr)
79 macvlan_hash_del(vlan);
80 /* Now that we are unhashed it is safe to change the device
81 * address without confusing packet delivery.
83 memcpy(vlan->dev->dev_addr, addr, ETH_ALEN);
84 macvlan_hash_add(vlan);
87 static int macvlan_addr_busy(const struct macvlan_port *port,
88 const unsigned char *addr)
90 /* Test to see if the specified multicast address is
91 * currently in use by the underlying device or
94 if (!compare_ether_addr_64bits(port->dev->dev_addr, addr))
97 if (macvlan_hash_lookup(port, addr))
104 static int macvlan_broadcast_one(struct sk_buff *skb,
105 const struct macvlan_dev *vlan,
106 const struct ethhdr *eth, bool local)
108 struct net_device *dev = vlan->dev;
113 return vlan->forward(dev, skb);
116 if (!compare_ether_addr_64bits(eth->h_dest,
118 skb->pkt_type = PACKET_BROADCAST;
120 skb->pkt_type = PACKET_MULTICAST;
122 return vlan->receive(skb);
125 static void macvlan_broadcast(struct sk_buff *skb,
126 const struct macvlan_port *port,
127 struct net_device *src,
128 enum macvlan_mode mode)
130 const struct ethhdr *eth = eth_hdr(skb);
131 const struct macvlan_dev *vlan;
132 struct hlist_node *n;
133 struct sk_buff *nskb;
137 if (skb->protocol == htons(ETH_P_PAUSE))
140 for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
141 hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[i], hlist) {
142 if (vlan->dev == src || !(vlan->mode & mode))
145 nskb = skb_clone(skb, GFP_ATOMIC);
146 err = macvlan_broadcast_one(nskb, vlan, eth,
147 mode == MACVLAN_MODE_BRIDGE);
148 macvlan_count_rx(vlan, skb->len + ETH_HLEN,
149 err == NET_RX_SUCCESS, 1);
154 /* called under rcu_read_lock() from netif_receive_skb */
155 static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb)
157 struct macvlan_port *port;
158 struct sk_buff *skb = *pskb;
159 const struct ethhdr *eth = eth_hdr(skb);
160 const struct macvlan_dev *vlan;
161 const struct macvlan_dev *src;
162 struct net_device *dev;
163 unsigned int len = 0;
164 int ret = NET_RX_DROP;
166 port = macvlan_port_get_rcu(skb->dev);
167 if (is_multicast_ether_addr(eth->h_dest)) {
168 src = macvlan_hash_lookup(port, eth->h_source);
170 /* frame comes from an external address */
171 macvlan_broadcast(skb, port, NULL,
172 MACVLAN_MODE_PRIVATE |
174 MACVLAN_MODE_PASSTHRU|
175 MACVLAN_MODE_BRIDGE);
176 else if (src->mode == MACVLAN_MODE_VEPA)
177 /* flood to everyone except source */
178 macvlan_broadcast(skb, port, src->dev,
180 MACVLAN_MODE_BRIDGE);
181 else if (src->mode == MACVLAN_MODE_BRIDGE)
183 * flood only to VEPA ports, bridge ports
184 * already saw the frame on the way out.
186 macvlan_broadcast(skb, port, src->dev,
188 return RX_HANDLER_PASS;
192 vlan = list_first_entry(&port->vlans, struct macvlan_dev, list);
194 vlan = macvlan_hash_lookup(port, eth->h_dest);
196 return RX_HANDLER_PASS;
199 if (unlikely(!(dev->flags & IFF_UP))) {
201 return RX_HANDLER_CONSUMED;
203 len = skb->len + ETH_HLEN;
204 skb = skb_share_check(skb, GFP_ATOMIC);
209 skb->pkt_type = PACKET_HOST;
211 ret = vlan->receive(skb);
214 macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, 0);
215 return RX_HANDLER_CONSUMED;
218 static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
220 const struct macvlan_dev *vlan = netdev_priv(dev);
221 const struct macvlan_port *port = vlan->port;
222 const struct macvlan_dev *dest;
223 __u8 ip_summed = skb->ip_summed;
225 if (vlan->mode == MACVLAN_MODE_BRIDGE) {
226 const struct ethhdr *eth = (void *)skb->data;
227 skb->ip_summed = CHECKSUM_UNNECESSARY;
229 /* send to other bridge ports directly */
230 if (is_multicast_ether_addr(eth->h_dest)) {
231 macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE);
235 dest = macvlan_hash_lookup(port, eth->h_dest);
236 if (dest && dest->mode == MACVLAN_MODE_BRIDGE) {
237 unsigned int length = skb->len + ETH_HLEN;
238 int ret = dest->forward(dest->dev, skb);
239 macvlan_count_rx(dest, length,
240 ret == NET_RX_SUCCESS, 0);
242 return NET_XMIT_SUCCESS;
247 skb->ip_summed = ip_summed;
248 skb_set_dev(skb, vlan->lowerdev);
249 return dev_queue_xmit(skb);
252 netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
253 struct net_device *dev)
255 unsigned int len = skb->len;
257 const struct macvlan_dev *vlan = netdev_priv(dev);
259 ret = macvlan_queue_xmit(skb, dev);
260 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
261 struct macvlan_pcpu_stats *pcpu_stats;
263 pcpu_stats = this_cpu_ptr(vlan->pcpu_stats);
264 u64_stats_update_begin(&pcpu_stats->syncp);
265 pcpu_stats->tx_packets++;
266 pcpu_stats->tx_bytes += len;
267 u64_stats_update_end(&pcpu_stats->syncp);
269 this_cpu_inc(vlan->pcpu_stats->tx_dropped);
273 EXPORT_SYMBOL_GPL(macvlan_start_xmit);
275 static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
276 unsigned short type, const void *daddr,
277 const void *saddr, unsigned len)
279 const struct macvlan_dev *vlan = netdev_priv(dev);
280 struct net_device *lowerdev = vlan->lowerdev;
282 return dev_hard_header(skb, lowerdev, type, daddr,
283 saddr ? : dev->dev_addr, len);
286 static const struct header_ops macvlan_hard_header_ops = {
287 .create = macvlan_hard_header,
288 .rebuild = eth_rebuild_header,
289 .parse = eth_header_parse,
290 .cache = eth_header_cache,
291 .cache_update = eth_header_cache_update,
294 static int macvlan_open(struct net_device *dev)
296 struct macvlan_dev *vlan = netdev_priv(dev);
297 struct net_device *lowerdev = vlan->lowerdev;
300 if (vlan->port->passthru) {
301 dev_set_promiscuity(lowerdev, 1);
306 if (macvlan_addr_busy(vlan->port, dev->dev_addr))
309 err = dev_uc_add(lowerdev, dev->dev_addr);
312 if (dev->flags & IFF_ALLMULTI) {
313 err = dev_set_allmulti(lowerdev, 1);
319 macvlan_hash_add(vlan);
323 dev_uc_del(lowerdev, dev->dev_addr);
328 static int macvlan_stop(struct net_device *dev)
330 struct macvlan_dev *vlan = netdev_priv(dev);
331 struct net_device *lowerdev = vlan->lowerdev;
333 if (vlan->port->passthru) {
334 dev_set_promiscuity(lowerdev, -1);
338 dev_mc_unsync(lowerdev, dev);
339 if (dev->flags & IFF_ALLMULTI)
340 dev_set_allmulti(lowerdev, -1);
342 dev_uc_del(lowerdev, dev->dev_addr);
345 macvlan_hash_del(vlan);
349 static int macvlan_set_mac_address(struct net_device *dev, void *p)
351 struct macvlan_dev *vlan = netdev_priv(dev);
352 struct net_device *lowerdev = vlan->lowerdev;
353 struct sockaddr *addr = p;
356 if (!is_valid_ether_addr(addr->sa_data))
357 return -EADDRNOTAVAIL;
359 if (!(dev->flags & IFF_UP)) {
360 /* Just copy in the new address */
361 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
363 /* Rehash and update the device filters */
364 if (macvlan_addr_busy(vlan->port, addr->sa_data))
367 err = dev_uc_add(lowerdev, addr->sa_data);
371 dev_uc_del(lowerdev, dev->dev_addr);
373 macvlan_hash_change_addr(vlan, addr->sa_data);
378 static void macvlan_change_rx_flags(struct net_device *dev, int change)
380 struct macvlan_dev *vlan = netdev_priv(dev);
381 struct net_device *lowerdev = vlan->lowerdev;
383 if (change & IFF_ALLMULTI)
384 dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
387 static void macvlan_set_multicast_list(struct net_device *dev)
389 struct macvlan_dev *vlan = netdev_priv(dev);
391 dev_mc_sync(vlan->lowerdev, dev);
394 static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
396 struct macvlan_dev *vlan = netdev_priv(dev);
398 if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
405 * macvlan network devices have devices nesting below it and are a special
406 * "super class" of normal network devices; split their locks off into a
407 * separate class since they always nest.
409 static struct lock_class_key macvlan_netdev_xmit_lock_key;
410 static struct lock_class_key macvlan_netdev_addr_lock_key;
412 #define MACVLAN_FEATURES \
413 (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
414 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
415 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO)
417 #define MACVLAN_STATE_MASK \
418 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
420 static void macvlan_set_lockdep_class_one(struct net_device *dev,
421 struct netdev_queue *txq,
424 lockdep_set_class(&txq->_xmit_lock,
425 &macvlan_netdev_xmit_lock_key);
428 static void macvlan_set_lockdep_class(struct net_device *dev)
430 lockdep_set_class(&dev->addr_list_lock,
431 &macvlan_netdev_addr_lock_key);
432 netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL);
435 static int macvlan_init(struct net_device *dev)
437 struct macvlan_dev *vlan = netdev_priv(dev);
438 const struct net_device *lowerdev = vlan->lowerdev;
440 dev->state = (dev->state & ~MACVLAN_STATE_MASK) |
441 (lowerdev->state & MACVLAN_STATE_MASK);
442 dev->features = lowerdev->features & MACVLAN_FEATURES;
443 dev->features |= NETIF_F_LLTX;
444 dev->gso_max_size = lowerdev->gso_max_size;
445 dev->iflink = lowerdev->ifindex;
446 dev->hard_header_len = lowerdev->hard_header_len;
448 macvlan_set_lockdep_class(dev);
450 vlan->pcpu_stats = alloc_percpu(struct macvlan_pcpu_stats);
451 if (!vlan->pcpu_stats)
457 static void macvlan_uninit(struct net_device *dev)
459 struct macvlan_dev *vlan = netdev_priv(dev);
461 free_percpu(vlan->pcpu_stats);
464 static struct rtnl_link_stats64 *macvlan_dev_get_stats64(struct net_device *dev,
465 struct rtnl_link_stats64 *stats)
467 struct macvlan_dev *vlan = netdev_priv(dev);
469 if (vlan->pcpu_stats) {
470 struct macvlan_pcpu_stats *p;
471 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
472 u32 rx_errors = 0, tx_dropped = 0;
476 for_each_possible_cpu(i) {
477 p = per_cpu_ptr(vlan->pcpu_stats, i);
479 start = u64_stats_fetch_begin_bh(&p->syncp);
480 rx_packets = p->rx_packets;
481 rx_bytes = p->rx_bytes;
482 rx_multicast = p->rx_multicast;
483 tx_packets = p->tx_packets;
484 tx_bytes = p->tx_bytes;
485 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
487 stats->rx_packets += rx_packets;
488 stats->rx_bytes += rx_bytes;
489 stats->multicast += rx_multicast;
490 stats->tx_packets += tx_packets;
491 stats->tx_bytes += tx_bytes;
492 /* rx_errors & tx_dropped are u32, updated
493 * without syncp protection.
495 rx_errors += p->rx_errors;
496 tx_dropped += p->tx_dropped;
498 stats->rx_errors = rx_errors;
499 stats->rx_dropped = rx_errors;
500 stats->tx_dropped = tx_dropped;
505 static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
506 struct ethtool_drvinfo *drvinfo)
508 snprintf(drvinfo->driver, 32, "macvlan");
509 snprintf(drvinfo->version, 32, "0.1");
512 static u32 macvlan_ethtool_get_rx_csum(struct net_device *dev)
514 const struct macvlan_dev *vlan = netdev_priv(dev);
515 return dev_ethtool_get_rx_csum(vlan->lowerdev);
518 static int macvlan_ethtool_get_settings(struct net_device *dev,
519 struct ethtool_cmd *cmd)
521 const struct macvlan_dev *vlan = netdev_priv(dev);
522 return dev_ethtool_get_settings(vlan->lowerdev, cmd);
525 static u32 macvlan_ethtool_get_flags(struct net_device *dev)
527 const struct macvlan_dev *vlan = netdev_priv(dev);
528 return dev_ethtool_get_flags(vlan->lowerdev);
531 static const struct ethtool_ops macvlan_ethtool_ops = {
532 .get_link = ethtool_op_get_link,
533 .get_settings = macvlan_ethtool_get_settings,
534 .get_rx_csum = macvlan_ethtool_get_rx_csum,
535 .get_drvinfo = macvlan_ethtool_get_drvinfo,
536 .get_flags = macvlan_ethtool_get_flags,
539 static const struct net_device_ops macvlan_netdev_ops = {
540 .ndo_init = macvlan_init,
541 .ndo_uninit = macvlan_uninit,
542 .ndo_open = macvlan_open,
543 .ndo_stop = macvlan_stop,
544 .ndo_start_xmit = macvlan_start_xmit,
545 .ndo_change_mtu = macvlan_change_mtu,
546 .ndo_change_rx_flags = macvlan_change_rx_flags,
547 .ndo_set_mac_address = macvlan_set_mac_address,
548 .ndo_set_multicast_list = macvlan_set_multicast_list,
549 .ndo_get_stats64 = macvlan_dev_get_stats64,
550 .ndo_validate_addr = eth_validate_addr,
553 void macvlan_common_setup(struct net_device *dev)
557 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
558 dev->netdev_ops = &macvlan_netdev_ops;
559 dev->destructor = free_netdev;
560 dev->header_ops = &macvlan_hard_header_ops,
561 dev->ethtool_ops = &macvlan_ethtool_ops;
563 EXPORT_SYMBOL_GPL(macvlan_common_setup);
565 static void macvlan_setup(struct net_device *dev)
567 macvlan_common_setup(dev);
568 dev->tx_queue_len = 0;
571 static int macvlan_port_create(struct net_device *dev)
573 struct macvlan_port *port;
577 if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
580 port = kzalloc(sizeof(*port), GFP_KERNEL);
584 port->passthru = false;
586 INIT_LIST_HEAD(&port->vlans);
587 for (i = 0; i < MACVLAN_HASH_SIZE; i++)
588 INIT_HLIST_HEAD(&port->vlan_hash[i]);
590 err = netdev_rx_handler_register(dev, macvlan_handle_frame, port);
594 dev->priv_flags |= IFF_MACVLAN_PORT;
598 static void macvlan_port_rcu_free(struct rcu_head *head)
600 struct macvlan_port *port;
602 port = container_of(head, struct macvlan_port, rcu);
606 static void macvlan_port_destroy(struct net_device *dev)
608 struct macvlan_port *port = macvlan_port_get(dev);
610 dev->priv_flags &= ~IFF_MACVLAN_PORT;
611 netdev_rx_handler_unregister(dev);
612 call_rcu(&port->rcu, macvlan_port_rcu_free);
615 static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
617 if (tb[IFLA_ADDRESS]) {
618 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
620 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
621 return -EADDRNOTAVAIL;
624 if (data && data[IFLA_MACVLAN_MODE]) {
625 switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) {
626 case MACVLAN_MODE_PRIVATE:
627 case MACVLAN_MODE_VEPA:
628 case MACVLAN_MODE_BRIDGE:
629 case MACVLAN_MODE_PASSTHRU:
638 int macvlan_common_newlink(struct net *src_net, struct net_device *dev,
639 struct nlattr *tb[], struct nlattr *data[],
640 int (*receive)(struct sk_buff *skb),
641 int (*forward)(struct net_device *dev,
642 struct sk_buff *skb))
644 struct macvlan_dev *vlan = netdev_priv(dev);
645 struct macvlan_port *port;
646 struct net_device *lowerdev;
652 lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
653 if (lowerdev == NULL)
656 /* When creating macvlans on top of other macvlans - use
657 * the real device as the lowerdev.
659 if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) {
660 struct macvlan_dev *lowervlan = netdev_priv(lowerdev);
661 lowerdev = lowervlan->lowerdev;
665 dev->mtu = lowerdev->mtu;
666 else if (dev->mtu > lowerdev->mtu)
669 if (!tb[IFLA_ADDRESS])
670 random_ether_addr(dev->dev_addr);
672 if (!macvlan_port_exists(lowerdev)) {
673 err = macvlan_port_create(lowerdev);
677 port = macvlan_port_get(lowerdev);
679 /* Only 1 macvlan device can be created in passthru mode */
683 vlan->lowerdev = lowerdev;
686 vlan->receive = receive;
687 vlan->forward = forward;
689 vlan->mode = MACVLAN_MODE_VEPA;
690 if (data && data[IFLA_MACVLAN_MODE])
691 vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
693 if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
694 if (!list_empty(&port->vlans))
696 port->passthru = true;
697 memcpy(dev->dev_addr, lowerdev->dev_addr, ETH_ALEN);
700 err = register_netdevice(dev);
704 list_add_tail(&vlan->list, &port->vlans);
705 netif_stacked_transfer_operstate(lowerdev, dev);
710 if (list_empty(&port->vlans))
711 macvlan_port_destroy(lowerdev);
715 EXPORT_SYMBOL_GPL(macvlan_common_newlink);
717 static int macvlan_newlink(struct net *src_net, struct net_device *dev,
718 struct nlattr *tb[], struct nlattr *data[])
720 return macvlan_common_newlink(src_net, dev, tb, data,
725 void macvlan_dellink(struct net_device *dev, struct list_head *head)
727 struct macvlan_dev *vlan = netdev_priv(dev);
728 struct macvlan_port *port = vlan->port;
730 list_del(&vlan->list);
731 unregister_netdevice_queue(dev, head);
733 if (list_empty(&port->vlans))
734 macvlan_port_destroy(port->dev);
736 EXPORT_SYMBOL_GPL(macvlan_dellink);
738 static int macvlan_changelink(struct net_device *dev,
739 struct nlattr *tb[], struct nlattr *data[])
741 struct macvlan_dev *vlan = netdev_priv(dev);
742 if (data && data[IFLA_MACVLAN_MODE])
743 vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
747 static size_t macvlan_get_size(const struct net_device *dev)
749 return nla_total_size(4);
752 static int macvlan_fill_info(struct sk_buff *skb,
753 const struct net_device *dev)
755 struct macvlan_dev *vlan = netdev_priv(dev);
757 NLA_PUT_U32(skb, IFLA_MACVLAN_MODE, vlan->mode);
764 static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = {
765 [IFLA_MACVLAN_MODE] = { .type = NLA_U32 },
768 int macvlan_link_register(struct rtnl_link_ops *ops)
771 ops->priv_size = sizeof(struct macvlan_dev);
772 ops->validate = macvlan_validate;
773 ops->maxtype = IFLA_MACVLAN_MAX;
774 ops->policy = macvlan_policy;
775 ops->changelink = macvlan_changelink;
776 ops->get_size = macvlan_get_size;
777 ops->fill_info = macvlan_fill_info;
779 return rtnl_link_register(ops);
781 EXPORT_SYMBOL_GPL(macvlan_link_register);
783 static struct rtnl_link_ops macvlan_link_ops = {
785 .setup = macvlan_setup,
786 .newlink = macvlan_newlink,
787 .dellink = macvlan_dellink,
790 static int macvlan_device_event(struct notifier_block *unused,
791 unsigned long event, void *ptr)
793 struct net_device *dev = ptr;
794 struct macvlan_dev *vlan, *next;
795 struct macvlan_port *port;
797 if (!macvlan_port_exists(dev))
800 port = macvlan_port_get(dev);
804 list_for_each_entry(vlan, &port->vlans, list)
805 netif_stacked_transfer_operstate(vlan->lowerdev,
808 case NETDEV_FEAT_CHANGE:
809 list_for_each_entry(vlan, &port->vlans, list) {
810 vlan->dev->features = dev->features & MACVLAN_FEATURES;
811 vlan->dev->gso_max_size = dev->gso_max_size;
812 netdev_features_change(vlan->dev);
815 case NETDEV_UNREGISTER:
816 /* twiddle thumbs on netns device moves */
817 if (dev->reg_state != NETREG_UNREGISTERING)
820 list_for_each_entry_safe(vlan, next, &port->vlans, list)
821 vlan->dev->rtnl_link_ops->dellink(vlan->dev, NULL);
823 case NETDEV_PRE_TYPE_CHANGE:
824 /* Forbid underlaying device to change its type. */
830 static struct notifier_block macvlan_notifier_block __read_mostly = {
831 .notifier_call = macvlan_device_event,
834 static int __init macvlan_init_module(void)
838 register_netdevice_notifier(&macvlan_notifier_block);
840 err = macvlan_link_register(&macvlan_link_ops);
845 unregister_netdevice_notifier(&macvlan_notifier_block);
849 static void __exit macvlan_cleanup_module(void)
851 rtnl_link_unregister(&macvlan_link_ops);
852 unregister_netdevice_notifier(&macvlan_notifier_block);
855 module_init(macvlan_init_module);
856 module_exit(macvlan_cleanup_module);
858 MODULE_LICENSE("GPL");
859 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
860 MODULE_DESCRIPTION("Driver for MAC address based VLANs");
861 MODULE_ALIAS_RTNL_LINK("macvlan");