blob: fa0dc514dbaf635d583a773d608e2dff94f11a84 [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>
Arnd Bergmann618e1b72009-11-26 06:07:10 +000032#include <net/xfrm.h>
Patrick McHardyb863ceb2007-07-14 18:55:06 -070033
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;
40};
41
Eric Dumazetfccaf712009-11-17 08:53:49 +000042/**
43 * struct macvlan_rx_stats - MACVLAN percpu rx stats
44 * @rx_packets: number of received packets
45 * @rx_bytes: number of received bytes
46 * @multicast: number of received multicast packets
47 * @rx_errors: number of errors
48 */
49struct macvlan_rx_stats {
50 unsigned long rx_packets;
51 unsigned long rx_bytes;
52 unsigned long multicast;
53 unsigned long rx_errors;
54};
55
Patrick McHardyb863ceb2007-07-14 18:55:06 -070056struct macvlan_dev {
57 struct net_device *dev;
58 struct list_head list;
59 struct hlist_node hlist;
60 struct macvlan_port *port;
61 struct net_device *lowerdev;
Eric Dumazetfccaf712009-11-17 08:53:49 +000062 struct macvlan_rx_stats *rx_stats;
Arnd Bergmann618e1b72009-11-26 06:07:10 +000063 enum macvlan_mode mode;
Patrick McHardyb863ceb2007-07-14 18:55:06 -070064};
65
66
67static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
68 const unsigned char *addr)
69{
70 struct macvlan_dev *vlan;
71 struct hlist_node *n;
72
73 hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[addr[5]], hlist) {
Eric Dumazetac067132009-09-01 05:46:05 +000074 if (!compare_ether_addr_64bits(vlan->dev->dev_addr, addr))
Patrick McHardyb863ceb2007-07-14 18:55:06 -070075 return vlan;
76 }
77 return NULL;
78}
79
Eric Biedermanf9ac30f2009-03-13 13:16:13 -070080static void macvlan_hash_add(struct macvlan_dev *vlan)
81{
82 struct macvlan_port *port = vlan->port;
83 const unsigned char *addr = vlan->dev->dev_addr;
84
85 hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]);
86}
87
88static void macvlan_hash_del(struct macvlan_dev *vlan)
89{
90 hlist_del_rcu(&vlan->hlist);
91 synchronize_rcu();
92}
93
94static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
95 const unsigned char *addr)
96{
97 macvlan_hash_del(vlan);
98 /* Now that we are unhashed it is safe to change the device
99 * address without confusing packet delivery.
100 */
101 memcpy(vlan->dev->dev_addr, addr, ETH_ALEN);
102 macvlan_hash_add(vlan);
103}
104
105static int macvlan_addr_busy(const struct macvlan_port *port,
106 const unsigned char *addr)
107{
108 /* Test to see if the specified multicast address is
109 * currently in use by the underlying device or
110 * another macvlan.
111 */
Eric Dumazetac067132009-09-01 05:46:05 +0000112 if (!compare_ether_addr_64bits(port->dev->dev_addr, addr))
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700113 return 1;
114
115 if (macvlan_hash_lookup(port, addr))
116 return 1;
117
118 return 0;
119}
120
Arnd Bergmanna1e514c2009-11-26 06:07:09 +0000121static inline void macvlan_count_rx(const struct macvlan_dev *vlan,
122 unsigned int len, bool success,
123 bool multicast)
124{
125 struct macvlan_rx_stats *rx_stats;
126
127 rx_stats = per_cpu_ptr(vlan->rx_stats, smp_processor_id());
128 if (likely(success)) {
129 rx_stats->rx_packets++;;
130 rx_stats->rx_bytes += len;
131 if (multicast)
132 rx_stats->multicast++;
133 } else {
134 rx_stats->rx_errors++;
135 }
136}
137
138static int macvlan_broadcast_one(struct sk_buff *skb, struct net_device *dev,
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000139 const struct ethhdr *eth, bool local)
Arnd Bergmanna1e514c2009-11-26 06:07:09 +0000140{
141 if (!skb)
142 return NET_RX_DROP;
143
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000144 if (local)
145 return dev_forward_skb(dev, skb);
146
Arnd Bergmanna1e514c2009-11-26 06:07:09 +0000147 skb->dev = dev;
148 if (!compare_ether_addr_64bits(eth->h_dest,
149 dev->broadcast))
150 skb->pkt_type = PACKET_BROADCAST;
151 else
152 skb->pkt_type = PACKET_MULTICAST;
153
154 return netif_rx(skb);
155}
156
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700157static void macvlan_broadcast(struct sk_buff *skb,
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000158 const struct macvlan_port *port,
159 struct net_device *src,
160 enum macvlan_mode mode)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700161{
162 const struct ethhdr *eth = eth_hdr(skb);
163 const struct macvlan_dev *vlan;
164 struct hlist_node *n;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700165 struct sk_buff *nskb;
166 unsigned int i;
Arnd Bergmanna1e514c2009-11-26 06:07:09 +0000167 int err;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700168
Patrick McHardyefbbced2008-11-26 15:30:48 -0800169 if (skb->protocol == htons(ETH_P_PAUSE))
170 return;
171
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700172 for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
173 hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[i], hlist) {
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000174 if (vlan->dev == src || !(vlan->mode & mode))
175 continue;
176
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700177 nskb = skb_clone(skb, GFP_ATOMIC);
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000178 err = macvlan_broadcast_one(nskb, vlan->dev, eth,
179 mode == MACVLAN_MODE_BRIDGE);
Arnd Bergmanna1e514c2009-11-26 06:07:09 +0000180 macvlan_count_rx(vlan, skb->len + ETH_HLEN,
181 err == NET_RX_SUCCESS, 1);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700182 }
183 }
184}
185
186/* called under rcu_read_lock() from netif_receive_skb */
187static struct sk_buff *macvlan_handle_frame(struct sk_buff *skb)
188{
189 const struct ethhdr *eth = eth_hdr(skb);
190 const struct macvlan_port *port;
191 const struct macvlan_dev *vlan;
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000192 const struct macvlan_dev *src;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700193 struct net_device *dev;
Arnd Bergmanna1e514c2009-11-26 06:07:09 +0000194 unsigned int len;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700195
196 port = rcu_dereference(skb->dev->macvlan_port);
197 if (port == NULL)
198 return skb;
199
200 if (is_multicast_ether_addr(eth->h_dest)) {
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000201 src = macvlan_hash_lookup(port, eth->h_source);
202 if (!src)
203 /* frame comes from an external address */
204 macvlan_broadcast(skb, port, NULL,
205 MACVLAN_MODE_PRIVATE |
206 MACVLAN_MODE_VEPA |
207 MACVLAN_MODE_BRIDGE);
208 else if (src->mode == MACVLAN_MODE_VEPA)
209 /* flood to everyone except source */
210 macvlan_broadcast(skb, port, src->dev,
211 MACVLAN_MODE_VEPA |
212 MACVLAN_MODE_BRIDGE);
213 else if (src->mode == MACVLAN_MODE_BRIDGE)
214 /*
215 * flood only to VEPA ports, bridge ports
216 * already saw the frame on the way out.
217 */
218 macvlan_broadcast(skb, port, src->dev,
219 MACVLAN_MODE_VEPA);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700220 return skb;
221 }
222
223 vlan = macvlan_hash_lookup(port, eth->h_dest);
224 if (vlan == NULL)
225 return skb;
226
227 dev = vlan->dev;
228 if (unlikely(!(dev->flags & IFF_UP))) {
229 kfree_skb(skb);
230 return NULL;
231 }
Arnd Bergmanna1e514c2009-11-26 06:07:09 +0000232 len = skb->len + ETH_HLEN;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700233 skb = skb_share_check(skb, GFP_ATOMIC);
Arnd Bergmanna1e514c2009-11-26 06:07:09 +0000234 macvlan_count_rx(vlan, len, skb != NULL, 0);
235 if (!skb)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700236 return NULL;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700237
238 skb->dev = dev;
239 skb->pkt_type = PACKET_HOST;
240
241 netif_rx(skb);
242 return NULL;
243}
244
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000245static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
246{
247 const struct macvlan_dev *vlan = netdev_priv(dev);
248 const struct macvlan_port *port = vlan->port;
249 const struct macvlan_dev *dest;
250
251 if (vlan->mode == MACVLAN_MODE_BRIDGE) {
252 const struct ethhdr *eth = (void *)skb->data;
253
254 /* send to other bridge ports directly */
255 if (is_multicast_ether_addr(eth->h_dest)) {
256 macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE);
257 goto xmit_world;
258 }
259
260 dest = macvlan_hash_lookup(port, eth->h_dest);
261 if (dest && dest->mode == MACVLAN_MODE_BRIDGE) {
262 unsigned int length = skb->len + ETH_HLEN;
263 int ret = dev_forward_skb(dest->dev, skb);
264 macvlan_count_rx(dest, length,
265 ret == NET_RX_SUCCESS, 0);
266
267 return NET_XMIT_SUCCESS;
268 }
269 }
270
271xmit_world:
272 skb->dev = vlan->lowerdev;
273 return dev_queue_xmit(skb);
274}
275
Stephen Hemminger424efe92009-08-31 19:50:51 +0000276static netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
277 struct net_device *dev)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700278{
Eric Dumazet2c114552009-09-03 00:11:45 +0000279 int i = skb_get_queue_mapping(skb);
280 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700281 unsigned int len = skb->len;
282 int ret;
283
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000284 ret = macvlan_queue_xmit(skb, dev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700285 if (likely(ret == NET_XMIT_SUCCESS)) {
Eric Dumazet2c114552009-09-03 00:11:45 +0000286 txq->tx_packets++;
287 txq->tx_bytes += len;
288 } else
289 txq->tx_dropped++;
290
Patrick McHardycbbef5e2009-11-10 06:14:24 +0000291 return ret;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700292}
293
294static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700295 unsigned short type, const void *daddr,
296 const void *saddr, unsigned len)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700297{
298 const struct macvlan_dev *vlan = netdev_priv(dev);
299 struct net_device *lowerdev = vlan->lowerdev;
300
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700301 return dev_hard_header(skb, lowerdev, type, daddr,
302 saddr ? : dev->dev_addr, len);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700303}
304
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700305static const struct header_ops macvlan_hard_header_ops = {
306 .create = macvlan_hard_header,
307 .rebuild = eth_rebuild_header,
308 .parse = eth_header_parse,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700309 .cache = eth_header_cache,
310 .cache_update = eth_header_cache_update,
311};
312
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700313static int macvlan_open(struct net_device *dev)
314{
315 struct macvlan_dev *vlan = netdev_priv(dev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700316 struct net_device *lowerdev = vlan->lowerdev;
317 int err;
318
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700319 err = -EBUSY;
320 if (macvlan_addr_busy(vlan->port, dev->dev_addr))
321 goto out;
322
Jiri Pirkoccffad22009-05-22 23:22:17 +0000323 err = dev_unicast_add(lowerdev, dev->dev_addr);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700324 if (err < 0)
Wang Chenb89fb7d2008-07-14 20:57:07 -0700325 goto out;
326 if (dev->flags & IFF_ALLMULTI) {
327 err = dev_set_allmulti(lowerdev, 1);
328 if (err < 0)
329 goto del_unicast;
330 }
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700331 macvlan_hash_add(vlan);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700332 return 0;
Wang Chenb89fb7d2008-07-14 20:57:07 -0700333
334del_unicast:
Jiri Pirkoccffad22009-05-22 23:22:17 +0000335 dev_unicast_delete(lowerdev, dev->dev_addr);
Wang Chenb89fb7d2008-07-14 20:57:07 -0700336out:
337 return err;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700338}
339
340static int macvlan_stop(struct net_device *dev)
341{
342 struct macvlan_dev *vlan = netdev_priv(dev);
343 struct net_device *lowerdev = vlan->lowerdev;
344
345 dev_mc_unsync(lowerdev, dev);
346 if (dev->flags & IFF_ALLMULTI)
347 dev_set_allmulti(lowerdev, -1);
348
Jiri Pirkoccffad22009-05-22 23:22:17 +0000349 dev_unicast_delete(lowerdev, dev->dev_addr);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700350
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700351 macvlan_hash_del(vlan);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700352 return 0;
353}
354
Patrick McHardyad5d20a2007-11-19 22:00:42 -0800355static int macvlan_set_mac_address(struct net_device *dev, void *p)
356{
357 struct macvlan_dev *vlan = netdev_priv(dev);
358 struct net_device *lowerdev = vlan->lowerdev;
359 struct sockaddr *addr = p;
360 int err;
361
362 if (!is_valid_ether_addr(addr->sa_data))
363 return -EADDRNOTAVAIL;
364
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700365 if (!(dev->flags & IFF_UP)) {
366 /* Just copy in the new address */
367 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
368 } else {
369 /* Rehash and update the device filters */
370 if (macvlan_addr_busy(vlan->port, addr->sa_data))
371 return -EBUSY;
Patrick McHardyad5d20a2007-11-19 22:00:42 -0800372
Jiri Pirkoccffad22009-05-22 23:22:17 +0000373 err = dev_unicast_add(lowerdev, addr->sa_data);
374 if (err)
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700375 return err;
Patrick McHardyad5d20a2007-11-19 22:00:42 -0800376
Jiri Pirkoccffad22009-05-22 23:22:17 +0000377 dev_unicast_delete(lowerdev, dev->dev_addr);
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700378
379 macvlan_hash_change_addr(vlan, addr->sa_data);
380 }
Patrick McHardyad5d20a2007-11-19 22:00:42 -0800381 return 0;
382}
383
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700384static void macvlan_change_rx_flags(struct net_device *dev, int change)
385{
386 struct macvlan_dev *vlan = netdev_priv(dev);
387 struct net_device *lowerdev = vlan->lowerdev;
388
389 if (change & IFF_ALLMULTI)
390 dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
391}
392
393static void macvlan_set_multicast_list(struct net_device *dev)
394{
395 struct macvlan_dev *vlan = netdev_priv(dev);
396
397 dev_mc_sync(vlan->lowerdev, dev);
398}
399
400static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
401{
402 struct macvlan_dev *vlan = netdev_priv(dev);
403
404 if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
405 return -EINVAL;
406 dev->mtu = new_mtu;
407 return 0;
408}
409
410/*
411 * macvlan network devices have devices nesting below it and are a special
412 * "super class" of normal network devices; split their locks off into a
413 * separate class since they always nest.
414 */
415static struct lock_class_key macvlan_netdev_xmit_lock_key;
David S. Millercf508b12008-07-22 14:16:42 -0700416static struct lock_class_key macvlan_netdev_addr_lock_key;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700417
418#define MACVLAN_FEATURES \
419 (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
420 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
Patrick Mullaney6eb3a852010-01-16 01:05:38 -0800421 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700422
423#define MACVLAN_STATE_MASK \
424 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
425
David S. Millere8a04642008-07-17 00:34:19 -0700426static void macvlan_set_lockdep_class_one(struct net_device *dev,
427 struct netdev_queue *txq,
428 void *_unused)
David S. Millerc773e842008-07-08 23:13:53 -0700429{
430 lockdep_set_class(&txq->_xmit_lock,
431 &macvlan_netdev_xmit_lock_key);
432}
433
434static void macvlan_set_lockdep_class(struct net_device *dev)
435{
David S. Millercf508b12008-07-22 14:16:42 -0700436 lockdep_set_class(&dev->addr_list_lock,
437 &macvlan_netdev_addr_lock_key);
David S. Millere8a04642008-07-17 00:34:19 -0700438 netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL);
David S. Millerc773e842008-07-08 23:13:53 -0700439}
440
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700441static int macvlan_init(struct net_device *dev)
442{
443 struct macvlan_dev *vlan = netdev_priv(dev);
444 const struct net_device *lowerdev = vlan->lowerdev;
445
446 dev->state = (dev->state & ~MACVLAN_STATE_MASK) |
447 (lowerdev->state & MACVLAN_STATE_MASK);
448 dev->features = lowerdev->features & MACVLAN_FEATURES;
Patrick McHardy8c2acc52009-11-23 14:18:53 -0800449 dev->gso_max_size = lowerdev->gso_max_size;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700450 dev->iflink = lowerdev->ifindex;
sg.tweak@gmail.comef5c8992009-06-10 09:55:02 +0000451 dev->hard_header_len = lowerdev->hard_header_len;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700452
David S. Millerc773e842008-07-08 23:13:53 -0700453 macvlan_set_lockdep_class(dev);
454
Eric Dumazetfccaf712009-11-17 08:53:49 +0000455 vlan->rx_stats = alloc_percpu(struct macvlan_rx_stats);
456 if (!vlan->rx_stats)
457 return -ENOMEM;
458
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700459 return 0;
460}
461
Eric Dumazetfccaf712009-11-17 08:53:49 +0000462static void macvlan_uninit(struct net_device *dev)
463{
464 struct macvlan_dev *vlan = netdev_priv(dev);
465
466 free_percpu(vlan->rx_stats);
467}
468
469static struct net_device_stats *macvlan_dev_get_stats(struct net_device *dev)
470{
471 struct net_device_stats *stats = &dev->stats;
472 struct macvlan_dev *vlan = netdev_priv(dev);
473
474 dev_txq_stats_fold(dev, stats);
475
476 if (vlan->rx_stats) {
477 struct macvlan_rx_stats *p, rx = {0};
478 int i;
479
480 for_each_possible_cpu(i) {
481 p = per_cpu_ptr(vlan->rx_stats, i);
482 rx.rx_packets += p->rx_packets;
483 rx.rx_bytes += p->rx_bytes;
484 rx.rx_errors += p->rx_errors;
485 rx.multicast += p->multicast;
486 }
487 stats->rx_packets = rx.rx_packets;
488 stats->rx_bytes = rx.rx_bytes;
489 stats->rx_errors = rx.rx_errors;
490 stats->rx_dropped = rx.rx_errors;
491 stats->multicast = rx.multicast;
492 }
493 return stats;
494}
495
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700496static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
497 struct ethtool_drvinfo *drvinfo)
498{
499 snprintf(drvinfo->driver, 32, "macvlan");
500 snprintf(drvinfo->version, 32, "0.1");
501}
502
503static u32 macvlan_ethtool_get_rx_csum(struct net_device *dev)
504{
505 const struct macvlan_dev *vlan = netdev_priv(dev);
Patrick McHardyb1b67dd2009-04-20 04:49:28 +0000506 return dev_ethtool_get_rx_csum(vlan->lowerdev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700507}
508
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700509static int macvlan_ethtool_get_settings(struct net_device *dev,
510 struct ethtool_cmd *cmd)
511{
512 const struct macvlan_dev *vlan = netdev_priv(dev);
Patrick McHardyb1b67dd2009-04-20 04:49:28 +0000513 return dev_ethtool_get_settings(vlan->lowerdev, cmd);
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700514}
515
516static u32 macvlan_ethtool_get_flags(struct net_device *dev)
517{
518 const struct macvlan_dev *vlan = netdev_priv(dev);
Patrick McHardyb1b67dd2009-04-20 04:49:28 +0000519 return dev_ethtool_get_flags(vlan->lowerdev);
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700520}
521
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700522static const struct ethtool_ops macvlan_ethtool_ops = {
523 .get_link = ethtool_op_get_link,
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700524 .get_settings = macvlan_ethtool_get_settings,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700525 .get_rx_csum = macvlan_ethtool_get_rx_csum,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700526 .get_drvinfo = macvlan_ethtool_get_drvinfo,
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700527 .get_flags = macvlan_ethtool_get_flags,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700528};
529
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800530static const struct net_device_ops macvlan_netdev_ops = {
531 .ndo_init = macvlan_init,
Eric Dumazetfccaf712009-11-17 08:53:49 +0000532 .ndo_uninit = macvlan_uninit,
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800533 .ndo_open = macvlan_open,
534 .ndo_stop = macvlan_stop,
Stephen Hemminger00829822008-11-20 20:14:53 -0800535 .ndo_start_xmit = macvlan_start_xmit,
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800536 .ndo_change_mtu = macvlan_change_mtu,
537 .ndo_change_rx_flags = macvlan_change_rx_flags,
538 .ndo_set_mac_address = macvlan_set_mac_address,
539 .ndo_set_multicast_list = macvlan_set_multicast_list,
Eric Dumazetfccaf712009-11-17 08:53:49 +0000540 .ndo_get_stats = macvlan_dev_get_stats,
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800541 .ndo_validate_addr = eth_validate_addr,
542};
543
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700544static void macvlan_setup(struct net_device *dev)
545{
546 ether_setup(dev);
547
Eric Dumazet93f154b2009-05-18 22:19:19 -0700548 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800549 dev->netdev_ops = &macvlan_netdev_ops;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700550 dev->destructor = free_netdev;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700551 dev->header_ops = &macvlan_hard_header_ops,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700552 dev->ethtool_ops = &macvlan_ethtool_ops;
553 dev->tx_queue_len = 0;
554}
555
556static int macvlan_port_create(struct net_device *dev)
557{
558 struct macvlan_port *port;
559 unsigned int i;
560
561 if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
562 return -EINVAL;
563
564 port = kzalloc(sizeof(*port), GFP_KERNEL);
565 if (port == NULL)
566 return -ENOMEM;
567
568 port->dev = dev;
569 INIT_LIST_HEAD(&port->vlans);
570 for (i = 0; i < MACVLAN_HASH_SIZE; i++)
571 INIT_HLIST_HEAD(&port->vlan_hash[i]);
572 rcu_assign_pointer(dev->macvlan_port, port);
573 return 0;
574}
575
576static void macvlan_port_destroy(struct net_device *dev)
577{
578 struct macvlan_port *port = dev->macvlan_port;
579
580 rcu_assign_pointer(dev->macvlan_port, NULL);
581 synchronize_rcu();
582 kfree(port);
583}
584
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700585static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
586{
587 if (tb[IFLA_ADDRESS]) {
588 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
589 return -EINVAL;
590 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
591 return -EADDRNOTAVAIL;
592 }
Arnd Bergmann27c0b1a2009-11-26 06:07:11 +0000593
594 if (data && data[IFLA_MACVLAN_MODE]) {
595 switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) {
596 case MACVLAN_MODE_PRIVATE:
597 case MACVLAN_MODE_VEPA:
598 case MACVLAN_MODE_BRIDGE:
599 break;
600 default:
601 return -EINVAL;
602 }
603 }
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700604 return 0;
605}
606
Eric Dumazet2c114552009-09-03 00:11:45 +0000607static int macvlan_get_tx_queues(struct net *net,
608 struct nlattr *tb[],
609 unsigned int *num_tx_queues,
610 unsigned int *real_num_tx_queues)
611{
612 struct net_device *real_dev;
613
614 if (!tb[IFLA_LINK])
615 return -EINVAL;
616
617 real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
618 if (!real_dev)
619 return -ENODEV;
620
621 *num_tx_queues = real_dev->num_tx_queues;
622 *real_num_tx_queues = real_dev->real_num_tx_queues;
623 return 0;
624}
625
Eric W. Biederman81adee42009-11-08 00:53:51 -0800626static int macvlan_newlink(struct net *src_net, struct net_device *dev,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700627 struct nlattr *tb[], struct nlattr *data[])
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
Eric W. Biederman81adee42009-11-08 00:53:51 -0800637 lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700638 if (lowerdev == NULL)
639 return -ENODEV;
640
Eric Biedermanb0832a22009-03-13 13:15:37 -0700641 /* When creating macvlans on top of other macvlans - use
642 * the real device as the lowerdev.
Patrick McHardya6ca5f12008-01-10 22:39:28 -0800643 */
Eric Biedermanb0832a22009-03-13 13:15:37 -0700644 if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) {
645 struct macvlan_dev *lowervlan = netdev_priv(lowerdev);
646 lowerdev = lowervlan->lowerdev;
647 }
Patrick McHardya6ca5f12008-01-10 22:39:28 -0800648
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700649 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
657 if (lowerdev->macvlan_port == NULL) {
658 err = macvlan_port_create(lowerdev);
659 if (err < 0)
660 return err;
661 }
662 port = lowerdev->macvlan_port;
663
664 vlan->lowerdev = lowerdev;
665 vlan->dev = dev;
666 vlan->port = port;
667
Arnd Bergmann27c0b1a2009-11-26 06:07:11 +0000668 vlan->mode = MACVLAN_MODE_VEPA;
669 if (data && data[IFLA_MACVLAN_MODE])
670 vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
671
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700672 err = register_netdevice(dev);
673 if (err < 0)
674 return err;
675
676 list_add_tail(&vlan->list, &port->vlans);
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -0800677 netif_stacked_transfer_operstate(lowerdev, dev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700678 return 0;
679}
680
Eric Dumazet23289a32009-10-27 07:06:36 +0000681static void macvlan_dellink(struct net_device *dev, struct list_head *head)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700682{
683 struct macvlan_dev *vlan = netdev_priv(dev);
684 struct macvlan_port *port = vlan->port;
685
686 list_del(&vlan->list);
Eric Dumazet23289a32009-10-27 07:06:36 +0000687 unregister_netdevice_queue(dev, head);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700688
689 if (list_empty(&port->vlans))
Patrick McHardy73120962008-05-08 01:13:31 -0700690 macvlan_port_destroy(port->dev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700691}
692
Arnd Bergmann27c0b1a2009-11-26 06:07:11 +0000693static int macvlan_changelink(struct net_device *dev,
694 struct nlattr *tb[], struct nlattr *data[])
695{
696 struct macvlan_dev *vlan = netdev_priv(dev);
697 if (data && data[IFLA_MACVLAN_MODE])
698 vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
699 return 0;
700}
701
702static size_t macvlan_get_size(const struct net_device *dev)
703{
704 return nla_total_size(4);
705}
706
707static int macvlan_fill_info(struct sk_buff *skb,
708 const struct net_device *dev)
709{
710 struct macvlan_dev *vlan = netdev_priv(dev);
711
712 NLA_PUT_U32(skb, IFLA_MACVLAN_MODE, vlan->mode);
713 return 0;
714
715nla_put_failure:
716 return -EMSGSIZE;
717}
718
719static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = {
720 [IFLA_MACVLAN_MODE] = { .type = NLA_U32 },
721};
722
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700723static struct rtnl_link_ops macvlan_link_ops __read_mostly = {
724 .kind = "macvlan",
725 .priv_size = sizeof(struct macvlan_dev),
Eric Dumazet2c114552009-09-03 00:11:45 +0000726 .get_tx_queues = macvlan_get_tx_queues,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700727 .setup = macvlan_setup,
728 .validate = macvlan_validate,
729 .newlink = macvlan_newlink,
730 .dellink = macvlan_dellink,
Arnd Bergmann27c0b1a2009-11-26 06:07:11 +0000731 .maxtype = IFLA_MACVLAN_MAX,
732 .policy = macvlan_policy,
733 .changelink = macvlan_changelink,
734 .get_size = macvlan_get_size,
735 .fill_info = macvlan_fill_info,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700736};
737
738static int macvlan_device_event(struct notifier_block *unused,
739 unsigned long event, void *ptr)
740{
741 struct net_device *dev = ptr;
742 struct macvlan_dev *vlan, *next;
743 struct macvlan_port *port;
744
745 port = dev->macvlan_port;
746 if (port == NULL)
747 return NOTIFY_DONE;
748
749 switch (event) {
750 case NETDEV_CHANGE:
751 list_for_each_entry(vlan, &port->vlans, list)
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -0800752 netif_stacked_transfer_operstate(vlan->lowerdev,
753 vlan->dev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700754 break;
755 case NETDEV_FEAT_CHANGE:
756 list_for_each_entry(vlan, &port->vlans, list) {
757 vlan->dev->features = dev->features & MACVLAN_FEATURES;
Patrick McHardy8c2acc52009-11-23 14:18:53 -0800758 vlan->dev->gso_max_size = dev->gso_max_size;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700759 netdev_features_change(vlan->dev);
760 }
761 break;
762 case NETDEV_UNREGISTER:
763 list_for_each_entry_safe(vlan, next, &port->vlans, list)
Eric Dumazet23289a32009-10-27 07:06:36 +0000764 macvlan_dellink(vlan->dev, NULL);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700765 break;
766 }
767 return NOTIFY_DONE;
768}
769
770static struct notifier_block macvlan_notifier_block __read_mostly = {
771 .notifier_call = macvlan_device_event,
772};
773
774static int __init macvlan_init_module(void)
775{
776 int err;
777
778 register_netdevice_notifier(&macvlan_notifier_block);
779 macvlan_handle_frame_hook = macvlan_handle_frame;
780
781 err = rtnl_link_register(&macvlan_link_ops);
782 if (err < 0)
783 goto err1;
784 return 0;
785err1:
Rami Rosen52913242008-01-31 16:56:03 -0800786 macvlan_handle_frame_hook = NULL;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700787 unregister_netdevice_notifier(&macvlan_notifier_block);
788 return err;
789}
790
791static void __exit macvlan_cleanup_module(void)
792{
793 rtnl_link_unregister(&macvlan_link_ops);
794 macvlan_handle_frame_hook = NULL;
795 unregister_netdevice_notifier(&macvlan_notifier_block);
796}
797
798module_init(macvlan_init_module);
799module_exit(macvlan_cleanup_module);
800
801MODULE_LICENSE("GPL");
802MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
803MODULE_DESCRIPTION("Driver for MAC address based VLANs");
804MODULE_ALIAS_RTNL_LINK("macvlan");