TOMOYO: Use callback for updating entries.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / macvlan.c
blobf15fe2cf72ae1261fe6d0cde6ca263c2d0c54809
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 static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
43 const unsigned char *addr)
45 struct macvlan_dev *vlan;
46 struct hlist_node *n;
48 hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[addr[5]], hlist) {
49 if (!compare_ether_addr_64bits(vlan->dev->dev_addr, addr))
50 return vlan;
52 return NULL;
55 static void macvlan_hash_add(struct macvlan_dev *vlan)
57 struct macvlan_port *port = vlan->port;
58 const unsigned char *addr = vlan->dev->dev_addr;
60 hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]);
63 static void macvlan_hash_del(struct macvlan_dev *vlan)
65 hlist_del_rcu(&vlan->hlist);
66 synchronize_rcu();
69 static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
70 const unsigned char *addr)
72 macvlan_hash_del(vlan);
73 /* Now that we are unhashed it is safe to change the device
74 * address without confusing packet delivery.
76 memcpy(vlan->dev->dev_addr, addr, ETH_ALEN);
77 macvlan_hash_add(vlan);
80 static int macvlan_addr_busy(const struct macvlan_port *port,
81 const unsigned char *addr)
83 /* Test to see if the specified multicast address is
84 * currently in use by the underlying device or
85 * another macvlan.
87 if (!compare_ether_addr_64bits(port->dev->dev_addr, addr))
88 return 1;
90 if (macvlan_hash_lookup(port, addr))
91 return 1;
93 return 0;
97 static int macvlan_broadcast_one(struct sk_buff *skb,
98 const struct macvlan_dev *vlan,
99 const struct ethhdr *eth, bool local)
101 struct net_device *dev = vlan->dev;
102 if (!skb)
103 return NET_RX_DROP;
105 if (local)
106 return vlan->forward(dev, skb);
108 skb->dev = dev;
109 if (!compare_ether_addr_64bits(eth->h_dest,
110 dev->broadcast))
111 skb->pkt_type = PACKET_BROADCAST;
112 else
113 skb->pkt_type = PACKET_MULTICAST;
115 return vlan->receive(skb);
118 static void macvlan_broadcast(struct sk_buff *skb,
119 const struct macvlan_port *port,
120 struct net_device *src,
121 enum macvlan_mode mode)
123 const struct ethhdr *eth = eth_hdr(skb);
124 const struct macvlan_dev *vlan;
125 struct hlist_node *n;
126 struct sk_buff *nskb;
127 unsigned int i;
128 int err;
130 if (skb->protocol == htons(ETH_P_PAUSE))
131 return;
133 for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
134 hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[i], hlist) {
135 if (vlan->dev == src || !(vlan->mode & mode))
136 continue;
138 nskb = skb_clone(skb, GFP_ATOMIC);
139 err = macvlan_broadcast_one(nskb, vlan, eth,
140 mode == MACVLAN_MODE_BRIDGE);
141 macvlan_count_rx(vlan, skb->len + ETH_HLEN,
142 err == NET_RX_SUCCESS, 1);
147 /* called under rcu_read_lock() from netif_receive_skb */
148 static struct sk_buff *macvlan_handle_frame(struct macvlan_port *port,
149 struct sk_buff *skb)
151 const struct ethhdr *eth = eth_hdr(skb);
152 const struct macvlan_dev *vlan;
153 const struct macvlan_dev *src;
154 struct net_device *dev;
155 unsigned int len;
157 if (is_multicast_ether_addr(eth->h_dest)) {
158 src = macvlan_hash_lookup(port, eth->h_source);
159 if (!src)
160 /* frame comes from an external address */
161 macvlan_broadcast(skb, port, NULL,
162 MACVLAN_MODE_PRIVATE |
163 MACVLAN_MODE_VEPA |
164 MACVLAN_MODE_BRIDGE);
165 else if (src->mode == MACVLAN_MODE_VEPA)
166 /* flood to everyone except source */
167 macvlan_broadcast(skb, port, src->dev,
168 MACVLAN_MODE_VEPA |
169 MACVLAN_MODE_BRIDGE);
170 else if (src->mode == MACVLAN_MODE_BRIDGE)
172 * flood only to VEPA ports, bridge ports
173 * already saw the frame on the way out.
175 macvlan_broadcast(skb, port, src->dev,
176 MACVLAN_MODE_VEPA);
177 return skb;
180 vlan = macvlan_hash_lookup(port, eth->h_dest);
181 if (vlan == NULL)
182 return skb;
184 dev = vlan->dev;
185 if (unlikely(!(dev->flags & IFF_UP))) {
186 kfree_skb(skb);
187 return NULL;
189 len = skb->len + ETH_HLEN;
190 skb = skb_share_check(skb, GFP_ATOMIC);
191 macvlan_count_rx(vlan, len, skb != NULL, 0);
192 if (!skb)
193 return NULL;
195 skb->dev = dev;
196 skb->pkt_type = PACKET_HOST;
198 vlan->receive(skb);
199 return NULL;
202 static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
204 const struct macvlan_dev *vlan = netdev_priv(dev);
205 const struct macvlan_port *port = vlan->port;
206 const struct macvlan_dev *dest;
208 if (vlan->mode == MACVLAN_MODE_BRIDGE) {
209 const struct ethhdr *eth = (void *)skb->data;
211 /* send to other bridge ports directly */
212 if (is_multicast_ether_addr(eth->h_dest)) {
213 macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE);
214 goto xmit_world;
217 dest = macvlan_hash_lookup(port, eth->h_dest);
218 if (dest && dest->mode == MACVLAN_MODE_BRIDGE) {
219 unsigned int length = skb->len + ETH_HLEN;
220 int ret = dest->forward(dest->dev, skb);
221 macvlan_count_rx(dest, length,
222 ret == NET_RX_SUCCESS, 0);
224 return NET_XMIT_SUCCESS;
228 xmit_world:
229 skb_set_dev(skb, vlan->lowerdev);
230 return dev_queue_xmit(skb);
233 netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
234 struct net_device *dev)
236 int i = skb_get_queue_mapping(skb);
237 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
238 unsigned int len = skb->len;
239 int ret;
241 ret = macvlan_queue_xmit(skb, dev);
242 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
243 txq->tx_packets++;
244 txq->tx_bytes += len;
245 } else
246 txq->tx_dropped++;
248 return ret;
250 EXPORT_SYMBOL_GPL(macvlan_start_xmit);
252 static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
253 unsigned short type, const void *daddr,
254 const void *saddr, unsigned len)
256 const struct macvlan_dev *vlan = netdev_priv(dev);
257 struct net_device *lowerdev = vlan->lowerdev;
259 return dev_hard_header(skb, lowerdev, type, daddr,
260 saddr ? : dev->dev_addr, len);
263 static const struct header_ops macvlan_hard_header_ops = {
264 .create = macvlan_hard_header,
265 .rebuild = eth_rebuild_header,
266 .parse = eth_header_parse,
267 .cache = eth_header_cache,
268 .cache_update = eth_header_cache_update,
271 static int macvlan_open(struct net_device *dev)
273 struct macvlan_dev *vlan = netdev_priv(dev);
274 struct net_device *lowerdev = vlan->lowerdev;
275 int err;
277 err = -EBUSY;
278 if (macvlan_addr_busy(vlan->port, dev->dev_addr))
279 goto out;
281 err = dev_uc_add(lowerdev, dev->dev_addr);
282 if (err < 0)
283 goto out;
284 if (dev->flags & IFF_ALLMULTI) {
285 err = dev_set_allmulti(lowerdev, 1);
286 if (err < 0)
287 goto del_unicast;
289 macvlan_hash_add(vlan);
290 return 0;
292 del_unicast:
293 dev_uc_del(lowerdev, dev->dev_addr);
294 out:
295 return err;
298 static int macvlan_stop(struct net_device *dev)
300 struct macvlan_dev *vlan = netdev_priv(dev);
301 struct net_device *lowerdev = vlan->lowerdev;
303 dev_mc_unsync(lowerdev, dev);
304 if (dev->flags & IFF_ALLMULTI)
305 dev_set_allmulti(lowerdev, -1);
307 dev_uc_del(lowerdev, dev->dev_addr);
309 macvlan_hash_del(vlan);
310 return 0;
313 static int macvlan_set_mac_address(struct net_device *dev, void *p)
315 struct macvlan_dev *vlan = netdev_priv(dev);
316 struct net_device *lowerdev = vlan->lowerdev;
317 struct sockaddr *addr = p;
318 int err;
320 if (!is_valid_ether_addr(addr->sa_data))
321 return -EADDRNOTAVAIL;
323 if (!(dev->flags & IFF_UP)) {
324 /* Just copy in the new address */
325 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
326 } else {
327 /* Rehash and update the device filters */
328 if (macvlan_addr_busy(vlan->port, addr->sa_data))
329 return -EBUSY;
331 err = dev_uc_add(lowerdev, addr->sa_data);
332 if (err)
333 return err;
335 dev_uc_del(lowerdev, dev->dev_addr);
337 macvlan_hash_change_addr(vlan, addr->sa_data);
339 return 0;
342 static void macvlan_change_rx_flags(struct net_device *dev, int change)
344 struct macvlan_dev *vlan = netdev_priv(dev);
345 struct net_device *lowerdev = vlan->lowerdev;
347 if (change & IFF_ALLMULTI)
348 dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
351 static void macvlan_set_multicast_list(struct net_device *dev)
353 struct macvlan_dev *vlan = netdev_priv(dev);
355 dev_mc_sync(vlan->lowerdev, dev);
358 static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
360 struct macvlan_dev *vlan = netdev_priv(dev);
362 if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
363 return -EINVAL;
364 dev->mtu = new_mtu;
365 return 0;
369 * macvlan network devices have devices nesting below it and are a special
370 * "super class" of normal network devices; split their locks off into a
371 * separate class since they always nest.
373 static struct lock_class_key macvlan_netdev_xmit_lock_key;
374 static struct lock_class_key macvlan_netdev_addr_lock_key;
376 #define MACVLAN_FEATURES \
377 (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
378 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
379 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO)
381 #define MACVLAN_STATE_MASK \
382 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
384 static void macvlan_set_lockdep_class_one(struct net_device *dev,
385 struct netdev_queue *txq,
386 void *_unused)
388 lockdep_set_class(&txq->_xmit_lock,
389 &macvlan_netdev_xmit_lock_key);
392 static void macvlan_set_lockdep_class(struct net_device *dev)
394 lockdep_set_class(&dev->addr_list_lock,
395 &macvlan_netdev_addr_lock_key);
396 netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL);
399 static int macvlan_init(struct net_device *dev)
401 struct macvlan_dev *vlan = netdev_priv(dev);
402 const struct net_device *lowerdev = vlan->lowerdev;
404 dev->state = (dev->state & ~MACVLAN_STATE_MASK) |
405 (lowerdev->state & MACVLAN_STATE_MASK);
406 dev->features = lowerdev->features & MACVLAN_FEATURES;
407 dev->gso_max_size = lowerdev->gso_max_size;
408 dev->iflink = lowerdev->ifindex;
409 dev->hard_header_len = lowerdev->hard_header_len;
411 macvlan_set_lockdep_class(dev);
413 vlan->rx_stats = alloc_percpu(struct macvlan_rx_stats);
414 if (!vlan->rx_stats)
415 return -ENOMEM;
417 return 0;
420 static void macvlan_uninit(struct net_device *dev)
422 struct macvlan_dev *vlan = netdev_priv(dev);
424 free_percpu(vlan->rx_stats);
427 static struct net_device_stats *macvlan_dev_get_stats(struct net_device *dev)
429 struct net_device_stats *stats = &dev->stats;
430 struct macvlan_dev *vlan = netdev_priv(dev);
432 dev_txq_stats_fold(dev, stats);
434 if (vlan->rx_stats) {
435 struct macvlan_rx_stats *p, rx = {0};
436 int i;
438 for_each_possible_cpu(i) {
439 p = per_cpu_ptr(vlan->rx_stats, i);
440 rx.rx_packets += p->rx_packets;
441 rx.rx_bytes += p->rx_bytes;
442 rx.rx_errors += p->rx_errors;
443 rx.multicast += p->multicast;
445 stats->rx_packets = rx.rx_packets;
446 stats->rx_bytes = rx.rx_bytes;
447 stats->rx_errors = rx.rx_errors;
448 stats->rx_dropped = rx.rx_errors;
449 stats->multicast = rx.multicast;
451 return stats;
454 static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
455 struct ethtool_drvinfo *drvinfo)
457 snprintf(drvinfo->driver, 32, "macvlan");
458 snprintf(drvinfo->version, 32, "0.1");
461 static u32 macvlan_ethtool_get_rx_csum(struct net_device *dev)
463 const struct macvlan_dev *vlan = netdev_priv(dev);
464 return dev_ethtool_get_rx_csum(vlan->lowerdev);
467 static int macvlan_ethtool_get_settings(struct net_device *dev,
468 struct ethtool_cmd *cmd)
470 const struct macvlan_dev *vlan = netdev_priv(dev);
471 return dev_ethtool_get_settings(vlan->lowerdev, cmd);
474 static u32 macvlan_ethtool_get_flags(struct net_device *dev)
476 const struct macvlan_dev *vlan = netdev_priv(dev);
477 return dev_ethtool_get_flags(vlan->lowerdev);
480 static const struct ethtool_ops macvlan_ethtool_ops = {
481 .get_link = ethtool_op_get_link,
482 .get_settings = macvlan_ethtool_get_settings,
483 .get_rx_csum = macvlan_ethtool_get_rx_csum,
484 .get_drvinfo = macvlan_ethtool_get_drvinfo,
485 .get_flags = macvlan_ethtool_get_flags,
488 static const struct net_device_ops macvlan_netdev_ops = {
489 .ndo_init = macvlan_init,
490 .ndo_uninit = macvlan_uninit,
491 .ndo_open = macvlan_open,
492 .ndo_stop = macvlan_stop,
493 .ndo_start_xmit = macvlan_start_xmit,
494 .ndo_change_mtu = macvlan_change_mtu,
495 .ndo_change_rx_flags = macvlan_change_rx_flags,
496 .ndo_set_mac_address = macvlan_set_mac_address,
497 .ndo_set_multicast_list = macvlan_set_multicast_list,
498 .ndo_get_stats = macvlan_dev_get_stats,
499 .ndo_validate_addr = eth_validate_addr,
502 void macvlan_common_setup(struct net_device *dev)
504 ether_setup(dev);
506 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
507 dev->netdev_ops = &macvlan_netdev_ops;
508 dev->destructor = free_netdev;
509 dev->header_ops = &macvlan_hard_header_ops,
510 dev->ethtool_ops = &macvlan_ethtool_ops;
512 EXPORT_SYMBOL_GPL(macvlan_common_setup);
514 static void macvlan_setup(struct net_device *dev)
516 macvlan_common_setup(dev);
517 dev->tx_queue_len = 0;
520 static int macvlan_port_create(struct net_device *dev)
522 struct macvlan_port *port;
523 unsigned int i;
525 if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
526 return -EINVAL;
528 port = kzalloc(sizeof(*port), GFP_KERNEL);
529 if (port == NULL)
530 return -ENOMEM;
532 port->dev = dev;
533 INIT_LIST_HEAD(&port->vlans);
534 for (i = 0; i < MACVLAN_HASH_SIZE; i++)
535 INIT_HLIST_HEAD(&port->vlan_hash[i]);
536 rcu_assign_pointer(dev->macvlan_port, port);
537 return 0;
540 static void macvlan_port_destroy(struct net_device *dev)
542 struct macvlan_port *port = dev->macvlan_port;
544 rcu_assign_pointer(dev->macvlan_port, NULL);
545 synchronize_rcu();
546 kfree(port);
549 static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
551 if (tb[IFLA_ADDRESS]) {
552 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
553 return -EINVAL;
554 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
555 return -EADDRNOTAVAIL;
558 if (data && data[IFLA_MACVLAN_MODE]) {
559 switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) {
560 case MACVLAN_MODE_PRIVATE:
561 case MACVLAN_MODE_VEPA:
562 case MACVLAN_MODE_BRIDGE:
563 break;
564 default:
565 return -EINVAL;
568 return 0;
571 static int macvlan_get_tx_queues(struct net *net,
572 struct nlattr *tb[],
573 unsigned int *num_tx_queues,
574 unsigned int *real_num_tx_queues)
576 struct net_device *real_dev;
578 if (!tb[IFLA_LINK])
579 return -EINVAL;
581 real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
582 if (!real_dev)
583 return -ENODEV;
585 *num_tx_queues = real_dev->num_tx_queues;
586 *real_num_tx_queues = real_dev->real_num_tx_queues;
587 return 0;
590 int macvlan_common_newlink(struct net *src_net, struct net_device *dev,
591 struct nlattr *tb[], struct nlattr *data[],
592 int (*receive)(struct sk_buff *skb),
593 int (*forward)(struct net_device *dev,
594 struct sk_buff *skb))
596 struct macvlan_dev *vlan = netdev_priv(dev);
597 struct macvlan_port *port;
598 struct net_device *lowerdev;
599 int err;
601 if (!tb[IFLA_LINK])
602 return -EINVAL;
604 lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
605 if (lowerdev == NULL)
606 return -ENODEV;
608 /* When creating macvlans on top of other macvlans - use
609 * the real device as the lowerdev.
611 if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) {
612 struct macvlan_dev *lowervlan = netdev_priv(lowerdev);
613 lowerdev = lowervlan->lowerdev;
616 if (!tb[IFLA_MTU])
617 dev->mtu = lowerdev->mtu;
618 else if (dev->mtu > lowerdev->mtu)
619 return -EINVAL;
621 if (!tb[IFLA_ADDRESS])
622 random_ether_addr(dev->dev_addr);
624 if (lowerdev->macvlan_port == NULL) {
625 err = macvlan_port_create(lowerdev);
626 if (err < 0)
627 return err;
629 port = lowerdev->macvlan_port;
631 vlan->lowerdev = lowerdev;
632 vlan->dev = dev;
633 vlan->port = port;
634 vlan->receive = receive;
635 vlan->forward = forward;
637 vlan->mode = MACVLAN_MODE_VEPA;
638 if (data && data[IFLA_MACVLAN_MODE])
639 vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
641 err = register_netdevice(dev);
642 if (err < 0)
643 goto destroy_port;
645 list_add_tail(&vlan->list, &port->vlans);
646 netif_stacked_transfer_operstate(lowerdev, dev);
648 return 0;
650 destroy_port:
651 if (list_empty(&port->vlans))
652 macvlan_port_destroy(lowerdev);
654 return err;
656 EXPORT_SYMBOL_GPL(macvlan_common_newlink);
658 static int macvlan_newlink(struct net *src_net, struct net_device *dev,
659 struct nlattr *tb[], struct nlattr *data[])
661 return macvlan_common_newlink(src_net, dev, tb, data,
662 netif_rx,
663 dev_forward_skb);
666 void macvlan_dellink(struct net_device *dev, struct list_head *head)
668 struct macvlan_dev *vlan = netdev_priv(dev);
669 struct macvlan_port *port = vlan->port;
671 list_del(&vlan->list);
672 unregister_netdevice_queue(dev, head);
674 if (list_empty(&port->vlans))
675 macvlan_port_destroy(port->dev);
677 EXPORT_SYMBOL_GPL(macvlan_dellink);
679 static int macvlan_changelink(struct net_device *dev,
680 struct nlattr *tb[], struct nlattr *data[])
682 struct macvlan_dev *vlan = netdev_priv(dev);
683 if (data && data[IFLA_MACVLAN_MODE])
684 vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
685 return 0;
688 static size_t macvlan_get_size(const struct net_device *dev)
690 return nla_total_size(4);
693 static int macvlan_fill_info(struct sk_buff *skb,
694 const struct net_device *dev)
696 struct macvlan_dev *vlan = netdev_priv(dev);
698 NLA_PUT_U32(skb, IFLA_MACVLAN_MODE, vlan->mode);
699 return 0;
701 nla_put_failure:
702 return -EMSGSIZE;
705 static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = {
706 [IFLA_MACVLAN_MODE] = { .type = NLA_U32 },
709 int macvlan_link_register(struct rtnl_link_ops *ops)
711 /* common fields */
712 ops->priv_size = sizeof(struct macvlan_dev);
713 ops->get_tx_queues = macvlan_get_tx_queues;
714 ops->validate = macvlan_validate;
715 ops->maxtype = IFLA_MACVLAN_MAX;
716 ops->policy = macvlan_policy;
717 ops->changelink = macvlan_changelink;
718 ops->get_size = macvlan_get_size;
719 ops->fill_info = macvlan_fill_info;
721 return rtnl_link_register(ops);
723 EXPORT_SYMBOL_GPL(macvlan_link_register);
725 static struct rtnl_link_ops macvlan_link_ops = {
726 .kind = "macvlan",
727 .setup = macvlan_setup,
728 .newlink = macvlan_newlink,
729 .dellink = macvlan_dellink,
732 static int macvlan_device_event(struct notifier_block *unused,
733 unsigned long event, void *ptr)
735 struct net_device *dev = ptr;
736 struct macvlan_dev *vlan, *next;
737 struct macvlan_port *port;
739 port = dev->macvlan_port;
740 if (port == NULL)
741 return NOTIFY_DONE;
743 switch (event) {
744 case NETDEV_CHANGE:
745 list_for_each_entry(vlan, &port->vlans, list)
746 netif_stacked_transfer_operstate(vlan->lowerdev,
747 vlan->dev);
748 break;
749 case NETDEV_FEAT_CHANGE:
750 list_for_each_entry(vlan, &port->vlans, list) {
751 vlan->dev->features = dev->features & MACVLAN_FEATURES;
752 vlan->dev->gso_max_size = dev->gso_max_size;
753 netdev_features_change(vlan->dev);
755 break;
756 case NETDEV_UNREGISTER:
757 list_for_each_entry_safe(vlan, next, &port->vlans, list)
758 vlan->dev->rtnl_link_ops->dellink(vlan->dev, NULL);
759 break;
760 case NETDEV_PRE_TYPE_CHANGE:
761 /* Forbid underlaying device to change its type. */
762 return NOTIFY_BAD;
764 return NOTIFY_DONE;
767 static struct notifier_block macvlan_notifier_block __read_mostly = {
768 .notifier_call = macvlan_device_event,
771 static int __init macvlan_init_module(void)
773 int err;
775 register_netdevice_notifier(&macvlan_notifier_block);
776 macvlan_handle_frame_hook = macvlan_handle_frame;
778 err = macvlan_link_register(&macvlan_link_ops);
779 if (err < 0)
780 goto err1;
781 return 0;
782 err1:
783 macvlan_handle_frame_hook = NULL;
784 unregister_netdevice_notifier(&macvlan_notifier_block);
785 return err;
788 static void __exit macvlan_cleanup_module(void)
790 rtnl_link_unregister(&macvlan_link_ops);
791 macvlan_handle_frame_hook = NULL;
792 unregister_netdevice_notifier(&macvlan_notifier_block);
795 module_init(macvlan_init_module);
796 module_exit(macvlan_cleanup_module);
798 MODULE_LICENSE("GPL");
799 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
800 MODULE_DESCRIPTION("Driver for MAC address based VLANs");
801 MODULE_ALIAS_RTNL_LINK("macvlan");