macvlan: export macvlan mode through netlink
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / macvlan.c
blob322112c7358afba98f13dadd871dd08e7afe5146
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_link.h>
30 #include <linux/if_macvlan.h>
31 #include <net/rtnetlink.h>
32 #include <net/xfrm.h>
34 #define MACVLAN_HASH_SIZE (1 << BITS_PER_BYTE)
36 struct macvlan_port {
37 struct net_device *dev;
38 struct hlist_head vlan_hash[MACVLAN_HASH_SIZE];
39 struct list_head vlans;
42 /**
43 * struct macvlan_rx_stats - MACVLAN percpu rx stats
44 * @rx_packets: number of received packets
45 * @rx_bytes: number of received bytes
46 * @multicast: number of received multicast packets
47 * @rx_errors: number of errors
49 struct macvlan_rx_stats {
50 unsigned long rx_packets;
51 unsigned long rx_bytes;
52 unsigned long multicast;
53 unsigned long rx_errors;
56 struct macvlan_dev {
57 struct net_device *dev;
58 struct list_head list;
59 struct hlist_node hlist;
60 struct macvlan_port *port;
61 struct net_device *lowerdev;
62 struct macvlan_rx_stats *rx_stats;
63 enum macvlan_mode mode;
67 static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
68 const unsigned char *addr)
70 struct macvlan_dev *vlan;
71 struct hlist_node *n;
73 hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[addr[5]], hlist) {
74 if (!compare_ether_addr_64bits(vlan->dev->dev_addr, addr))
75 return vlan;
77 return NULL;
80 static void macvlan_hash_add(struct macvlan_dev *vlan)
82 struct macvlan_port *port = vlan->port;
83 const unsigned char *addr = vlan->dev->dev_addr;
85 hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]);
88 static void macvlan_hash_del(struct macvlan_dev *vlan)
90 hlist_del_rcu(&vlan->hlist);
91 synchronize_rcu();
94 static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
95 const unsigned char *addr)
97 macvlan_hash_del(vlan);
98 /* Now that we are unhashed it is safe to change the device
99 * address without confusing packet delivery.
101 memcpy(vlan->dev->dev_addr, addr, ETH_ALEN);
102 macvlan_hash_add(vlan);
105 static int macvlan_addr_busy(const struct macvlan_port *port,
106 const unsigned char *addr)
108 /* Test to see if the specified multicast address is
109 * currently in use by the underlying device or
110 * another macvlan.
112 if (!compare_ether_addr_64bits(port->dev->dev_addr, addr))
113 return 1;
115 if (macvlan_hash_lookup(port, addr))
116 return 1;
118 return 0;
121 static inline void macvlan_count_rx(const struct macvlan_dev *vlan,
122 unsigned int len, bool success,
123 bool multicast)
125 struct macvlan_rx_stats *rx_stats;
127 rx_stats = per_cpu_ptr(vlan->rx_stats, smp_processor_id());
128 if (likely(success)) {
129 rx_stats->rx_packets++;;
130 rx_stats->rx_bytes += len;
131 if (multicast)
132 rx_stats->multicast++;
133 } else {
134 rx_stats->rx_errors++;
138 static int macvlan_broadcast_one(struct sk_buff *skb, struct net_device *dev,
139 const struct ethhdr *eth, bool local)
141 if (!skb)
142 return NET_RX_DROP;
144 if (local)
145 return dev_forward_skb(dev, skb);
147 skb->dev = dev;
148 if (!compare_ether_addr_64bits(eth->h_dest,
149 dev->broadcast))
150 skb->pkt_type = PACKET_BROADCAST;
151 else
152 skb->pkt_type = PACKET_MULTICAST;
154 return netif_rx(skb);
157 static void macvlan_broadcast(struct sk_buff *skb,
158 const struct macvlan_port *port,
159 struct net_device *src,
160 enum macvlan_mode mode)
162 const struct ethhdr *eth = eth_hdr(skb);
163 const struct macvlan_dev *vlan;
164 struct hlist_node *n;
165 struct sk_buff *nskb;
166 unsigned int i;
167 int err;
169 if (skb->protocol == htons(ETH_P_PAUSE))
170 return;
172 for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
173 hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[i], hlist) {
174 if (vlan->dev == src || !(vlan->mode & mode))
175 continue;
177 nskb = skb_clone(skb, GFP_ATOMIC);
178 err = macvlan_broadcast_one(nskb, vlan->dev, eth,
179 mode == MACVLAN_MODE_BRIDGE);
180 macvlan_count_rx(vlan, skb->len + ETH_HLEN,
181 err == NET_RX_SUCCESS, 1);
186 /* called under rcu_read_lock() from netif_receive_skb */
187 static struct sk_buff *macvlan_handle_frame(struct sk_buff *skb)
189 const struct ethhdr *eth = eth_hdr(skb);
190 const struct macvlan_port *port;
191 const struct macvlan_dev *vlan;
192 const struct macvlan_dev *src;
193 struct net_device *dev;
194 unsigned int len;
196 port = rcu_dereference(skb->dev->macvlan_port);
197 if (port == NULL)
198 return skb;
200 if (is_multicast_ether_addr(eth->h_dest)) {
201 src = macvlan_hash_lookup(port, eth->h_source);
202 if (!src)
203 /* frame comes from an external address */
204 macvlan_broadcast(skb, port, NULL,
205 MACVLAN_MODE_PRIVATE |
206 MACVLAN_MODE_VEPA |
207 MACVLAN_MODE_BRIDGE);
208 else if (src->mode == MACVLAN_MODE_VEPA)
209 /* flood to everyone except source */
210 macvlan_broadcast(skb, port, src->dev,
211 MACVLAN_MODE_VEPA |
212 MACVLAN_MODE_BRIDGE);
213 else if (src->mode == MACVLAN_MODE_BRIDGE)
215 * flood only to VEPA ports, bridge ports
216 * already saw the frame on the way out.
218 macvlan_broadcast(skb, port, src->dev,
219 MACVLAN_MODE_VEPA);
220 return skb;
223 vlan = macvlan_hash_lookup(port, eth->h_dest);
224 if (vlan == NULL)
225 return skb;
227 dev = vlan->dev;
228 if (unlikely(!(dev->flags & IFF_UP))) {
229 kfree_skb(skb);
230 return NULL;
232 len = skb->len + ETH_HLEN;
233 skb = skb_share_check(skb, GFP_ATOMIC);
234 macvlan_count_rx(vlan, len, skb != NULL, 0);
235 if (!skb)
236 return NULL;
238 skb->dev = dev;
239 skb->pkt_type = PACKET_HOST;
241 netif_rx(skb);
242 return NULL;
245 static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
247 const struct macvlan_dev *vlan = netdev_priv(dev);
248 const struct macvlan_port *port = vlan->port;
249 const struct macvlan_dev *dest;
251 if (vlan->mode == MACVLAN_MODE_BRIDGE) {
252 const struct ethhdr *eth = (void *)skb->data;
254 /* send to other bridge ports directly */
255 if (is_multicast_ether_addr(eth->h_dest)) {
256 macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE);
257 goto xmit_world;
260 dest = macvlan_hash_lookup(port, eth->h_dest);
261 if (dest && dest->mode == MACVLAN_MODE_BRIDGE) {
262 unsigned int length = skb->len + ETH_HLEN;
263 int ret = dev_forward_skb(dest->dev, skb);
264 macvlan_count_rx(dest, length,
265 ret == NET_RX_SUCCESS, 0);
267 return NET_XMIT_SUCCESS;
271 xmit_world:
272 skb->dev = vlan->lowerdev;
273 return dev_queue_xmit(skb);
276 static netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
277 struct net_device *dev)
279 int i = skb_get_queue_mapping(skb);
280 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
281 unsigned int len = skb->len;
282 int ret;
284 ret = macvlan_queue_xmit(skb, dev);
285 if (likely(ret == NET_XMIT_SUCCESS)) {
286 txq->tx_packets++;
287 txq->tx_bytes += len;
288 } else
289 txq->tx_dropped++;
291 return ret;
294 static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
295 unsigned short type, const void *daddr,
296 const void *saddr, unsigned len)
298 const struct macvlan_dev *vlan = netdev_priv(dev);
299 struct net_device *lowerdev = vlan->lowerdev;
301 return dev_hard_header(skb, lowerdev, type, daddr,
302 saddr ? : dev->dev_addr, len);
305 static const struct header_ops macvlan_hard_header_ops = {
306 .create = macvlan_hard_header,
307 .rebuild = eth_rebuild_header,
308 .parse = eth_header_parse,
309 .cache = eth_header_cache,
310 .cache_update = eth_header_cache_update,
313 static int macvlan_open(struct net_device *dev)
315 struct macvlan_dev *vlan = netdev_priv(dev);
316 struct net_device *lowerdev = vlan->lowerdev;
317 int err;
319 err = -EBUSY;
320 if (macvlan_addr_busy(vlan->port, dev->dev_addr))
321 goto out;
323 err = dev_unicast_add(lowerdev, dev->dev_addr);
324 if (err < 0)
325 goto out;
326 if (dev->flags & IFF_ALLMULTI) {
327 err = dev_set_allmulti(lowerdev, 1);
328 if (err < 0)
329 goto del_unicast;
331 macvlan_hash_add(vlan);
332 return 0;
334 del_unicast:
335 dev_unicast_delete(lowerdev, dev->dev_addr);
336 out:
337 return err;
340 static int macvlan_stop(struct net_device *dev)
342 struct macvlan_dev *vlan = netdev_priv(dev);
343 struct net_device *lowerdev = vlan->lowerdev;
345 dev_mc_unsync(lowerdev, dev);
346 if (dev->flags & IFF_ALLMULTI)
347 dev_set_allmulti(lowerdev, -1);
349 dev_unicast_delete(lowerdev, dev->dev_addr);
351 macvlan_hash_del(vlan);
352 return 0;
355 static int macvlan_set_mac_address(struct net_device *dev, void *p)
357 struct macvlan_dev *vlan = netdev_priv(dev);
358 struct net_device *lowerdev = vlan->lowerdev;
359 struct sockaddr *addr = p;
360 int err;
362 if (!is_valid_ether_addr(addr->sa_data))
363 return -EADDRNOTAVAIL;
365 if (!(dev->flags & IFF_UP)) {
366 /* Just copy in the new address */
367 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
368 } else {
369 /* Rehash and update the device filters */
370 if (macvlan_addr_busy(vlan->port, addr->sa_data))
371 return -EBUSY;
373 err = dev_unicast_add(lowerdev, addr->sa_data);
374 if (err)
375 return err;
377 dev_unicast_delete(lowerdev, dev->dev_addr);
379 macvlan_hash_change_addr(vlan, addr->sa_data);
381 return 0;
384 static void macvlan_change_rx_flags(struct net_device *dev, int change)
386 struct macvlan_dev *vlan = netdev_priv(dev);
387 struct net_device *lowerdev = vlan->lowerdev;
389 if (change & IFF_ALLMULTI)
390 dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
393 static void macvlan_set_multicast_list(struct net_device *dev)
395 struct macvlan_dev *vlan = netdev_priv(dev);
397 dev_mc_sync(vlan->lowerdev, dev);
400 static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
402 struct macvlan_dev *vlan = netdev_priv(dev);
404 if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
405 return -EINVAL;
406 dev->mtu = new_mtu;
407 return 0;
411 * macvlan network devices have devices nesting below it and are a special
412 * "super class" of normal network devices; split their locks off into a
413 * separate class since they always nest.
415 static struct lock_class_key macvlan_netdev_xmit_lock_key;
416 static struct lock_class_key macvlan_netdev_addr_lock_key;
418 #define MACVLAN_FEATURES \
419 (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
420 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
421 NETIF_F_TSO_ECN | NETIF_F_TSO6)
423 #define MACVLAN_STATE_MASK \
424 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
426 static void macvlan_set_lockdep_class_one(struct net_device *dev,
427 struct netdev_queue *txq,
428 void *_unused)
430 lockdep_set_class(&txq->_xmit_lock,
431 &macvlan_netdev_xmit_lock_key);
434 static void macvlan_set_lockdep_class(struct net_device *dev)
436 lockdep_set_class(&dev->addr_list_lock,
437 &macvlan_netdev_addr_lock_key);
438 netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL);
441 static int macvlan_init(struct net_device *dev)
443 struct macvlan_dev *vlan = netdev_priv(dev);
444 const struct net_device *lowerdev = vlan->lowerdev;
446 dev->state = (dev->state & ~MACVLAN_STATE_MASK) |
447 (lowerdev->state & MACVLAN_STATE_MASK);
448 dev->features = lowerdev->features & MACVLAN_FEATURES;
449 dev->iflink = lowerdev->ifindex;
450 dev->hard_header_len = lowerdev->hard_header_len;
452 macvlan_set_lockdep_class(dev);
454 vlan->rx_stats = alloc_percpu(struct macvlan_rx_stats);
455 if (!vlan->rx_stats)
456 return -ENOMEM;
458 return 0;
461 static void macvlan_uninit(struct net_device *dev)
463 struct macvlan_dev *vlan = netdev_priv(dev);
465 free_percpu(vlan->rx_stats);
468 static struct net_device_stats *macvlan_dev_get_stats(struct net_device *dev)
470 struct net_device_stats *stats = &dev->stats;
471 struct macvlan_dev *vlan = netdev_priv(dev);
473 dev_txq_stats_fold(dev, stats);
475 if (vlan->rx_stats) {
476 struct macvlan_rx_stats *p, rx = {0};
477 int i;
479 for_each_possible_cpu(i) {
480 p = per_cpu_ptr(vlan->rx_stats, i);
481 rx.rx_packets += p->rx_packets;
482 rx.rx_bytes += p->rx_bytes;
483 rx.rx_errors += p->rx_errors;
484 rx.multicast += p->multicast;
486 stats->rx_packets = rx.rx_packets;
487 stats->rx_bytes = rx.rx_bytes;
488 stats->rx_errors = rx.rx_errors;
489 stats->rx_dropped = rx.rx_errors;
490 stats->multicast = rx.multicast;
492 return stats;
495 static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
496 struct ethtool_drvinfo *drvinfo)
498 snprintf(drvinfo->driver, 32, "macvlan");
499 snprintf(drvinfo->version, 32, "0.1");
502 static u32 macvlan_ethtool_get_rx_csum(struct net_device *dev)
504 const struct macvlan_dev *vlan = netdev_priv(dev);
505 return dev_ethtool_get_rx_csum(vlan->lowerdev);
508 static int macvlan_ethtool_get_settings(struct net_device *dev,
509 struct ethtool_cmd *cmd)
511 const struct macvlan_dev *vlan = netdev_priv(dev);
512 return dev_ethtool_get_settings(vlan->lowerdev, cmd);
515 static u32 macvlan_ethtool_get_flags(struct net_device *dev)
517 const struct macvlan_dev *vlan = netdev_priv(dev);
518 return dev_ethtool_get_flags(vlan->lowerdev);
521 static const struct ethtool_ops macvlan_ethtool_ops = {
522 .get_link = ethtool_op_get_link,
523 .get_settings = macvlan_ethtool_get_settings,
524 .get_rx_csum = macvlan_ethtool_get_rx_csum,
525 .get_drvinfo = macvlan_ethtool_get_drvinfo,
526 .get_flags = macvlan_ethtool_get_flags,
529 static const struct net_device_ops macvlan_netdev_ops = {
530 .ndo_init = macvlan_init,
531 .ndo_uninit = macvlan_uninit,
532 .ndo_open = macvlan_open,
533 .ndo_stop = macvlan_stop,
534 .ndo_start_xmit = macvlan_start_xmit,
535 .ndo_change_mtu = macvlan_change_mtu,
536 .ndo_change_rx_flags = macvlan_change_rx_flags,
537 .ndo_set_mac_address = macvlan_set_mac_address,
538 .ndo_set_multicast_list = macvlan_set_multicast_list,
539 .ndo_get_stats = macvlan_dev_get_stats,
540 .ndo_validate_addr = eth_validate_addr,
543 static void macvlan_setup(struct net_device *dev)
545 ether_setup(dev);
547 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
548 dev->netdev_ops = &macvlan_netdev_ops;
549 dev->destructor = free_netdev;
550 dev->header_ops = &macvlan_hard_header_ops,
551 dev->ethtool_ops = &macvlan_ethtool_ops;
552 dev->tx_queue_len = 0;
555 static int macvlan_port_create(struct net_device *dev)
557 struct macvlan_port *port;
558 unsigned int i;
560 if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
561 return -EINVAL;
563 port = kzalloc(sizeof(*port), GFP_KERNEL);
564 if (port == NULL)
565 return -ENOMEM;
567 port->dev = dev;
568 INIT_LIST_HEAD(&port->vlans);
569 for (i = 0; i < MACVLAN_HASH_SIZE; i++)
570 INIT_HLIST_HEAD(&port->vlan_hash[i]);
571 rcu_assign_pointer(dev->macvlan_port, port);
572 return 0;
575 static void macvlan_port_destroy(struct net_device *dev)
577 struct macvlan_port *port = dev->macvlan_port;
579 rcu_assign_pointer(dev->macvlan_port, NULL);
580 synchronize_rcu();
581 kfree(port);
584 static void macvlan_transfer_operstate(struct net_device *dev)
586 struct macvlan_dev *vlan = netdev_priv(dev);
587 const struct net_device *lowerdev = vlan->lowerdev;
589 if (lowerdev->operstate == IF_OPER_DORMANT)
590 netif_dormant_on(dev);
591 else
592 netif_dormant_off(dev);
594 if (netif_carrier_ok(lowerdev)) {
595 if (!netif_carrier_ok(dev))
596 netif_carrier_on(dev);
597 } else {
598 if (netif_carrier_ok(dev))
599 netif_carrier_off(dev);
603 static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
605 if (tb[IFLA_ADDRESS]) {
606 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
607 return -EINVAL;
608 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
609 return -EADDRNOTAVAIL;
612 if (data && data[IFLA_MACVLAN_MODE]) {
613 switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) {
614 case MACVLAN_MODE_PRIVATE:
615 case MACVLAN_MODE_VEPA:
616 case MACVLAN_MODE_BRIDGE:
617 break;
618 default:
619 return -EINVAL;
622 return 0;
625 static int macvlan_get_tx_queues(struct net *net,
626 struct nlattr *tb[],
627 unsigned int *num_tx_queues,
628 unsigned int *real_num_tx_queues)
630 struct net_device *real_dev;
632 if (!tb[IFLA_LINK])
633 return -EINVAL;
635 real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
636 if (!real_dev)
637 return -ENODEV;
639 *num_tx_queues = real_dev->num_tx_queues;
640 *real_num_tx_queues = real_dev->real_num_tx_queues;
641 return 0;
644 static int macvlan_newlink(struct net *src_net, struct net_device *dev,
645 struct nlattr *tb[], struct nlattr *data[])
647 struct macvlan_dev *vlan = netdev_priv(dev);
648 struct macvlan_port *port;
649 struct net_device *lowerdev;
650 int err;
652 if (!tb[IFLA_LINK])
653 return -EINVAL;
655 lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
656 if (lowerdev == NULL)
657 return -ENODEV;
659 /* When creating macvlans on top of other macvlans - use
660 * the real device as the lowerdev.
662 if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) {
663 struct macvlan_dev *lowervlan = netdev_priv(lowerdev);
664 lowerdev = lowervlan->lowerdev;
667 if (!tb[IFLA_MTU])
668 dev->mtu = lowerdev->mtu;
669 else if (dev->mtu > lowerdev->mtu)
670 return -EINVAL;
672 if (!tb[IFLA_ADDRESS])
673 random_ether_addr(dev->dev_addr);
675 if (lowerdev->macvlan_port == NULL) {
676 err = macvlan_port_create(lowerdev);
677 if (err < 0)
678 return err;
680 port = lowerdev->macvlan_port;
682 vlan->lowerdev = lowerdev;
683 vlan->dev = dev;
684 vlan->port = port;
686 vlan->mode = MACVLAN_MODE_VEPA;
687 if (data && data[IFLA_MACVLAN_MODE])
688 vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
690 err = register_netdevice(dev);
691 if (err < 0)
692 return err;
694 list_add_tail(&vlan->list, &port->vlans);
695 macvlan_transfer_operstate(dev);
696 return 0;
699 static void macvlan_dellink(struct net_device *dev, struct list_head *head)
701 struct macvlan_dev *vlan = netdev_priv(dev);
702 struct macvlan_port *port = vlan->port;
704 list_del(&vlan->list);
705 unregister_netdevice_queue(dev, head);
707 if (list_empty(&port->vlans))
708 macvlan_port_destroy(port->dev);
711 static int macvlan_changelink(struct net_device *dev,
712 struct nlattr *tb[], struct nlattr *data[])
714 struct macvlan_dev *vlan = netdev_priv(dev);
715 if (data && data[IFLA_MACVLAN_MODE])
716 vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
717 return 0;
720 static size_t macvlan_get_size(const struct net_device *dev)
722 return nla_total_size(4);
725 static int macvlan_fill_info(struct sk_buff *skb,
726 const struct net_device *dev)
728 struct macvlan_dev *vlan = netdev_priv(dev);
730 NLA_PUT_U32(skb, IFLA_MACVLAN_MODE, vlan->mode);
731 return 0;
733 nla_put_failure:
734 return -EMSGSIZE;
737 static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = {
738 [IFLA_MACVLAN_MODE] = { .type = NLA_U32 },
741 static struct rtnl_link_ops macvlan_link_ops __read_mostly = {
742 .kind = "macvlan",
743 .priv_size = sizeof(struct macvlan_dev),
744 .get_tx_queues = macvlan_get_tx_queues,
745 .setup = macvlan_setup,
746 .validate = macvlan_validate,
747 .newlink = macvlan_newlink,
748 .dellink = macvlan_dellink,
749 .maxtype = IFLA_MACVLAN_MAX,
750 .policy = macvlan_policy,
751 .changelink = macvlan_changelink,
752 .get_size = macvlan_get_size,
753 .fill_info = macvlan_fill_info,
756 static int macvlan_device_event(struct notifier_block *unused,
757 unsigned long event, void *ptr)
759 struct net_device *dev = ptr;
760 struct macvlan_dev *vlan, *next;
761 struct macvlan_port *port;
763 port = dev->macvlan_port;
764 if (port == NULL)
765 return NOTIFY_DONE;
767 switch (event) {
768 case NETDEV_CHANGE:
769 list_for_each_entry(vlan, &port->vlans, list)
770 macvlan_transfer_operstate(vlan->dev);
771 break;
772 case NETDEV_FEAT_CHANGE:
773 list_for_each_entry(vlan, &port->vlans, list) {
774 vlan->dev->features = dev->features & MACVLAN_FEATURES;
775 netdev_features_change(vlan->dev);
777 break;
778 case NETDEV_UNREGISTER:
779 list_for_each_entry_safe(vlan, next, &port->vlans, list)
780 macvlan_dellink(vlan->dev, NULL);
781 break;
783 return NOTIFY_DONE;
786 static struct notifier_block macvlan_notifier_block __read_mostly = {
787 .notifier_call = macvlan_device_event,
790 static int __init macvlan_init_module(void)
792 int err;
794 register_netdevice_notifier(&macvlan_notifier_block);
795 macvlan_handle_frame_hook = macvlan_handle_frame;
797 err = rtnl_link_register(&macvlan_link_ops);
798 if (err < 0)
799 goto err1;
800 return 0;
801 err1:
802 macvlan_handle_frame_hook = NULL;
803 unregister_netdevice_notifier(&macvlan_notifier_block);
804 return err;
807 static void __exit macvlan_cleanup_module(void)
809 rtnl_link_unregister(&macvlan_link_ops);
810 macvlan_handle_frame_hook = NULL;
811 unregister_netdevice_notifier(&macvlan_notifier_block);
814 module_init(macvlan_init_module);
815 module_exit(macvlan_cleanup_module);
817 MODULE_LICENSE("GPL");
818 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
819 MODULE_DESCRIPTION("Driver for MAC address based VLANs");
820 MODULE_ALIAS_RTNL_LINK("macvlan");