macvlan: implement bridge, VEPA and private mode
[firefly-linux-kernel-4.4.55.git] / drivers / net / macvlan.c
1 /*
2  * Copyright (c) 2007 Patrick McHardy <kaber@trash.net>
3  *
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.
8  *
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  * ---
15  */
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>
33
34 #define MACVLAN_HASH_SIZE       (1 << BITS_PER_BYTE)
35
36 enum macvlan_mode {
37         MACVLAN_MODE_PRIVATE    = 1,
38         MACVLAN_MODE_VEPA       = 2,
39         MACVLAN_MODE_BRIDGE     = 4,
40 };
41
42 struct macvlan_port {
43         struct net_device       *dev;
44         struct hlist_head       vlan_hash[MACVLAN_HASH_SIZE];
45         struct list_head        vlans;
46 };
47
48 /**
49  *      struct macvlan_rx_stats - MACVLAN percpu rx stats
50  *      @rx_packets: number of received packets
51  *      @rx_bytes: number of received bytes
52  *      @multicast: number of received multicast packets
53  *      @rx_errors: number of errors
54  */
55 struct macvlan_rx_stats {
56         unsigned long rx_packets;
57         unsigned long rx_bytes;
58         unsigned long multicast;
59         unsigned long rx_errors;
60 };
61
62 struct macvlan_dev {
63         struct net_device       *dev;
64         struct list_head        list;
65         struct hlist_node       hlist;
66         struct macvlan_port     *port;
67         struct net_device       *lowerdev;
68         struct macvlan_rx_stats *rx_stats;
69         enum macvlan_mode       mode;
70 };
71
72
73 static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
74                                                const unsigned char *addr)
75 {
76         struct macvlan_dev *vlan;
77         struct hlist_node *n;
78
79         hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[addr[5]], hlist) {
80                 if (!compare_ether_addr_64bits(vlan->dev->dev_addr, addr))
81                         return vlan;
82         }
83         return NULL;
84 }
85
86 static void macvlan_hash_add(struct macvlan_dev *vlan)
87 {
88         struct macvlan_port *port = vlan->port;
89         const unsigned char *addr = vlan->dev->dev_addr;
90
91         hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]);
92 }
93
94 static void macvlan_hash_del(struct macvlan_dev *vlan)
95 {
96         hlist_del_rcu(&vlan->hlist);
97         synchronize_rcu();
98 }
99
100 static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
101                                         const unsigned char *addr)
102 {
103         macvlan_hash_del(vlan);
104         /* Now that we are unhashed it is safe to change the device
105          * address without confusing packet delivery.
106          */
107         memcpy(vlan->dev->dev_addr, addr, ETH_ALEN);
108         macvlan_hash_add(vlan);
109 }
110
111 static int macvlan_addr_busy(const struct macvlan_port *port,
112                                 const unsigned char *addr)
113 {
114         /* Test to see if the specified multicast address is
115          * currently in use by the underlying device or
116          * another macvlan.
117          */
118         if (!compare_ether_addr_64bits(port->dev->dev_addr, addr))
119                 return 1;
120
121         if (macvlan_hash_lookup(port, addr))
122                 return 1;
123
124         return 0;
125 }
126
127 static inline void macvlan_count_rx(const struct macvlan_dev *vlan,
128                                     unsigned int len, bool success,
129                                     bool multicast)
130 {
131         struct macvlan_rx_stats *rx_stats;
132
133         rx_stats = per_cpu_ptr(vlan->rx_stats, smp_processor_id());
134         if (likely(success)) {
135                 rx_stats->rx_packets++;;
136                 rx_stats->rx_bytes += len;
137                 if (multicast)
138                         rx_stats->multicast++;
139         } else {
140                 rx_stats->rx_errors++;
141         }
142 }
143
144 static int macvlan_broadcast_one(struct sk_buff *skb, struct net_device *dev,
145                                  const struct ethhdr *eth, bool local)
146 {
147         if (!skb)
148                 return NET_RX_DROP;
149
150         if (local)
151                 return dev_forward_skb(dev, skb);
152
153         skb->dev = dev;
154         if (!compare_ether_addr_64bits(eth->h_dest,
155                                        dev->broadcast))
156                 skb->pkt_type = PACKET_BROADCAST;
157         else
158                 skb->pkt_type = PACKET_MULTICAST;
159
160         return netif_rx(skb);
161 }
162
163 static void macvlan_broadcast(struct sk_buff *skb,
164                               const struct macvlan_port *port,
165                               struct net_device *src,
166                               enum macvlan_mode mode)
167 {
168         const struct ethhdr *eth = eth_hdr(skb);
169         const struct macvlan_dev *vlan;
170         struct hlist_node *n;
171         struct sk_buff *nskb;
172         unsigned int i;
173         int err;
174
175         if (skb->protocol == htons(ETH_P_PAUSE))
176                 return;
177
178         for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
179                 hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[i], hlist) {
180                         if (vlan->dev == src || !(vlan->mode & mode))
181                                 continue;
182
183                         nskb = skb_clone(skb, GFP_ATOMIC);
184                         err = macvlan_broadcast_one(nskb, vlan->dev, eth,
185                                          mode == MACVLAN_MODE_BRIDGE);
186                         macvlan_count_rx(vlan, skb->len + ETH_HLEN,
187                                          err == NET_RX_SUCCESS, 1);
188                 }
189         }
190 }
191
192 /* called under rcu_read_lock() from netif_receive_skb */
193 static struct sk_buff *macvlan_handle_frame(struct sk_buff *skb)
194 {
195         const struct ethhdr *eth = eth_hdr(skb);
196         const struct macvlan_port *port;
197         const struct macvlan_dev *vlan;
198         const struct macvlan_dev *src;
199         struct net_device *dev;
200         unsigned int len;
201
202         port = rcu_dereference(skb->dev->macvlan_port);
203         if (port == NULL)
204                 return skb;
205
206         if (is_multicast_ether_addr(eth->h_dest)) {
207                 src = macvlan_hash_lookup(port, eth->h_source);
208                 if (!src)
209                         /* frame comes from an external address */
210                         macvlan_broadcast(skb, port, NULL,
211                                           MACVLAN_MODE_PRIVATE |
212                                           MACVLAN_MODE_VEPA    |
213                                           MACVLAN_MODE_BRIDGE);
214                 else if (src->mode == MACVLAN_MODE_VEPA)
215                         /* flood to everyone except source */
216                         macvlan_broadcast(skb, port, src->dev,
217                                           MACVLAN_MODE_VEPA |
218                                           MACVLAN_MODE_BRIDGE);
219                 else if (src->mode == MACVLAN_MODE_BRIDGE)
220                         /*
221                          * flood only to VEPA ports, bridge ports
222                          * already saw the frame on the way out.
223                          */
224                         macvlan_broadcast(skb, port, src->dev,
225                                           MACVLAN_MODE_VEPA);
226                 return skb;
227         }
228
229         vlan = macvlan_hash_lookup(port, eth->h_dest);
230         if (vlan == NULL)
231                 return skb;
232
233         dev = vlan->dev;
234         if (unlikely(!(dev->flags & IFF_UP))) {
235                 kfree_skb(skb);
236                 return NULL;
237         }
238         len = skb->len + ETH_HLEN;
239         skb = skb_share_check(skb, GFP_ATOMIC);
240         macvlan_count_rx(vlan, len, skb != NULL, 0);
241         if (!skb)
242                 return NULL;
243
244         skb->dev = dev;
245         skb->pkt_type = PACKET_HOST;
246
247         netif_rx(skb);
248         return NULL;
249 }
250
251 static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
252 {
253         const struct macvlan_dev *vlan = netdev_priv(dev);
254         const struct macvlan_port *port = vlan->port;
255         const struct macvlan_dev *dest;
256
257         if (vlan->mode == MACVLAN_MODE_BRIDGE) {
258                 const struct ethhdr *eth = (void *)skb->data;
259
260                 /* send to other bridge ports directly */
261                 if (is_multicast_ether_addr(eth->h_dest)) {
262                         macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE);
263                         goto xmit_world;
264                 }
265
266                 dest = macvlan_hash_lookup(port, eth->h_dest);
267                 if (dest && dest->mode == MACVLAN_MODE_BRIDGE) {
268                         unsigned int length = skb->len + ETH_HLEN;
269                         int ret = dev_forward_skb(dest->dev, skb);
270                         macvlan_count_rx(dest, length,
271                                          ret == NET_RX_SUCCESS, 0);
272
273                         return NET_XMIT_SUCCESS;
274                 }
275         }
276
277 xmit_world:
278         skb->dev = vlan->lowerdev;
279         return dev_queue_xmit(skb);
280 }
281
282 static netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
283                                       struct net_device *dev)
284 {
285         int i = skb_get_queue_mapping(skb);
286         struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
287         unsigned int len = skb->len;
288         int ret;
289
290         ret = macvlan_queue_xmit(skb, dev);
291         if (likely(ret == NET_XMIT_SUCCESS)) {
292                 txq->tx_packets++;
293                 txq->tx_bytes += len;
294         } else
295                 txq->tx_dropped++;
296
297         return ret;
298 }
299
300 static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
301                                unsigned short type, const void *daddr,
302                                const void *saddr, unsigned len)
303 {
304         const struct macvlan_dev *vlan = netdev_priv(dev);
305         struct net_device *lowerdev = vlan->lowerdev;
306
307         return dev_hard_header(skb, lowerdev, type, daddr,
308                                saddr ? : dev->dev_addr, len);
309 }
310
311 static const struct header_ops macvlan_hard_header_ops = {
312         .create         = macvlan_hard_header,
313         .rebuild        = eth_rebuild_header,
314         .parse          = eth_header_parse,
315         .cache          = eth_header_cache,
316         .cache_update   = eth_header_cache_update,
317 };
318
319 static int macvlan_open(struct net_device *dev)
320 {
321         struct macvlan_dev *vlan = netdev_priv(dev);
322         struct net_device *lowerdev = vlan->lowerdev;
323         int err;
324
325         err = -EBUSY;
326         if (macvlan_addr_busy(vlan->port, dev->dev_addr))
327                 goto out;
328
329         err = dev_unicast_add(lowerdev, dev->dev_addr);
330         if (err < 0)
331                 goto out;
332         if (dev->flags & IFF_ALLMULTI) {
333                 err = dev_set_allmulti(lowerdev, 1);
334                 if (err < 0)
335                         goto del_unicast;
336         }
337         macvlan_hash_add(vlan);
338         return 0;
339
340 del_unicast:
341         dev_unicast_delete(lowerdev, dev->dev_addr);
342 out:
343         return err;
344 }
345
346 static int macvlan_stop(struct net_device *dev)
347 {
348         struct macvlan_dev *vlan = netdev_priv(dev);
349         struct net_device *lowerdev = vlan->lowerdev;
350
351         dev_mc_unsync(lowerdev, dev);
352         if (dev->flags & IFF_ALLMULTI)
353                 dev_set_allmulti(lowerdev, -1);
354
355         dev_unicast_delete(lowerdev, dev->dev_addr);
356
357         macvlan_hash_del(vlan);
358         return 0;
359 }
360
361 static int macvlan_set_mac_address(struct net_device *dev, void *p)
362 {
363         struct macvlan_dev *vlan = netdev_priv(dev);
364         struct net_device *lowerdev = vlan->lowerdev;
365         struct sockaddr *addr = p;
366         int err;
367
368         if (!is_valid_ether_addr(addr->sa_data))
369                 return -EADDRNOTAVAIL;
370
371         if (!(dev->flags & IFF_UP)) {
372                 /* Just copy in the new address */
373                 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
374         } else {
375                 /* Rehash and update the device filters */
376                 if (macvlan_addr_busy(vlan->port, addr->sa_data))
377                         return -EBUSY;
378
379                 err = dev_unicast_add(lowerdev, addr->sa_data);
380                 if (err)
381                         return err;
382
383                 dev_unicast_delete(lowerdev, dev->dev_addr);
384
385                 macvlan_hash_change_addr(vlan, addr->sa_data);
386         }
387         return 0;
388 }
389
390 static void macvlan_change_rx_flags(struct net_device *dev, int change)
391 {
392         struct macvlan_dev *vlan = netdev_priv(dev);
393         struct net_device *lowerdev = vlan->lowerdev;
394
395         if (change & IFF_ALLMULTI)
396                 dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
397 }
398
399 static void macvlan_set_multicast_list(struct net_device *dev)
400 {
401         struct macvlan_dev *vlan = netdev_priv(dev);
402
403         dev_mc_sync(vlan->lowerdev, dev);
404 }
405
406 static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
407 {
408         struct macvlan_dev *vlan = netdev_priv(dev);
409
410         if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
411                 return -EINVAL;
412         dev->mtu = new_mtu;
413         return 0;
414 }
415
416 /*
417  * macvlan network devices have devices nesting below it and are a special
418  * "super class" of normal network devices; split their locks off into a
419  * separate class since they always nest.
420  */
421 static struct lock_class_key macvlan_netdev_xmit_lock_key;
422 static struct lock_class_key macvlan_netdev_addr_lock_key;
423
424 #define MACVLAN_FEATURES \
425         (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
426          NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
427          NETIF_F_TSO_ECN | NETIF_F_TSO6)
428
429 #define MACVLAN_STATE_MASK \
430         ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
431
432 static void macvlan_set_lockdep_class_one(struct net_device *dev,
433                                           struct netdev_queue *txq,
434                                           void *_unused)
435 {
436         lockdep_set_class(&txq->_xmit_lock,
437                           &macvlan_netdev_xmit_lock_key);
438 }
439
440 static void macvlan_set_lockdep_class(struct net_device *dev)
441 {
442         lockdep_set_class(&dev->addr_list_lock,
443                           &macvlan_netdev_addr_lock_key);
444         netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL);
445 }
446
447 static int macvlan_init(struct net_device *dev)
448 {
449         struct macvlan_dev *vlan = netdev_priv(dev);
450         const struct net_device *lowerdev = vlan->lowerdev;
451
452         dev->state              = (dev->state & ~MACVLAN_STATE_MASK) |
453                                   (lowerdev->state & MACVLAN_STATE_MASK);
454         dev->features           = lowerdev->features & MACVLAN_FEATURES;
455         dev->iflink             = lowerdev->ifindex;
456         dev->hard_header_len    = lowerdev->hard_header_len;
457
458         macvlan_set_lockdep_class(dev);
459
460         vlan->rx_stats = alloc_percpu(struct macvlan_rx_stats);
461         if (!vlan->rx_stats)
462                 return -ENOMEM;
463
464         return 0;
465 }
466
467 static void macvlan_uninit(struct net_device *dev)
468 {
469         struct macvlan_dev *vlan = netdev_priv(dev);
470
471         free_percpu(vlan->rx_stats);
472 }
473
474 static struct net_device_stats *macvlan_dev_get_stats(struct net_device *dev)
475 {
476         struct net_device_stats *stats = &dev->stats;
477         struct macvlan_dev *vlan = netdev_priv(dev);
478
479         dev_txq_stats_fold(dev, stats);
480
481         if (vlan->rx_stats) {
482                 struct macvlan_rx_stats *p, rx = {0};
483                 int i;
484
485                 for_each_possible_cpu(i) {
486                         p = per_cpu_ptr(vlan->rx_stats, i);
487                         rx.rx_packets += p->rx_packets;
488                         rx.rx_bytes   += p->rx_bytes;
489                         rx.rx_errors  += p->rx_errors;
490                         rx.multicast  += p->multicast;
491                 }
492                 stats->rx_packets = rx.rx_packets;
493                 stats->rx_bytes   = rx.rx_bytes;
494                 stats->rx_errors  = rx.rx_errors;
495                 stats->rx_dropped = rx.rx_errors;
496                 stats->multicast  = rx.multicast;
497         }
498         return stats;
499 }
500
501 static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
502                                         struct ethtool_drvinfo *drvinfo)
503 {
504         snprintf(drvinfo->driver, 32, "macvlan");
505         snprintf(drvinfo->version, 32, "0.1");
506 }
507
508 static u32 macvlan_ethtool_get_rx_csum(struct net_device *dev)
509 {
510         const struct macvlan_dev *vlan = netdev_priv(dev);
511         return dev_ethtool_get_rx_csum(vlan->lowerdev);
512 }
513
514 static int macvlan_ethtool_get_settings(struct net_device *dev,
515                                         struct ethtool_cmd *cmd)
516 {
517         const struct macvlan_dev *vlan = netdev_priv(dev);
518         return dev_ethtool_get_settings(vlan->lowerdev, cmd);
519 }
520
521 static u32 macvlan_ethtool_get_flags(struct net_device *dev)
522 {
523         const struct macvlan_dev *vlan = netdev_priv(dev);
524         return dev_ethtool_get_flags(vlan->lowerdev);
525 }
526
527 static const struct ethtool_ops macvlan_ethtool_ops = {
528         .get_link               = ethtool_op_get_link,
529         .get_settings           = macvlan_ethtool_get_settings,
530         .get_rx_csum            = macvlan_ethtool_get_rx_csum,
531         .get_drvinfo            = macvlan_ethtool_get_drvinfo,
532         .get_flags              = macvlan_ethtool_get_flags,
533 };
534
535 static const struct net_device_ops macvlan_netdev_ops = {
536         .ndo_init               = macvlan_init,
537         .ndo_uninit             = macvlan_uninit,
538         .ndo_open               = macvlan_open,
539         .ndo_stop               = macvlan_stop,
540         .ndo_start_xmit         = macvlan_start_xmit,
541         .ndo_change_mtu         = macvlan_change_mtu,
542         .ndo_change_rx_flags    = macvlan_change_rx_flags,
543         .ndo_set_mac_address    = macvlan_set_mac_address,
544         .ndo_set_multicast_list = macvlan_set_multicast_list,
545         .ndo_get_stats          = macvlan_dev_get_stats,
546         .ndo_validate_addr      = eth_validate_addr,
547 };
548
549 static void macvlan_setup(struct net_device *dev)
550 {
551         ether_setup(dev);
552
553         dev->priv_flags        &= ~IFF_XMIT_DST_RELEASE;
554         dev->netdev_ops         = &macvlan_netdev_ops;
555         dev->destructor         = free_netdev;
556         dev->header_ops         = &macvlan_hard_header_ops,
557         dev->ethtool_ops        = &macvlan_ethtool_ops;
558         dev->tx_queue_len       = 0;
559 }
560
561 static int macvlan_port_create(struct net_device *dev)
562 {
563         struct macvlan_port *port;
564         unsigned int i;
565
566         if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
567                 return -EINVAL;
568
569         port = kzalloc(sizeof(*port), GFP_KERNEL);
570         if (port == NULL)
571                 return -ENOMEM;
572
573         port->dev = dev;
574         INIT_LIST_HEAD(&port->vlans);
575         for (i = 0; i < MACVLAN_HASH_SIZE; i++)
576                 INIT_HLIST_HEAD(&port->vlan_hash[i]);
577         rcu_assign_pointer(dev->macvlan_port, port);
578         return 0;
579 }
580
581 static void macvlan_port_destroy(struct net_device *dev)
582 {
583         struct macvlan_port *port = dev->macvlan_port;
584
585         rcu_assign_pointer(dev->macvlan_port, NULL);
586         synchronize_rcu();
587         kfree(port);
588 }
589
590 static void macvlan_transfer_operstate(struct net_device *dev)
591 {
592         struct macvlan_dev *vlan = netdev_priv(dev);
593         const struct net_device *lowerdev = vlan->lowerdev;
594
595         if (lowerdev->operstate == IF_OPER_DORMANT)
596                 netif_dormant_on(dev);
597         else
598                 netif_dormant_off(dev);
599
600         if (netif_carrier_ok(lowerdev)) {
601                 if (!netif_carrier_ok(dev))
602                         netif_carrier_on(dev);
603         } else {
604                 if (netif_carrier_ok(dev))
605                         netif_carrier_off(dev);
606         }
607 }
608
609 static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
610 {
611         if (tb[IFLA_ADDRESS]) {
612                 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
613                         return -EINVAL;
614                 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
615                         return -EADDRNOTAVAIL;
616         }
617         return 0;
618 }
619
620 static int macvlan_get_tx_queues(struct net *net,
621                                  struct nlattr *tb[],
622                                  unsigned int *num_tx_queues,
623                                  unsigned int *real_num_tx_queues)
624 {
625         struct net_device *real_dev;
626
627         if (!tb[IFLA_LINK])
628                 return -EINVAL;
629
630         real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
631         if (!real_dev)
632                 return -ENODEV;
633
634         *num_tx_queues      = real_dev->num_tx_queues;
635         *real_num_tx_queues = real_dev->real_num_tx_queues;
636         return 0;
637 }
638
639 static int macvlan_newlink(struct net *src_net, struct net_device *dev,
640                            struct nlattr *tb[], struct nlattr *data[])
641 {
642         struct macvlan_dev *vlan = netdev_priv(dev);
643         struct macvlan_port *port;
644         struct net_device *lowerdev;
645         int err;
646
647         if (!tb[IFLA_LINK])
648                 return -EINVAL;
649
650         lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
651         if (lowerdev == NULL)
652                 return -ENODEV;
653
654         /* When creating macvlans on top of other macvlans - use
655          * the real device as the lowerdev.
656          */
657         if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) {
658                 struct macvlan_dev *lowervlan = netdev_priv(lowerdev);
659                 lowerdev = lowervlan->lowerdev;
660         }
661
662         if (!tb[IFLA_MTU])
663                 dev->mtu = lowerdev->mtu;
664         else if (dev->mtu > lowerdev->mtu)
665                 return -EINVAL;
666
667         if (!tb[IFLA_ADDRESS])
668                 random_ether_addr(dev->dev_addr);
669
670         if (lowerdev->macvlan_port == NULL) {
671                 err = macvlan_port_create(lowerdev);
672                 if (err < 0)
673                         return err;
674         }
675         port = lowerdev->macvlan_port;
676
677         vlan->lowerdev = lowerdev;
678         vlan->dev      = dev;
679         vlan->port     = port;
680
681         err = register_netdevice(dev);
682         if (err < 0)
683                 return err;
684
685         list_add_tail(&vlan->list, &port->vlans);
686         macvlan_transfer_operstate(dev);
687         return 0;
688 }
689
690 static void macvlan_dellink(struct net_device *dev, struct list_head *head)
691 {
692         struct macvlan_dev *vlan = netdev_priv(dev);
693         struct macvlan_port *port = vlan->port;
694
695         list_del(&vlan->list);
696         unregister_netdevice_queue(dev, head);
697
698         if (list_empty(&port->vlans))
699                 macvlan_port_destroy(port->dev);
700 }
701
702 static struct rtnl_link_ops macvlan_link_ops __read_mostly = {
703         .kind           = "macvlan",
704         .priv_size      = sizeof(struct macvlan_dev),
705         .get_tx_queues  = macvlan_get_tx_queues,
706         .setup          = macvlan_setup,
707         .validate       = macvlan_validate,
708         .newlink        = macvlan_newlink,
709         .dellink        = macvlan_dellink,
710 };
711
712 static int macvlan_device_event(struct notifier_block *unused,
713                                 unsigned long event, void *ptr)
714 {
715         struct net_device *dev = ptr;
716         struct macvlan_dev *vlan, *next;
717         struct macvlan_port *port;
718
719         port = dev->macvlan_port;
720         if (port == NULL)
721                 return NOTIFY_DONE;
722
723         switch (event) {
724         case NETDEV_CHANGE:
725                 list_for_each_entry(vlan, &port->vlans, list)
726                         macvlan_transfer_operstate(vlan->dev);
727                 break;
728         case NETDEV_FEAT_CHANGE:
729                 list_for_each_entry(vlan, &port->vlans, list) {
730                         vlan->dev->features = dev->features & MACVLAN_FEATURES;
731                         netdev_features_change(vlan->dev);
732                 }
733                 break;
734         case NETDEV_UNREGISTER:
735                 list_for_each_entry_safe(vlan, next, &port->vlans, list)
736                         macvlan_dellink(vlan->dev, NULL);
737                 break;
738         }
739         return NOTIFY_DONE;
740 }
741
742 static struct notifier_block macvlan_notifier_block __read_mostly = {
743         .notifier_call  = macvlan_device_event,
744 };
745
746 static int __init macvlan_init_module(void)
747 {
748         int err;
749
750         register_netdevice_notifier(&macvlan_notifier_block);
751         macvlan_handle_frame_hook = macvlan_handle_frame;
752
753         err = rtnl_link_register(&macvlan_link_ops);
754         if (err < 0)
755                 goto err1;
756         return 0;
757 err1:
758         macvlan_handle_frame_hook = NULL;
759         unregister_netdevice_notifier(&macvlan_notifier_block);
760         return err;
761 }
762
763 static void __exit macvlan_cleanup_module(void)
764 {
765         rtnl_link_unregister(&macvlan_link_ops);
766         macvlan_handle_frame_hook = NULL;
767         unregister_netdevice_notifier(&macvlan_notifier_block);
768 }
769
770 module_init(macvlan_init_module);
771 module_exit(macvlan_cleanup_module);
772
773 MODULE_LICENSE("GPL");
774 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
775 MODULE_DESCRIPTION("Driver for MAC address based VLANs");
776 MODULE_ALIAS_RTNL_LINK("macvlan");