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