NET: cdc-phonet, handle empty phonet header
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / macvlan.c
blob21a9c9ab4b342d6eff468ea8ff4232426de57b49
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->gso_max_size = lowerdev->gso_max_size;
450 dev->iflink = lowerdev->ifindex;
451 dev->hard_header_len = lowerdev->hard_header_len;
453 macvlan_set_lockdep_class(dev);
455 vlan->rx_stats = alloc_percpu(struct macvlan_rx_stats);
456 if (!vlan->rx_stats)
457 return -ENOMEM;
459 return 0;
462 static void macvlan_uninit(struct net_device *dev)
464 struct macvlan_dev *vlan = netdev_priv(dev);
466 free_percpu(vlan->rx_stats);
469 static struct net_device_stats *macvlan_dev_get_stats(struct net_device *dev)
471 struct net_device_stats *stats = &dev->stats;
472 struct macvlan_dev *vlan = netdev_priv(dev);
474 dev_txq_stats_fold(dev, stats);
476 if (vlan->rx_stats) {
477 struct macvlan_rx_stats *p, rx = {0};
478 int i;
480 for_each_possible_cpu(i) {
481 p = per_cpu_ptr(vlan->rx_stats, i);
482 rx.rx_packets += p->rx_packets;
483 rx.rx_bytes += p->rx_bytes;
484 rx.rx_errors += p->rx_errors;
485 rx.multicast += p->multicast;
487 stats->rx_packets = rx.rx_packets;
488 stats->rx_bytes = rx.rx_bytes;
489 stats->rx_errors = rx.rx_errors;
490 stats->rx_dropped = rx.rx_errors;
491 stats->multicast = rx.multicast;
493 return stats;
496 static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
497 struct ethtool_drvinfo *drvinfo)
499 snprintf(drvinfo->driver, 32, "macvlan");
500 snprintf(drvinfo->version, 32, "0.1");
503 static u32 macvlan_ethtool_get_rx_csum(struct net_device *dev)
505 const struct macvlan_dev *vlan = netdev_priv(dev);
506 return dev_ethtool_get_rx_csum(vlan->lowerdev);
509 static int macvlan_ethtool_get_settings(struct net_device *dev,
510 struct ethtool_cmd *cmd)
512 const struct macvlan_dev *vlan = netdev_priv(dev);
513 return dev_ethtool_get_settings(vlan->lowerdev, cmd);
516 static u32 macvlan_ethtool_get_flags(struct net_device *dev)
518 const struct macvlan_dev *vlan = netdev_priv(dev);
519 return dev_ethtool_get_flags(vlan->lowerdev);
522 static const struct ethtool_ops macvlan_ethtool_ops = {
523 .get_link = ethtool_op_get_link,
524 .get_settings = macvlan_ethtool_get_settings,
525 .get_rx_csum = macvlan_ethtool_get_rx_csum,
526 .get_drvinfo = macvlan_ethtool_get_drvinfo,
527 .get_flags = macvlan_ethtool_get_flags,
530 static const struct net_device_ops macvlan_netdev_ops = {
531 .ndo_init = macvlan_init,
532 .ndo_uninit = macvlan_uninit,
533 .ndo_open = macvlan_open,
534 .ndo_stop = macvlan_stop,
535 .ndo_start_xmit = macvlan_start_xmit,
536 .ndo_change_mtu = macvlan_change_mtu,
537 .ndo_change_rx_flags = macvlan_change_rx_flags,
538 .ndo_set_mac_address = macvlan_set_mac_address,
539 .ndo_set_multicast_list = macvlan_set_multicast_list,
540 .ndo_get_stats = macvlan_dev_get_stats,
541 .ndo_validate_addr = eth_validate_addr,
544 static void macvlan_setup(struct net_device *dev)
546 ether_setup(dev);
548 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
549 dev->netdev_ops = &macvlan_netdev_ops;
550 dev->destructor = free_netdev;
551 dev->header_ops = &macvlan_hard_header_ops,
552 dev->ethtool_ops = &macvlan_ethtool_ops;
553 dev->tx_queue_len = 0;
556 static int macvlan_port_create(struct net_device *dev)
558 struct macvlan_port *port;
559 unsigned int i;
561 if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
562 return -EINVAL;
564 port = kzalloc(sizeof(*port), GFP_KERNEL);
565 if (port == NULL)
566 return -ENOMEM;
568 port->dev = dev;
569 INIT_LIST_HEAD(&port->vlans);
570 for (i = 0; i < MACVLAN_HASH_SIZE; i++)
571 INIT_HLIST_HEAD(&port->vlan_hash[i]);
572 rcu_assign_pointer(dev->macvlan_port, port);
573 return 0;
576 static void macvlan_port_destroy(struct net_device *dev)
578 struct macvlan_port *port = dev->macvlan_port;
580 rcu_assign_pointer(dev->macvlan_port, NULL);
581 synchronize_rcu();
582 kfree(port);
585 static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
587 if (tb[IFLA_ADDRESS]) {
588 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
589 return -EINVAL;
590 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
591 return -EADDRNOTAVAIL;
594 if (data && data[IFLA_MACVLAN_MODE]) {
595 switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) {
596 case MACVLAN_MODE_PRIVATE:
597 case MACVLAN_MODE_VEPA:
598 case MACVLAN_MODE_BRIDGE:
599 break;
600 default:
601 return -EINVAL;
604 return 0;
607 static int macvlan_get_tx_queues(struct net *net,
608 struct nlattr *tb[],
609 unsigned int *num_tx_queues,
610 unsigned int *real_num_tx_queues)
612 struct net_device *real_dev;
614 if (!tb[IFLA_LINK])
615 return -EINVAL;
617 real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
618 if (!real_dev)
619 return -ENODEV;
621 *num_tx_queues = real_dev->num_tx_queues;
622 *real_num_tx_queues = real_dev->real_num_tx_queues;
623 return 0;
626 static int macvlan_newlink(struct net *src_net, struct net_device *dev,
627 struct nlattr *tb[], struct nlattr *data[])
629 struct macvlan_dev *vlan = netdev_priv(dev);
630 struct macvlan_port *port;
631 struct net_device *lowerdev;
632 int err;
634 if (!tb[IFLA_LINK])
635 return -EINVAL;
637 lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
638 if (lowerdev == NULL)
639 return -ENODEV;
641 /* When creating macvlans on top of other macvlans - use
642 * the real device as the lowerdev.
644 if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) {
645 struct macvlan_dev *lowervlan = netdev_priv(lowerdev);
646 lowerdev = lowervlan->lowerdev;
649 if (!tb[IFLA_MTU])
650 dev->mtu = lowerdev->mtu;
651 else if (dev->mtu > lowerdev->mtu)
652 return -EINVAL;
654 if (!tb[IFLA_ADDRESS])
655 random_ether_addr(dev->dev_addr);
657 if (lowerdev->macvlan_port == NULL) {
658 err = macvlan_port_create(lowerdev);
659 if (err < 0)
660 return err;
662 port = lowerdev->macvlan_port;
664 vlan->lowerdev = lowerdev;
665 vlan->dev = dev;
666 vlan->port = port;
668 vlan->mode = MACVLAN_MODE_VEPA;
669 if (data && data[IFLA_MACVLAN_MODE])
670 vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
672 err = register_netdevice(dev);
673 if (err < 0)
674 return err;
676 list_add_tail(&vlan->list, &port->vlans);
677 netif_stacked_transfer_operstate(lowerdev, dev);
678 return 0;
681 static void macvlan_dellink(struct net_device *dev, struct list_head *head)
683 struct macvlan_dev *vlan = netdev_priv(dev);
684 struct macvlan_port *port = vlan->port;
686 list_del(&vlan->list);
687 unregister_netdevice_queue(dev, head);
689 if (list_empty(&port->vlans))
690 macvlan_port_destroy(port->dev);
693 static int macvlan_changelink(struct net_device *dev,
694 struct nlattr *tb[], struct nlattr *data[])
696 struct macvlan_dev *vlan = netdev_priv(dev);
697 if (data && data[IFLA_MACVLAN_MODE])
698 vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
699 return 0;
702 static size_t macvlan_get_size(const struct net_device *dev)
704 return nla_total_size(4);
707 static int macvlan_fill_info(struct sk_buff *skb,
708 const struct net_device *dev)
710 struct macvlan_dev *vlan = netdev_priv(dev);
712 NLA_PUT_U32(skb, IFLA_MACVLAN_MODE, vlan->mode);
713 return 0;
715 nla_put_failure:
716 return -EMSGSIZE;
719 static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = {
720 [IFLA_MACVLAN_MODE] = { .type = NLA_U32 },
723 static struct rtnl_link_ops macvlan_link_ops __read_mostly = {
724 .kind = "macvlan",
725 .priv_size = sizeof(struct macvlan_dev),
726 .get_tx_queues = macvlan_get_tx_queues,
727 .setup = macvlan_setup,
728 .validate = macvlan_validate,
729 .newlink = macvlan_newlink,
730 .dellink = macvlan_dellink,
731 .maxtype = IFLA_MACVLAN_MAX,
732 .policy = macvlan_policy,
733 .changelink = macvlan_changelink,
734 .get_size = macvlan_get_size,
735 .fill_info = macvlan_fill_info,
738 static int macvlan_device_event(struct notifier_block *unused,
739 unsigned long event, void *ptr)
741 struct net_device *dev = ptr;
742 struct macvlan_dev *vlan, *next;
743 struct macvlan_port *port;
745 port = dev->macvlan_port;
746 if (port == NULL)
747 return NOTIFY_DONE;
749 switch (event) {
750 case NETDEV_CHANGE:
751 list_for_each_entry(vlan, &port->vlans, list)
752 netif_stacked_transfer_operstate(vlan->lowerdev,
753 vlan->dev);
754 break;
755 case NETDEV_FEAT_CHANGE:
756 list_for_each_entry(vlan, &port->vlans, list) {
757 vlan->dev->features = dev->features & MACVLAN_FEATURES;
758 vlan->dev->gso_max_size = dev->gso_max_size;
759 netdev_features_change(vlan->dev);
761 break;
762 case NETDEV_UNREGISTER:
763 list_for_each_entry_safe(vlan, next, &port->vlans, list)
764 macvlan_dellink(vlan->dev, NULL);
765 break;
767 return NOTIFY_DONE;
770 static struct notifier_block macvlan_notifier_block __read_mostly = {
771 .notifier_call = macvlan_device_event,
774 static int __init macvlan_init_module(void)
776 int err;
778 register_netdevice_notifier(&macvlan_notifier_block);
779 macvlan_handle_frame_hook = macvlan_handle_frame;
781 err = rtnl_link_register(&macvlan_link_ops);
782 if (err < 0)
783 goto err1;
784 return 0;
785 err1:
786 macvlan_handle_frame_hook = NULL;
787 unregister_netdevice_notifier(&macvlan_notifier_block);
788 return err;
791 static void __exit macvlan_cleanup_module(void)
793 rtnl_link_unregister(&macvlan_link_ops);
794 macvlan_handle_frame_hook = NULL;
795 unregister_netdevice_notifier(&macvlan_notifier_block);
798 module_init(macvlan_init_module);
799 module_exit(macvlan_cleanup_module);
801 MODULE_LICENSE("GPL");
802 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
803 MODULE_DESCRIPTION("Driver for MAC address based VLANs");
804 MODULE_ALIAS_RTNL_LINK("macvlan");