blob: ae2b5c79c55e73295b5daaffd6e6f1d13a4236f3 [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
Eric Dumazetfccaf712009-11-17 08:53:49 +000041/**
42 * struct macvlan_rx_stats - MACVLAN percpu rx stats
43 * @rx_packets: number of received packets
44 * @rx_bytes: number of received bytes
45 * @multicast: number of received multicast packets
46 * @rx_errors: number of errors
47 */
48struct macvlan_rx_stats {
49 unsigned long rx_packets;
50 unsigned long rx_bytes;
51 unsigned long multicast;
52 unsigned long rx_errors;
53};
54
Patrick McHardyb863ceb2007-07-14 18:55:06 -070055struct macvlan_dev {
56 struct net_device *dev;
57 struct list_head list;
58 struct hlist_node hlist;
59 struct macvlan_port *port;
60 struct net_device *lowerdev;
Eric Dumazetfccaf712009-11-17 08:53:49 +000061 struct macvlan_rx_stats *rx_stats;
Patrick McHardyb863ceb2007-07-14 18:55:06 -070062};
63
64
65static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
66 const unsigned char *addr)
67{
68 struct macvlan_dev *vlan;
69 struct hlist_node *n;
70
71 hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[addr[5]], hlist) {
Eric Dumazetac067132009-09-01 05:46:05 +000072 if (!compare_ether_addr_64bits(vlan->dev->dev_addr, addr))
Patrick McHardyb863ceb2007-07-14 18:55:06 -070073 return vlan;
74 }
75 return NULL;
76}
77
Eric Biedermanf9ac30f2009-03-13 13:16:13 -070078static void macvlan_hash_add(struct macvlan_dev *vlan)
79{
80 struct macvlan_port *port = vlan->port;
81 const unsigned char *addr = vlan->dev->dev_addr;
82
83 hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]);
84}
85
86static void macvlan_hash_del(struct macvlan_dev *vlan)
87{
88 hlist_del_rcu(&vlan->hlist);
89 synchronize_rcu();
90}
91
92static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
93 const unsigned char *addr)
94{
95 macvlan_hash_del(vlan);
96 /* Now that we are unhashed it is safe to change the device
97 * address without confusing packet delivery.
98 */
99 memcpy(vlan->dev->dev_addr, addr, ETH_ALEN);
100 macvlan_hash_add(vlan);
101}
102
103static int macvlan_addr_busy(const struct macvlan_port *port,
104 const unsigned char *addr)
105{
106 /* Test to see if the specified multicast address is
107 * currently in use by the underlying device or
108 * another macvlan.
109 */
Eric Dumazetac067132009-09-01 05:46:05 +0000110 if (!compare_ether_addr_64bits(port->dev->dev_addr, addr))
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700111 return 1;
112
113 if (macvlan_hash_lookup(port, addr))
114 return 1;
115
116 return 0;
117}
118
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700119static void macvlan_broadcast(struct sk_buff *skb,
120 const struct macvlan_port *port)
121{
122 const struct ethhdr *eth = eth_hdr(skb);
123 const struct macvlan_dev *vlan;
124 struct hlist_node *n;
125 struct net_device *dev;
126 struct sk_buff *nskb;
127 unsigned int i;
Eric Dumazetfccaf712009-11-17 08:53:49 +0000128 struct macvlan_rx_stats *rx_stats;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700129
Patrick McHardyefbbced2008-11-26 15:30:48 -0800130 if (skb->protocol == htons(ETH_P_PAUSE))
131 return;
132
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700133 for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
134 hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[i], hlist) {
135 dev = vlan->dev;
Eric Dumazetfccaf712009-11-17 08:53:49 +0000136 rx_stats = per_cpu_ptr(vlan->rx_stats, smp_processor_id());
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700137
138 nskb = skb_clone(skb, GFP_ATOMIC);
139 if (nskb == NULL) {
Eric Dumazetfccaf712009-11-17 08:53:49 +0000140 rx_stats->rx_errors++;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700141 continue;
142 }
143
Eric Dumazetfccaf712009-11-17 08:53:49 +0000144 rx_stats->rx_bytes += skb->len + ETH_HLEN;
145 rx_stats->rx_packets++;
146 rx_stats->multicast++;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700147
148 nskb->dev = dev;
Eric Dumazetac067132009-09-01 05:46:05 +0000149 if (!compare_ether_addr_64bits(eth->h_dest, dev->broadcast))
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700150 nskb->pkt_type = PACKET_BROADCAST;
151 else
152 nskb->pkt_type = PACKET_MULTICAST;
153
154 netif_rx(nskb);
155 }
156 }
157}
158
159/* called under rcu_read_lock() from netif_receive_skb */
160static struct sk_buff *macvlan_handle_frame(struct sk_buff *skb)
161{
162 const struct ethhdr *eth = eth_hdr(skb);
163 const struct macvlan_port *port;
164 const struct macvlan_dev *vlan;
165 struct net_device *dev;
Eric Dumazetfccaf712009-11-17 08:53:49 +0000166 struct macvlan_rx_stats *rx_stats;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700167
168 port = rcu_dereference(skb->dev->macvlan_port);
169 if (port == NULL)
170 return skb;
171
172 if (is_multicast_ether_addr(eth->h_dest)) {
173 macvlan_broadcast(skb, port);
174 return skb;
175 }
176
177 vlan = macvlan_hash_lookup(port, eth->h_dest);
178 if (vlan == NULL)
179 return skb;
180
181 dev = vlan->dev;
182 if (unlikely(!(dev->flags & IFF_UP))) {
183 kfree_skb(skb);
184 return NULL;
185 }
Eric Dumazetfccaf712009-11-17 08:53:49 +0000186 rx_stats = per_cpu_ptr(vlan->rx_stats, smp_processor_id());
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700187 skb = skb_share_check(skb, GFP_ATOMIC);
188 if (skb == NULL) {
Eric Dumazetfccaf712009-11-17 08:53:49 +0000189 rx_stats->rx_errors++;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700190 return NULL;
191 }
192
Eric Dumazetfccaf712009-11-17 08:53:49 +0000193 rx_stats->rx_bytes += skb->len + ETH_HLEN;
194 rx_stats->rx_packets++;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700195
196 skb->dev = dev;
197 skb->pkt_type = PACKET_HOST;
198
199 netif_rx(skb);
200 return NULL;
201}
202
Stephen Hemminger424efe92009-08-31 19:50:51 +0000203static netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
204 struct net_device *dev)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700205{
Eric Dumazet2c114552009-09-03 00:11:45 +0000206 int i = skb_get_queue_mapping(skb);
207 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700208 const struct macvlan_dev *vlan = netdev_priv(dev);
209 unsigned int len = skb->len;
210 int ret;
211
212 skb->dev = vlan->lowerdev;
213 ret = dev_queue_xmit(skb);
214
215 if (likely(ret == NET_XMIT_SUCCESS)) {
Eric Dumazet2c114552009-09-03 00:11:45 +0000216 txq->tx_packets++;
217 txq->tx_bytes += len;
218 } else
219 txq->tx_dropped++;
220
Patrick McHardycbbef5e2009-11-10 06:14:24 +0000221 return ret;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700222}
223
224static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700225 unsigned short type, const void *daddr,
226 const void *saddr, unsigned len)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700227{
228 const struct macvlan_dev *vlan = netdev_priv(dev);
229 struct net_device *lowerdev = vlan->lowerdev;
230
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700231 return dev_hard_header(skb, lowerdev, type, daddr,
232 saddr ? : dev->dev_addr, len);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700233}
234
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700235static const struct header_ops macvlan_hard_header_ops = {
236 .create = macvlan_hard_header,
237 .rebuild = eth_rebuild_header,
238 .parse = eth_header_parse,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700239 .cache = eth_header_cache,
240 .cache_update = eth_header_cache_update,
241};
242
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700243static int macvlan_open(struct net_device *dev)
244{
245 struct macvlan_dev *vlan = netdev_priv(dev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700246 struct net_device *lowerdev = vlan->lowerdev;
247 int err;
248
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700249 err = -EBUSY;
250 if (macvlan_addr_busy(vlan->port, dev->dev_addr))
251 goto out;
252
Jiri Pirkoccffad252009-05-22 23:22:17 +0000253 err = dev_unicast_add(lowerdev, dev->dev_addr);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700254 if (err < 0)
Wang Chenb89fb7d2008-07-14 20:57:07 -0700255 goto out;
256 if (dev->flags & IFF_ALLMULTI) {
257 err = dev_set_allmulti(lowerdev, 1);
258 if (err < 0)
259 goto del_unicast;
260 }
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700261 macvlan_hash_add(vlan);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700262 return 0;
Wang Chenb89fb7d2008-07-14 20:57:07 -0700263
264del_unicast:
Jiri Pirkoccffad252009-05-22 23:22:17 +0000265 dev_unicast_delete(lowerdev, dev->dev_addr);
Wang Chenb89fb7d2008-07-14 20:57:07 -0700266out:
267 return err;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700268}
269
270static int macvlan_stop(struct net_device *dev)
271{
272 struct macvlan_dev *vlan = netdev_priv(dev);
273 struct net_device *lowerdev = vlan->lowerdev;
274
275 dev_mc_unsync(lowerdev, dev);
276 if (dev->flags & IFF_ALLMULTI)
277 dev_set_allmulti(lowerdev, -1);
278
Jiri Pirkoccffad252009-05-22 23:22:17 +0000279 dev_unicast_delete(lowerdev, dev->dev_addr);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700280
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700281 macvlan_hash_del(vlan);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700282 return 0;
283}
284
Patrick McHardyad5d20a2007-11-19 22:00:42 -0800285static int macvlan_set_mac_address(struct net_device *dev, void *p)
286{
287 struct macvlan_dev *vlan = netdev_priv(dev);
288 struct net_device *lowerdev = vlan->lowerdev;
289 struct sockaddr *addr = p;
290 int err;
291
292 if (!is_valid_ether_addr(addr->sa_data))
293 return -EADDRNOTAVAIL;
294
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700295 if (!(dev->flags & IFF_UP)) {
296 /* Just copy in the new address */
297 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
298 } else {
299 /* Rehash and update the device filters */
300 if (macvlan_addr_busy(vlan->port, addr->sa_data))
301 return -EBUSY;
Patrick McHardyad5d20a2007-11-19 22:00:42 -0800302
Jiri Pirkoccffad252009-05-22 23:22:17 +0000303 err = dev_unicast_add(lowerdev, addr->sa_data);
304 if (err)
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700305 return err;
Patrick McHardyad5d20a2007-11-19 22:00:42 -0800306
Jiri Pirkoccffad252009-05-22 23:22:17 +0000307 dev_unicast_delete(lowerdev, dev->dev_addr);
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700308
309 macvlan_hash_change_addr(vlan, addr->sa_data);
310 }
Patrick McHardyad5d20a2007-11-19 22:00:42 -0800311 return 0;
312}
313
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700314static void macvlan_change_rx_flags(struct net_device *dev, int change)
315{
316 struct macvlan_dev *vlan = netdev_priv(dev);
317 struct net_device *lowerdev = vlan->lowerdev;
318
319 if (change & IFF_ALLMULTI)
320 dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
321}
322
323static void macvlan_set_multicast_list(struct net_device *dev)
324{
325 struct macvlan_dev *vlan = netdev_priv(dev);
326
327 dev_mc_sync(vlan->lowerdev, dev);
328}
329
330static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
331{
332 struct macvlan_dev *vlan = netdev_priv(dev);
333
334 if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
335 return -EINVAL;
336 dev->mtu = new_mtu;
337 return 0;
338}
339
340/*
341 * macvlan network devices have devices nesting below it and are a special
342 * "super class" of normal network devices; split their locks off into a
343 * separate class since they always nest.
344 */
345static struct lock_class_key macvlan_netdev_xmit_lock_key;
David S. Millercf508b12008-07-22 14:16:42 -0700346static struct lock_class_key macvlan_netdev_addr_lock_key;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700347
348#define MACVLAN_FEATURES \
349 (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
350 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
351 NETIF_F_TSO_ECN | NETIF_F_TSO6)
352
353#define MACVLAN_STATE_MASK \
354 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
355
David S. Millere8a04642008-07-17 00:34:19 -0700356static void macvlan_set_lockdep_class_one(struct net_device *dev,
357 struct netdev_queue *txq,
358 void *_unused)
David S. Millerc773e842008-07-08 23:13:53 -0700359{
360 lockdep_set_class(&txq->_xmit_lock,
361 &macvlan_netdev_xmit_lock_key);
362}
363
364static void macvlan_set_lockdep_class(struct net_device *dev)
365{
David S. Millercf508b12008-07-22 14:16:42 -0700366 lockdep_set_class(&dev->addr_list_lock,
367 &macvlan_netdev_addr_lock_key);
David S. Millere8a04642008-07-17 00:34:19 -0700368 netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL);
David S. Millerc773e842008-07-08 23:13:53 -0700369}
370
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700371static int macvlan_init(struct net_device *dev)
372{
373 struct macvlan_dev *vlan = netdev_priv(dev);
374 const struct net_device *lowerdev = vlan->lowerdev;
375
376 dev->state = (dev->state & ~MACVLAN_STATE_MASK) |
377 (lowerdev->state & MACVLAN_STATE_MASK);
378 dev->features = lowerdev->features & MACVLAN_FEATURES;
379 dev->iflink = lowerdev->ifindex;
sg.tweak@gmail.comef5c8992009-06-10 09:55:02 +0000380 dev->hard_header_len = lowerdev->hard_header_len;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700381
David S. Millerc773e842008-07-08 23:13:53 -0700382 macvlan_set_lockdep_class(dev);
383
Eric Dumazetfccaf712009-11-17 08:53:49 +0000384 vlan->rx_stats = alloc_percpu(struct macvlan_rx_stats);
385 if (!vlan->rx_stats)
386 return -ENOMEM;
387
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700388 return 0;
389}
390
Eric Dumazetfccaf712009-11-17 08:53:49 +0000391static void macvlan_uninit(struct net_device *dev)
392{
393 struct macvlan_dev *vlan = netdev_priv(dev);
394
395 free_percpu(vlan->rx_stats);
396}
397
398static struct net_device_stats *macvlan_dev_get_stats(struct net_device *dev)
399{
400 struct net_device_stats *stats = &dev->stats;
401 struct macvlan_dev *vlan = netdev_priv(dev);
402
403 dev_txq_stats_fold(dev, stats);
404
405 if (vlan->rx_stats) {
406 struct macvlan_rx_stats *p, rx = {0};
407 int i;
408
409 for_each_possible_cpu(i) {
410 p = per_cpu_ptr(vlan->rx_stats, i);
411 rx.rx_packets += p->rx_packets;
412 rx.rx_bytes += p->rx_bytes;
413 rx.rx_errors += p->rx_errors;
414 rx.multicast += p->multicast;
415 }
416 stats->rx_packets = rx.rx_packets;
417 stats->rx_bytes = rx.rx_bytes;
418 stats->rx_errors = rx.rx_errors;
419 stats->rx_dropped = rx.rx_errors;
420 stats->multicast = rx.multicast;
421 }
422 return stats;
423}
424
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700425static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
426 struct ethtool_drvinfo *drvinfo)
427{
428 snprintf(drvinfo->driver, 32, "macvlan");
429 snprintf(drvinfo->version, 32, "0.1");
430}
431
432static u32 macvlan_ethtool_get_rx_csum(struct net_device *dev)
433{
434 const struct macvlan_dev *vlan = netdev_priv(dev);
Patrick McHardyb1b67dd2009-04-20 04:49:28 +0000435 return dev_ethtool_get_rx_csum(vlan->lowerdev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700436}
437
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700438static int macvlan_ethtool_get_settings(struct net_device *dev,
439 struct ethtool_cmd *cmd)
440{
441 const struct macvlan_dev *vlan = netdev_priv(dev);
Patrick McHardyb1b67dd2009-04-20 04:49:28 +0000442 return dev_ethtool_get_settings(vlan->lowerdev, cmd);
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700443}
444
445static u32 macvlan_ethtool_get_flags(struct net_device *dev)
446{
447 const struct macvlan_dev *vlan = netdev_priv(dev);
Patrick McHardyb1b67dd2009-04-20 04:49:28 +0000448 return dev_ethtool_get_flags(vlan->lowerdev);
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700449}
450
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700451static const struct ethtool_ops macvlan_ethtool_ops = {
452 .get_link = ethtool_op_get_link,
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700453 .get_settings = macvlan_ethtool_get_settings,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700454 .get_rx_csum = macvlan_ethtool_get_rx_csum,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700455 .get_drvinfo = macvlan_ethtool_get_drvinfo,
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700456 .get_flags = macvlan_ethtool_get_flags,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700457};
458
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800459static const struct net_device_ops macvlan_netdev_ops = {
460 .ndo_init = macvlan_init,
Eric Dumazetfccaf712009-11-17 08:53:49 +0000461 .ndo_uninit = macvlan_uninit,
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800462 .ndo_open = macvlan_open,
463 .ndo_stop = macvlan_stop,
Stephen Hemminger00829822008-11-20 20:14:53 -0800464 .ndo_start_xmit = macvlan_start_xmit,
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800465 .ndo_change_mtu = macvlan_change_mtu,
466 .ndo_change_rx_flags = macvlan_change_rx_flags,
467 .ndo_set_mac_address = macvlan_set_mac_address,
468 .ndo_set_multicast_list = macvlan_set_multicast_list,
Eric Dumazetfccaf712009-11-17 08:53:49 +0000469 .ndo_get_stats = macvlan_dev_get_stats,
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800470 .ndo_validate_addr = eth_validate_addr,
471};
472
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700473static void macvlan_setup(struct net_device *dev)
474{
475 ether_setup(dev);
476
Eric Dumazet93f154b2009-05-18 22:19:19 -0700477 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800478 dev->netdev_ops = &macvlan_netdev_ops;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700479 dev->destructor = free_netdev;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700480 dev->header_ops = &macvlan_hard_header_ops,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700481 dev->ethtool_ops = &macvlan_ethtool_ops;
482 dev->tx_queue_len = 0;
483}
484
485static int macvlan_port_create(struct net_device *dev)
486{
487 struct macvlan_port *port;
488 unsigned int i;
489
490 if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
491 return -EINVAL;
492
493 port = kzalloc(sizeof(*port), GFP_KERNEL);
494 if (port == NULL)
495 return -ENOMEM;
496
497 port->dev = dev;
498 INIT_LIST_HEAD(&port->vlans);
499 for (i = 0; i < MACVLAN_HASH_SIZE; i++)
500 INIT_HLIST_HEAD(&port->vlan_hash[i]);
501 rcu_assign_pointer(dev->macvlan_port, port);
502 return 0;
503}
504
505static void macvlan_port_destroy(struct net_device *dev)
506{
507 struct macvlan_port *port = dev->macvlan_port;
508
509 rcu_assign_pointer(dev->macvlan_port, NULL);
510 synchronize_rcu();
511 kfree(port);
512}
513
514static void macvlan_transfer_operstate(struct net_device *dev)
515{
516 struct macvlan_dev *vlan = netdev_priv(dev);
517 const struct net_device *lowerdev = vlan->lowerdev;
518
519 if (lowerdev->operstate == IF_OPER_DORMANT)
520 netif_dormant_on(dev);
521 else
522 netif_dormant_off(dev);
523
524 if (netif_carrier_ok(lowerdev)) {
525 if (!netif_carrier_ok(dev))
526 netif_carrier_on(dev);
527 } else {
Patrick McHardyf12ca5f2008-01-21 00:47:08 -0800528 if (netif_carrier_ok(dev))
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700529 netif_carrier_off(dev);
530 }
531}
532
533static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
534{
535 if (tb[IFLA_ADDRESS]) {
536 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
537 return -EINVAL;
538 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
539 return -EADDRNOTAVAIL;
540 }
541 return 0;
542}
543
Eric Dumazet2c114552009-09-03 00:11:45 +0000544static int macvlan_get_tx_queues(struct net *net,
545 struct nlattr *tb[],
546 unsigned int *num_tx_queues,
547 unsigned int *real_num_tx_queues)
548{
549 struct net_device *real_dev;
550
551 if (!tb[IFLA_LINK])
552 return -EINVAL;
553
554 real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
555 if (!real_dev)
556 return -ENODEV;
557
558 *num_tx_queues = real_dev->num_tx_queues;
559 *real_num_tx_queues = real_dev->real_num_tx_queues;
560 return 0;
561}
562
Eric W. Biederman81adee42009-11-08 00:53:51 -0800563static int macvlan_newlink(struct net *src_net, struct net_device *dev,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700564 struct nlattr *tb[], struct nlattr *data[])
565{
566 struct macvlan_dev *vlan = netdev_priv(dev);
567 struct macvlan_port *port;
568 struct net_device *lowerdev;
569 int err;
570
571 if (!tb[IFLA_LINK])
572 return -EINVAL;
573
Eric W. Biederman81adee42009-11-08 00:53:51 -0800574 lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700575 if (lowerdev == NULL)
576 return -ENODEV;
577
Eric Biedermanb0832a22009-03-13 13:15:37 -0700578 /* When creating macvlans on top of other macvlans - use
579 * the real device as the lowerdev.
Patrick McHardya6ca5f12008-01-10 22:39:28 -0800580 */
Eric Biedermanb0832a22009-03-13 13:15:37 -0700581 if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) {
582 struct macvlan_dev *lowervlan = netdev_priv(lowerdev);
583 lowerdev = lowervlan->lowerdev;
584 }
Patrick McHardya6ca5f12008-01-10 22:39:28 -0800585
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700586 if (!tb[IFLA_MTU])
587 dev->mtu = lowerdev->mtu;
588 else if (dev->mtu > lowerdev->mtu)
589 return -EINVAL;
590
591 if (!tb[IFLA_ADDRESS])
592 random_ether_addr(dev->dev_addr);
593
594 if (lowerdev->macvlan_port == NULL) {
595 err = macvlan_port_create(lowerdev);
596 if (err < 0)
597 return err;
598 }
599 port = lowerdev->macvlan_port;
600
601 vlan->lowerdev = lowerdev;
602 vlan->dev = dev;
603 vlan->port = port;
604
605 err = register_netdevice(dev);
606 if (err < 0)
607 return err;
608
609 list_add_tail(&vlan->list, &port->vlans);
610 macvlan_transfer_operstate(dev);
611 return 0;
612}
613
Eric Dumazet23289a32009-10-27 07:06:36 +0000614static void macvlan_dellink(struct net_device *dev, struct list_head *head)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700615{
616 struct macvlan_dev *vlan = netdev_priv(dev);
617 struct macvlan_port *port = vlan->port;
618
619 list_del(&vlan->list);
Eric Dumazet23289a32009-10-27 07:06:36 +0000620 unregister_netdevice_queue(dev, head);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700621
622 if (list_empty(&port->vlans))
Patrick McHardy73120962008-05-08 01:13:31 -0700623 macvlan_port_destroy(port->dev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700624}
625
626static struct rtnl_link_ops macvlan_link_ops __read_mostly = {
627 .kind = "macvlan",
628 .priv_size = sizeof(struct macvlan_dev),
Eric Dumazet2c114552009-09-03 00:11:45 +0000629 .get_tx_queues = macvlan_get_tx_queues,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700630 .setup = macvlan_setup,
631 .validate = macvlan_validate,
632 .newlink = macvlan_newlink,
633 .dellink = macvlan_dellink,
634};
635
636static int macvlan_device_event(struct notifier_block *unused,
637 unsigned long event, void *ptr)
638{
639 struct net_device *dev = ptr;
640 struct macvlan_dev *vlan, *next;
641 struct macvlan_port *port;
642
643 port = dev->macvlan_port;
644 if (port == NULL)
645 return NOTIFY_DONE;
646
647 switch (event) {
648 case NETDEV_CHANGE:
649 list_for_each_entry(vlan, &port->vlans, list)
650 macvlan_transfer_operstate(vlan->dev);
651 break;
652 case NETDEV_FEAT_CHANGE:
653 list_for_each_entry(vlan, &port->vlans, list) {
654 vlan->dev->features = dev->features & MACVLAN_FEATURES;
655 netdev_features_change(vlan->dev);
656 }
657 break;
658 case NETDEV_UNREGISTER:
659 list_for_each_entry_safe(vlan, next, &port->vlans, list)
Eric Dumazet23289a32009-10-27 07:06:36 +0000660 macvlan_dellink(vlan->dev, NULL);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700661 break;
662 }
663 return NOTIFY_DONE;
664}
665
666static struct notifier_block macvlan_notifier_block __read_mostly = {
667 .notifier_call = macvlan_device_event,
668};
669
670static int __init macvlan_init_module(void)
671{
672 int err;
673
674 register_netdevice_notifier(&macvlan_notifier_block);
675 macvlan_handle_frame_hook = macvlan_handle_frame;
676
677 err = rtnl_link_register(&macvlan_link_ops);
678 if (err < 0)
679 goto err1;
680 return 0;
681err1:
Rami Rosen52913242008-01-31 16:56:03 -0800682 macvlan_handle_frame_hook = NULL;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700683 unregister_netdevice_notifier(&macvlan_notifier_block);
684 return err;
685}
686
687static void __exit macvlan_cleanup_module(void)
688{
689 rtnl_link_unregister(&macvlan_link_ops);
690 macvlan_handle_frame_hook = NULL;
691 unregister_netdevice_notifier(&macvlan_notifier_block);
692}
693
694module_init(macvlan_init_module);
695module_exit(macvlan_cleanup_module);
696
697MODULE_LICENSE("GPL");
698MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
699MODULE_DESCRIPTION("Driver for MAC address based VLANs");
700MODULE_ALIAS_RTNL_LINK("macvlan");