blob: 271aa7e1d033b18d9d7b528a802d9e635972a7e2 [file] [log] [blame]
Patrick McHardyb863ceb2007-07-14 18:55:06 -07001/*
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>
Franck Bui-Huu82524742008-05-12 21:21:05 +020023#include <linux/rculist.h>
Patrick McHardyb863ceb2007-07-14 18:55:06 -070024#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
33#define MACVLAN_HASH_SIZE (1 << BITS_PER_BYTE)
34
35struct macvlan_port {
36 struct net_device *dev;
37 struct hlist_head vlan_hash[MACVLAN_HASH_SIZE];
38 struct list_head vlans;
39};
40
41struct macvlan_dev {
42 struct net_device *dev;
43 struct list_head list;
44 struct hlist_node hlist;
45 struct macvlan_port *port;
46 struct net_device *lowerdev;
47};
48
49
50static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
51 const unsigned char *addr)
52{
53 struct macvlan_dev *vlan;
54 struct hlist_node *n;
55
56 hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[addr[5]], hlist) {
Eric Dumazetac067132009-09-01 05:46:05 +000057 if (!compare_ether_addr_64bits(vlan->dev->dev_addr, addr))
Patrick McHardyb863ceb2007-07-14 18:55:06 -070058 return vlan;
59 }
60 return NULL;
61}
62
Eric Biedermanf9ac30f2009-03-13 13:16:13 -070063static void macvlan_hash_add(struct macvlan_dev *vlan)
64{
65 struct macvlan_port *port = vlan->port;
66 const unsigned char *addr = vlan->dev->dev_addr;
67
68 hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]);
69}
70
71static void macvlan_hash_del(struct macvlan_dev *vlan)
72{
73 hlist_del_rcu(&vlan->hlist);
74 synchronize_rcu();
75}
76
77static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
78 const unsigned char *addr)
79{
80 macvlan_hash_del(vlan);
81 /* Now that we are unhashed it is safe to change the device
82 * address without confusing packet delivery.
83 */
84 memcpy(vlan->dev->dev_addr, addr, ETH_ALEN);
85 macvlan_hash_add(vlan);
86}
87
88static int macvlan_addr_busy(const struct macvlan_port *port,
89 const unsigned char *addr)
90{
91 /* Test to see if the specified multicast address is
92 * currently in use by the underlying device or
93 * another macvlan.
94 */
Eric Dumazetac067132009-09-01 05:46:05 +000095 if (!compare_ether_addr_64bits(port->dev->dev_addr, addr))
Eric Biedermanf9ac30f2009-03-13 13:16:13 -070096 return 1;
97
98 if (macvlan_hash_lookup(port, addr))
99 return 1;
100
101 return 0;
102}
103
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700104static void macvlan_broadcast(struct sk_buff *skb,
105 const struct macvlan_port *port)
106{
107 const struct ethhdr *eth = eth_hdr(skb);
108 const struct macvlan_dev *vlan;
109 struct hlist_node *n;
110 struct net_device *dev;
111 struct sk_buff *nskb;
112 unsigned int i;
113
Patrick McHardyefbbced2008-11-26 15:30:48 -0800114 if (skb->protocol == htons(ETH_P_PAUSE))
115 return;
116
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700117 for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
118 hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[i], hlist) {
119 dev = vlan->dev;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700120
121 nskb = skb_clone(skb, GFP_ATOMIC);
122 if (nskb == NULL) {
123 dev->stats.rx_errors++;
124 dev->stats.rx_dropped++;
125 continue;
126 }
127
128 dev->stats.rx_bytes += skb->len + ETH_HLEN;
129 dev->stats.rx_packets++;
130 dev->stats.multicast++;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700131
132 nskb->dev = dev;
Eric Dumazetac067132009-09-01 05:46:05 +0000133 if (!compare_ether_addr_64bits(eth->h_dest, dev->broadcast))
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700134 nskb->pkt_type = PACKET_BROADCAST;
135 else
136 nskb->pkt_type = PACKET_MULTICAST;
137
138 netif_rx(nskb);
139 }
140 }
141}
142
143/* called under rcu_read_lock() from netif_receive_skb */
144static struct sk_buff *macvlan_handle_frame(struct sk_buff *skb)
145{
146 const struct ethhdr *eth = eth_hdr(skb);
147 const struct macvlan_port *port;
148 const struct macvlan_dev *vlan;
149 struct net_device *dev;
150
151 port = rcu_dereference(skb->dev->macvlan_port);
152 if (port == NULL)
153 return skb;
154
155 if (is_multicast_ether_addr(eth->h_dest)) {
156 macvlan_broadcast(skb, port);
157 return skb;
158 }
159
160 vlan = macvlan_hash_lookup(port, eth->h_dest);
161 if (vlan == NULL)
162 return skb;
163
164 dev = vlan->dev;
165 if (unlikely(!(dev->flags & IFF_UP))) {
166 kfree_skb(skb);
167 return NULL;
168 }
169
170 skb = skb_share_check(skb, GFP_ATOMIC);
171 if (skb == NULL) {
172 dev->stats.rx_errors++;
173 dev->stats.rx_dropped++;
174 return NULL;
175 }
176
177 dev->stats.rx_bytes += skb->len + ETH_HLEN;
178 dev->stats.rx_packets++;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700179
180 skb->dev = dev;
181 skb->pkt_type = PACKET_HOST;
182
183 netif_rx(skb);
184 return NULL;
185}
186
Stephen Hemminger424efe92009-08-31 19:50:51 +0000187static netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
188 struct net_device *dev)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700189{
Eric Dumazet2c114552009-09-03 00:11:45 +0000190 int i = skb_get_queue_mapping(skb);
191 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700192 const struct macvlan_dev *vlan = netdev_priv(dev);
193 unsigned int len = skb->len;
194 int ret;
195
196 skb->dev = vlan->lowerdev;
197 ret = dev_queue_xmit(skb);
198
199 if (likely(ret == NET_XMIT_SUCCESS)) {
Eric Dumazet2c114552009-09-03 00:11:45 +0000200 txq->tx_packets++;
201 txq->tx_bytes += len;
202 } else
203 txq->tx_dropped++;
204
Patrick McHardycbbef5e2009-11-10 06:14:24 +0000205 return ret;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700206}
207
208static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700209 unsigned short type, const void *daddr,
210 const void *saddr, unsigned len)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700211{
212 const struct macvlan_dev *vlan = netdev_priv(dev);
213 struct net_device *lowerdev = vlan->lowerdev;
214
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700215 return dev_hard_header(skb, lowerdev, type, daddr,
216 saddr ? : dev->dev_addr, len);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700217}
218
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700219static const struct header_ops macvlan_hard_header_ops = {
220 .create = macvlan_hard_header,
221 .rebuild = eth_rebuild_header,
222 .parse = eth_header_parse,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700223 .cache = eth_header_cache,
224 .cache_update = eth_header_cache_update,
225};
226
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700227static int macvlan_open(struct net_device *dev)
228{
229 struct macvlan_dev *vlan = netdev_priv(dev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700230 struct net_device *lowerdev = vlan->lowerdev;
231 int err;
232
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700233 err = -EBUSY;
234 if (macvlan_addr_busy(vlan->port, dev->dev_addr))
235 goto out;
236
Jiri Pirkoccffad252009-05-22 23:22:17 +0000237 err = dev_unicast_add(lowerdev, dev->dev_addr);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700238 if (err < 0)
Wang Chenb89fb7d2008-07-14 20:57:07 -0700239 goto out;
240 if (dev->flags & IFF_ALLMULTI) {
241 err = dev_set_allmulti(lowerdev, 1);
242 if (err < 0)
243 goto del_unicast;
244 }
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700245 macvlan_hash_add(vlan);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700246 return 0;
Wang Chenb89fb7d2008-07-14 20:57:07 -0700247
248del_unicast:
Jiri Pirkoccffad252009-05-22 23:22:17 +0000249 dev_unicast_delete(lowerdev, dev->dev_addr);
Wang Chenb89fb7d2008-07-14 20:57:07 -0700250out:
251 return err;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700252}
253
254static int macvlan_stop(struct net_device *dev)
255{
256 struct macvlan_dev *vlan = netdev_priv(dev);
257 struct net_device *lowerdev = vlan->lowerdev;
258
259 dev_mc_unsync(lowerdev, dev);
260 if (dev->flags & IFF_ALLMULTI)
261 dev_set_allmulti(lowerdev, -1);
262
Jiri Pirkoccffad252009-05-22 23:22:17 +0000263 dev_unicast_delete(lowerdev, dev->dev_addr);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700264
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700265 macvlan_hash_del(vlan);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700266 return 0;
267}
268
Patrick McHardyad5d20a2007-11-19 22:00:42 -0800269static int macvlan_set_mac_address(struct net_device *dev, void *p)
270{
271 struct macvlan_dev *vlan = netdev_priv(dev);
272 struct net_device *lowerdev = vlan->lowerdev;
273 struct sockaddr *addr = p;
274 int err;
275
276 if (!is_valid_ether_addr(addr->sa_data))
277 return -EADDRNOTAVAIL;
278
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700279 if (!(dev->flags & IFF_UP)) {
280 /* Just copy in the new address */
281 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
282 } else {
283 /* Rehash and update the device filters */
284 if (macvlan_addr_busy(vlan->port, addr->sa_data))
285 return -EBUSY;
Patrick McHardyad5d20a2007-11-19 22:00:42 -0800286
Jiri Pirkoccffad252009-05-22 23:22:17 +0000287 err = dev_unicast_add(lowerdev, addr->sa_data);
288 if (err)
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700289 return err;
Patrick McHardyad5d20a2007-11-19 22:00:42 -0800290
Jiri Pirkoccffad252009-05-22 23:22:17 +0000291 dev_unicast_delete(lowerdev, dev->dev_addr);
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700292
293 macvlan_hash_change_addr(vlan, addr->sa_data);
294 }
Patrick McHardyad5d20a2007-11-19 22:00:42 -0800295 return 0;
296}
297
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700298static void macvlan_change_rx_flags(struct net_device *dev, int change)
299{
300 struct macvlan_dev *vlan = netdev_priv(dev);
301 struct net_device *lowerdev = vlan->lowerdev;
302
303 if (change & IFF_ALLMULTI)
304 dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
305}
306
307static void macvlan_set_multicast_list(struct net_device *dev)
308{
309 struct macvlan_dev *vlan = netdev_priv(dev);
310
311 dev_mc_sync(vlan->lowerdev, dev);
312}
313
314static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
315{
316 struct macvlan_dev *vlan = netdev_priv(dev);
317
318 if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
319 return -EINVAL;
320 dev->mtu = new_mtu;
321 return 0;
322}
323
324/*
325 * macvlan network devices have devices nesting below it and are a special
326 * "super class" of normal network devices; split their locks off into a
327 * separate class since they always nest.
328 */
329static struct lock_class_key macvlan_netdev_xmit_lock_key;
David S. Millercf508b12008-07-22 14:16:42 -0700330static struct lock_class_key macvlan_netdev_addr_lock_key;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700331
332#define MACVLAN_FEATURES \
333 (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
334 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
335 NETIF_F_TSO_ECN | NETIF_F_TSO6)
336
337#define MACVLAN_STATE_MASK \
338 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
339
David S. Millere8a04642008-07-17 00:34:19 -0700340static void macvlan_set_lockdep_class_one(struct net_device *dev,
341 struct netdev_queue *txq,
342 void *_unused)
David S. Millerc773e842008-07-08 23:13:53 -0700343{
344 lockdep_set_class(&txq->_xmit_lock,
345 &macvlan_netdev_xmit_lock_key);
346}
347
348static void macvlan_set_lockdep_class(struct net_device *dev)
349{
David S. Millercf508b12008-07-22 14:16:42 -0700350 lockdep_set_class(&dev->addr_list_lock,
351 &macvlan_netdev_addr_lock_key);
David S. Millere8a04642008-07-17 00:34:19 -0700352 netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL);
David S. Millerc773e842008-07-08 23:13:53 -0700353}
354
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700355static int macvlan_init(struct net_device *dev)
356{
357 struct macvlan_dev *vlan = netdev_priv(dev);
358 const struct net_device *lowerdev = vlan->lowerdev;
359
360 dev->state = (dev->state & ~MACVLAN_STATE_MASK) |
361 (lowerdev->state & MACVLAN_STATE_MASK);
362 dev->features = lowerdev->features & MACVLAN_FEATURES;
363 dev->iflink = lowerdev->ifindex;
sg.tweak@gmail.comef5c8992009-06-10 09:55:02 +0000364 dev->hard_header_len = lowerdev->hard_header_len;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700365
David S. Millerc773e842008-07-08 23:13:53 -0700366 macvlan_set_lockdep_class(dev);
367
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700368 return 0;
369}
370
371static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
372 struct ethtool_drvinfo *drvinfo)
373{
374 snprintf(drvinfo->driver, 32, "macvlan");
375 snprintf(drvinfo->version, 32, "0.1");
376}
377
378static u32 macvlan_ethtool_get_rx_csum(struct net_device *dev)
379{
380 const struct macvlan_dev *vlan = netdev_priv(dev);
Patrick McHardyb1b67dd2009-04-20 04:49:28 +0000381 return dev_ethtool_get_rx_csum(vlan->lowerdev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700382}
383
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700384static int macvlan_ethtool_get_settings(struct net_device *dev,
385 struct ethtool_cmd *cmd)
386{
387 const struct macvlan_dev *vlan = netdev_priv(dev);
Patrick McHardyb1b67dd2009-04-20 04:49:28 +0000388 return dev_ethtool_get_settings(vlan->lowerdev, cmd);
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700389}
390
391static u32 macvlan_ethtool_get_flags(struct net_device *dev)
392{
393 const struct macvlan_dev *vlan = netdev_priv(dev);
Patrick McHardyb1b67dd2009-04-20 04:49:28 +0000394 return dev_ethtool_get_flags(vlan->lowerdev);
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700395}
396
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700397static const struct ethtool_ops macvlan_ethtool_ops = {
398 .get_link = ethtool_op_get_link,
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700399 .get_settings = macvlan_ethtool_get_settings,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700400 .get_rx_csum = macvlan_ethtool_get_rx_csum,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700401 .get_drvinfo = macvlan_ethtool_get_drvinfo,
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700402 .get_flags = macvlan_ethtool_get_flags,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700403};
404
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800405static const struct net_device_ops macvlan_netdev_ops = {
406 .ndo_init = macvlan_init,
407 .ndo_open = macvlan_open,
408 .ndo_stop = macvlan_stop,
Stephen Hemminger00829822008-11-20 20:14:53 -0800409 .ndo_start_xmit = macvlan_start_xmit,
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800410 .ndo_change_mtu = macvlan_change_mtu,
411 .ndo_change_rx_flags = macvlan_change_rx_flags,
412 .ndo_set_mac_address = macvlan_set_mac_address,
413 .ndo_set_multicast_list = macvlan_set_multicast_list,
414 .ndo_validate_addr = eth_validate_addr,
415};
416
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700417static void macvlan_setup(struct net_device *dev)
418{
419 ether_setup(dev);
420
Eric Dumazet93f154b2009-05-18 22:19:19 -0700421 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800422 dev->netdev_ops = &macvlan_netdev_ops;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700423 dev->destructor = free_netdev;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700424 dev->header_ops = &macvlan_hard_header_ops,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700425 dev->ethtool_ops = &macvlan_ethtool_ops;
426 dev->tx_queue_len = 0;
427}
428
429static int macvlan_port_create(struct net_device *dev)
430{
431 struct macvlan_port *port;
432 unsigned int i;
433
434 if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
435 return -EINVAL;
436
437 port = kzalloc(sizeof(*port), GFP_KERNEL);
438 if (port == NULL)
439 return -ENOMEM;
440
441 port->dev = dev;
442 INIT_LIST_HEAD(&port->vlans);
443 for (i = 0; i < MACVLAN_HASH_SIZE; i++)
444 INIT_HLIST_HEAD(&port->vlan_hash[i]);
445 rcu_assign_pointer(dev->macvlan_port, port);
446 return 0;
447}
448
449static void macvlan_port_destroy(struct net_device *dev)
450{
451 struct macvlan_port *port = dev->macvlan_port;
452
453 rcu_assign_pointer(dev->macvlan_port, NULL);
454 synchronize_rcu();
455 kfree(port);
456}
457
458static void macvlan_transfer_operstate(struct net_device *dev)
459{
460 struct macvlan_dev *vlan = netdev_priv(dev);
461 const struct net_device *lowerdev = vlan->lowerdev;
462
463 if (lowerdev->operstate == IF_OPER_DORMANT)
464 netif_dormant_on(dev);
465 else
466 netif_dormant_off(dev);
467
468 if (netif_carrier_ok(lowerdev)) {
469 if (!netif_carrier_ok(dev))
470 netif_carrier_on(dev);
471 } else {
Patrick McHardyf12ca5f2008-01-21 00:47:08 -0800472 if (netif_carrier_ok(dev))
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700473 netif_carrier_off(dev);
474 }
475}
476
477static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
478{
479 if (tb[IFLA_ADDRESS]) {
480 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
481 return -EINVAL;
482 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
483 return -EADDRNOTAVAIL;
484 }
485 return 0;
486}
487
Eric Dumazet2c114552009-09-03 00:11:45 +0000488static int macvlan_get_tx_queues(struct net *net,
489 struct nlattr *tb[],
490 unsigned int *num_tx_queues,
491 unsigned int *real_num_tx_queues)
492{
493 struct net_device *real_dev;
494
495 if (!tb[IFLA_LINK])
496 return -EINVAL;
497
498 real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
499 if (!real_dev)
500 return -ENODEV;
501
502 *num_tx_queues = real_dev->num_tx_queues;
503 *real_num_tx_queues = real_dev->real_num_tx_queues;
504 return 0;
505}
506
Eric W. Biederman81adee42009-11-08 00:53:51 -0800507static int macvlan_newlink(struct net *src_net, struct net_device *dev,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700508 struct nlattr *tb[], struct nlattr *data[])
509{
510 struct macvlan_dev *vlan = netdev_priv(dev);
511 struct macvlan_port *port;
512 struct net_device *lowerdev;
513 int err;
514
515 if (!tb[IFLA_LINK])
516 return -EINVAL;
517
Eric W. Biederman81adee42009-11-08 00:53:51 -0800518 lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700519 if (lowerdev == NULL)
520 return -ENODEV;
521
Eric Biedermanb0832a22009-03-13 13:15:37 -0700522 /* When creating macvlans on top of other macvlans - use
523 * the real device as the lowerdev.
Patrick McHardya6ca5f12008-01-10 22:39:28 -0800524 */
Eric Biedermanb0832a22009-03-13 13:15:37 -0700525 if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) {
526 struct macvlan_dev *lowervlan = netdev_priv(lowerdev);
527 lowerdev = lowervlan->lowerdev;
528 }
Patrick McHardya6ca5f12008-01-10 22:39:28 -0800529
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700530 if (!tb[IFLA_MTU])
531 dev->mtu = lowerdev->mtu;
532 else if (dev->mtu > lowerdev->mtu)
533 return -EINVAL;
534
535 if (!tb[IFLA_ADDRESS])
536 random_ether_addr(dev->dev_addr);
537
538 if (lowerdev->macvlan_port == NULL) {
539 err = macvlan_port_create(lowerdev);
540 if (err < 0)
541 return err;
542 }
543 port = lowerdev->macvlan_port;
544
545 vlan->lowerdev = lowerdev;
546 vlan->dev = dev;
547 vlan->port = port;
548
549 err = register_netdevice(dev);
550 if (err < 0)
551 return err;
552
553 list_add_tail(&vlan->list, &port->vlans);
554 macvlan_transfer_operstate(dev);
555 return 0;
556}
557
Eric Dumazet23289a32009-10-27 07:06:36 +0000558static void macvlan_dellink(struct net_device *dev, struct list_head *head)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700559{
560 struct macvlan_dev *vlan = netdev_priv(dev);
561 struct macvlan_port *port = vlan->port;
562
563 list_del(&vlan->list);
Eric Dumazet23289a32009-10-27 07:06:36 +0000564 unregister_netdevice_queue(dev, head);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700565
566 if (list_empty(&port->vlans))
Patrick McHardy73120962008-05-08 01:13:31 -0700567 macvlan_port_destroy(port->dev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700568}
569
570static struct rtnl_link_ops macvlan_link_ops __read_mostly = {
571 .kind = "macvlan",
572 .priv_size = sizeof(struct macvlan_dev),
Eric Dumazet2c114552009-09-03 00:11:45 +0000573 .get_tx_queues = macvlan_get_tx_queues,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700574 .setup = macvlan_setup,
575 .validate = macvlan_validate,
576 .newlink = macvlan_newlink,
577 .dellink = macvlan_dellink,
578};
579
580static int macvlan_device_event(struct notifier_block *unused,
581 unsigned long event, void *ptr)
582{
583 struct net_device *dev = ptr;
584 struct macvlan_dev *vlan, *next;
585 struct macvlan_port *port;
586
587 port = dev->macvlan_port;
588 if (port == NULL)
589 return NOTIFY_DONE;
590
591 switch (event) {
592 case NETDEV_CHANGE:
593 list_for_each_entry(vlan, &port->vlans, list)
594 macvlan_transfer_operstate(vlan->dev);
595 break;
596 case NETDEV_FEAT_CHANGE:
597 list_for_each_entry(vlan, &port->vlans, list) {
598 vlan->dev->features = dev->features & MACVLAN_FEATURES;
599 netdev_features_change(vlan->dev);
600 }
601 break;
602 case NETDEV_UNREGISTER:
603 list_for_each_entry_safe(vlan, next, &port->vlans, list)
Eric Dumazet23289a32009-10-27 07:06:36 +0000604 macvlan_dellink(vlan->dev, NULL);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700605 break;
606 }
607 return NOTIFY_DONE;
608}
609
610static struct notifier_block macvlan_notifier_block __read_mostly = {
611 .notifier_call = macvlan_device_event,
612};
613
614static int __init macvlan_init_module(void)
615{
616 int err;
617
618 register_netdevice_notifier(&macvlan_notifier_block);
619 macvlan_handle_frame_hook = macvlan_handle_frame;
620
621 err = rtnl_link_register(&macvlan_link_ops);
622 if (err < 0)
623 goto err1;
624 return 0;
625err1:
Rami Rosen52913242008-01-31 16:56:03 -0800626 macvlan_handle_frame_hook = NULL;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700627 unregister_netdevice_notifier(&macvlan_notifier_block);
628 return err;
629}
630
631static void __exit macvlan_cleanup_module(void)
632{
633 rtnl_link_unregister(&macvlan_link_ops);
634 macvlan_handle_frame_hook = NULL;
635 unregister_netdevice_notifier(&macvlan_notifier_block);
636}
637
638module_init(macvlan_init_module);
639module_exit(macvlan_cleanup_module);
640
641MODULE_LICENSE("GPL");
642MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
643MODULE_DESCRIPTION("Driver for MAC address based VLANs");
644MODULE_ALIAS_RTNL_LINK("macvlan");