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