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