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