blob: 0fc9dc7f20db02da7ac784c193b940453b7eccd7 [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;
Jiri Pirko8b37ef02010-06-07 01:36:29 +000040 struct rcu_head rcu;
Patrick McHardyb863ceb2007-07-14 18:55:06 -070041};
42
Jiri Pirkoa35e2c12010-06-15 03:27:57 +000043#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
Patrick McHardyb863ceb2007-07-14 18:55:06 -070048static 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) {
Eric Dumazetac067132009-09-01 05:46:05 +000055 if (!compare_ether_addr_64bits(vlan->dev->dev_addr, addr))
Patrick McHardyb863ceb2007-07-14 18:55:06 -070056 return vlan;
57 }
58 return NULL;
59}
60
Eric Biedermanf9ac30f2009-03-13 13:16:13 -070061static 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 */
Eric Dumazetac067132009-09-01 05:46:05 +000093 if (!compare_ether_addr_64bits(port->dev->dev_addr, addr))
Eric Biedermanf9ac30f2009-03-13 13:16:13 -070094 return 1;
95
96 if (macvlan_hash_lookup(port, addr))
97 return 1;
98
99 return 0;
100}
101
Arnd Bergmanna1e514c2009-11-26 06:07:09 +0000102
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000103static int macvlan_broadcast_one(struct sk_buff *skb,
104 const struct macvlan_dev *vlan,
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000105 const struct ethhdr *eth, bool local)
Arnd Bergmanna1e514c2009-11-26 06:07:09 +0000106{
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000107 struct net_device *dev = vlan->dev;
Arnd Bergmanna1e514c2009-11-26 06:07:09 +0000108 if (!skb)
109 return NET_RX_DROP;
110
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000111 if (local)
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000112 return vlan->forward(dev, skb);
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000113
Arnd Bergmanna1e514c2009-11-26 06:07:09 +0000114 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
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000121 return vlan->receive(skb);
Arnd Bergmanna1e514c2009-11-26 06:07:09 +0000122}
123
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700124static void macvlan_broadcast(struct sk_buff *skb,
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000125 const struct macvlan_port *port,
126 struct net_device *src,
127 enum macvlan_mode mode)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700128{
129 const struct ethhdr *eth = eth_hdr(skb);
130 const struct macvlan_dev *vlan;
131 struct hlist_node *n;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700132 struct sk_buff *nskb;
133 unsigned int i;
Arnd Bergmanna1e514c2009-11-26 06:07:09 +0000134 int err;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700135
Patrick McHardyefbbced2008-11-26 15:30:48 -0800136 if (skb->protocol == htons(ETH_P_PAUSE))
137 return;
138
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700139 for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
140 hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[i], hlist) {
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000141 if (vlan->dev == src || !(vlan->mode & mode))
142 continue;
143
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700144 nskb = skb_clone(skb, GFP_ATOMIC);
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000145 err = macvlan_broadcast_one(nskb, vlan, eth,
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000146 mode == MACVLAN_MODE_BRIDGE);
Arnd Bergmanna1e514c2009-11-26 06:07:09 +0000147 macvlan_count_rx(vlan, skb->len + ETH_HLEN,
148 err == NET_RX_SUCCESS, 1);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700149 }
150 }
151}
152
153/* called under rcu_read_lock() from netif_receive_skb */
Jiri Pirkoab95bfe2010-06-01 21:52:08 +0000154static struct sk_buff *macvlan_handle_frame(struct sk_buff *skb)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700155{
Jiri Pirkoab95bfe2010-06-01 21:52:08 +0000156 struct macvlan_port *port;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700157 const struct ethhdr *eth = eth_hdr(skb);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700158 const struct macvlan_dev *vlan;
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000159 const struct macvlan_dev *src;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700160 struct net_device *dev;
Sridhar Samudralaba018772010-07-27 09:10:07 +0000161 unsigned int len = 0;
162 int ret = NET_RX_DROP;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700163
Jiri Pirkoa35e2c12010-06-15 03:27:57 +0000164 port = macvlan_port_get_rcu(skb->dev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700165 if (is_multicast_ether_addr(eth->h_dest)) {
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000166 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);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700185 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 }
Arnd Bergmanna1e514c2009-11-26 06:07:09 +0000197 len = skb->len + ETH_HLEN;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700198 skb = skb_share_check(skb, GFP_ATOMIC);
Arnd Bergmanna1e514c2009-11-26 06:07:09 +0000199 if (!skb)
Sridhar Samudralaba018772010-07-27 09:10:07 +0000200 goto out;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700201
202 skb->dev = dev;
203 skb->pkt_type = PACKET_HOST;
204
Sridhar Samudralaba018772010-07-27 09:10:07 +0000205 ret = vlan->receive(skb);
206
207out:
208 macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, 0);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700209 return NULL;
210}
211
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000212static 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;
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000230 int ret = dest->forward(dest->dev, skb);
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000231 macvlan_count_rx(dest, length,
232 ret == NET_RX_SUCCESS, 0);
233
234 return NET_XMIT_SUCCESS;
235 }
236 }
237
238xmit_world:
Arnd Bergmann8a83a002010-01-30 12:23:03 +0000239 skb_set_dev(skb, vlan->lowerdev);
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000240 return dev_queue_xmit(skb);
241}
242
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000243netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
244 struct net_device *dev)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700245{
Eric Dumazet2c114552009-09-03 00:11:45 +0000246 int i = skb_get_queue_mapping(skb);
247 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700248 unsigned int len = skb->len;
249 int ret;
250
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000251 ret = macvlan_queue_xmit(skb, dev);
Eric Dumazet2d6c9ff2010-05-10 04:51:02 +0000252 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
Eric Dumazet2c114552009-09-03 00:11:45 +0000253 txq->tx_packets++;
254 txq->tx_bytes += len;
255 } else
256 txq->tx_dropped++;
257
Patrick McHardycbbef5e2009-11-10 06:14:24 +0000258 return ret;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700259}
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000260EXPORT_SYMBOL_GPL(macvlan_start_xmit);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700261
262static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700263 unsigned short type, const void *daddr,
264 const void *saddr, unsigned len)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700265{
266 const struct macvlan_dev *vlan = netdev_priv(dev);
267 struct net_device *lowerdev = vlan->lowerdev;
268
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700269 return dev_hard_header(skb, lowerdev, type, daddr,
270 saddr ? : dev->dev_addr, len);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700271}
272
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700273static const struct header_ops macvlan_hard_header_ops = {
274 .create = macvlan_hard_header,
275 .rebuild = eth_rebuild_header,
276 .parse = eth_header_parse,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700277 .cache = eth_header_cache,
278 .cache_update = eth_header_cache_update,
279};
280
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700281static int macvlan_open(struct net_device *dev)
282{
283 struct macvlan_dev *vlan = netdev_priv(dev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700284 struct net_device *lowerdev = vlan->lowerdev;
285 int err;
286
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700287 err = -EBUSY;
288 if (macvlan_addr_busy(vlan->port, dev->dev_addr))
289 goto out;
290
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000291 err = dev_uc_add(lowerdev, dev->dev_addr);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700292 if (err < 0)
Wang Chenb89fb7d2008-07-14 20:57:07 -0700293 goto out;
294 if (dev->flags & IFF_ALLMULTI) {
295 err = dev_set_allmulti(lowerdev, 1);
296 if (err < 0)
297 goto del_unicast;
298 }
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700299 macvlan_hash_add(vlan);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700300 return 0;
Wang Chenb89fb7d2008-07-14 20:57:07 -0700301
302del_unicast:
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000303 dev_uc_del(lowerdev, dev->dev_addr);
Wang Chenb89fb7d2008-07-14 20:57:07 -0700304out:
305 return err;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700306}
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
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000317 dev_uc_del(lowerdev, dev->dev_addr);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700318
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700319 macvlan_hash_del(vlan);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700320 return 0;
321}
322
Patrick McHardyad5d20a2007-11-19 22:00:42 -0800323static 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
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700333 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;
Patrick McHardyad5d20a2007-11-19 22:00:42 -0800340
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000341 err = dev_uc_add(lowerdev, addr->sa_data);
Jiri Pirkoccffad252009-05-22 23:22:17 +0000342 if (err)
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700343 return err;
Patrick McHardyad5d20a2007-11-19 22:00:42 -0800344
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000345 dev_uc_del(lowerdev, dev->dev_addr);
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700346
347 macvlan_hash_change_addr(vlan, addr->sa_data);
348 }
Patrick McHardyad5d20a2007-11-19 22:00:42 -0800349 return 0;
350}
351
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700352static 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;
David S. Millercf508b12008-07-22 14:16:42 -0700384static struct lock_class_key macvlan_netdev_addr_lock_key;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700385
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 | \
Patrick Mullaney6eb3a852010-01-16 01:05:38 -0800389 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700390
391#define MACVLAN_STATE_MASK \
392 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
393
David S. Millere8a04642008-07-17 00:34:19 -0700394static void macvlan_set_lockdep_class_one(struct net_device *dev,
395 struct netdev_queue *txq,
396 void *_unused)
David S. Millerc773e842008-07-08 23:13:53 -0700397{
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{
David S. Millercf508b12008-07-22 14:16:42 -0700404 lockdep_set_class(&dev->addr_list_lock,
405 &macvlan_netdev_addr_lock_key);
David S. Millere8a04642008-07-17 00:34:19 -0700406 netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL);
David S. Millerc773e842008-07-08 23:13:53 -0700407}
408
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700409static 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;
Patrick McHardy8c2acc52009-11-23 14:18:53 -0800417 dev->gso_max_size = lowerdev->gso_max_size;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700418 dev->iflink = lowerdev->ifindex;
sg.tweak@gmail.comef5c8992009-06-10 09:55:02 +0000419 dev->hard_header_len = lowerdev->hard_header_len;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700420
David S. Millerc773e842008-07-08 23:13:53 -0700421 macvlan_set_lockdep_class(dev);
422
Eric Dumazetfccaf712009-11-17 08:53:49 +0000423 vlan->rx_stats = alloc_percpu(struct macvlan_rx_stats);
424 if (!vlan->rx_stats)
425 return -ENOMEM;
426
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700427 return 0;
428}
429
Eric Dumazetfccaf712009-11-17 08:53:49 +0000430static 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
Eric Dumazet28172732010-07-07 14:58:56 -0700437static struct rtnl_link_stats64 *macvlan_dev_get_stats64(struct net_device *dev,
438 struct rtnl_link_stats64 *stats)
Eric Dumazetfccaf712009-11-17 08:53:49 +0000439{
Eric Dumazetfccaf712009-11-17 08:53:49 +0000440 struct macvlan_dev *vlan = netdev_priv(dev);
441
Ben Hutchings3cfde792010-07-09 09:11:52 +0000442 dev_txq_stats_fold(dev, stats);
Eric Dumazetfccaf712009-11-17 08:53:49 +0000443
444 if (vlan->rx_stats) {
Eric Dumazetbc661542010-06-24 00:54:21 +0000445 struct macvlan_rx_stats *p, accum = {0};
446 u64 rx_packets, rx_bytes, rx_multicast;
447 unsigned int start;
Eric Dumazetfccaf712009-11-17 08:53:49 +0000448 int i;
449
450 for_each_possible_cpu(i) {
451 p = per_cpu_ptr(vlan->rx_stats, i);
Eric Dumazetbc661542010-06-24 00:54:21 +0000452 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;
Eric Dumazetfccaf712009-11-17 08:53:49 +0000463 }
Eric Dumazetbc661542010-06-24 00:54:21 +0000464 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;
Eric Dumazetfccaf712009-11-17 08:53:49 +0000469 }
470 return stats;
471}
472
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700473static 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);
Patrick McHardyb1b67dd2009-04-20 04:49:28 +0000483 return dev_ethtool_get_rx_csum(vlan->lowerdev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700484}
485
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700486static int macvlan_ethtool_get_settings(struct net_device *dev,
487 struct ethtool_cmd *cmd)
488{
489 const struct macvlan_dev *vlan = netdev_priv(dev);
Patrick McHardyb1b67dd2009-04-20 04:49:28 +0000490 return dev_ethtool_get_settings(vlan->lowerdev, cmd);
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700491}
492
493static u32 macvlan_ethtool_get_flags(struct net_device *dev)
494{
495 const struct macvlan_dev *vlan = netdev_priv(dev);
Patrick McHardyb1b67dd2009-04-20 04:49:28 +0000496 return dev_ethtool_get_flags(vlan->lowerdev);
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700497}
498
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700499static const struct ethtool_ops macvlan_ethtool_ops = {
500 .get_link = ethtool_op_get_link,
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700501 .get_settings = macvlan_ethtool_get_settings,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700502 .get_rx_csum = macvlan_ethtool_get_rx_csum,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700503 .get_drvinfo = macvlan_ethtool_get_drvinfo,
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700504 .get_flags = macvlan_ethtool_get_flags,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700505};
506
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800507static const struct net_device_ops macvlan_netdev_ops = {
508 .ndo_init = macvlan_init,
Eric Dumazetfccaf712009-11-17 08:53:49 +0000509 .ndo_uninit = macvlan_uninit,
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800510 .ndo_open = macvlan_open,
511 .ndo_stop = macvlan_stop,
Stephen Hemminger00829822008-11-20 20:14:53 -0800512 .ndo_start_xmit = macvlan_start_xmit,
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800513 .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,
Eric Dumazetbc661542010-06-24 00:54:21 +0000517 .ndo_get_stats64 = macvlan_dev_get_stats64,
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800518 .ndo_validate_addr = eth_validate_addr,
519};
520
Herbert Xu8a357472010-07-21 21:44:31 +0000521void macvlan_common_setup(struct net_device *dev)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700522{
523 ether_setup(dev);
524
Eric Dumazet93f154b2009-05-18 22:19:19 -0700525 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800526 dev->netdev_ops = &macvlan_netdev_ops;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700527 dev->destructor = free_netdev;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700528 dev->header_ops = &macvlan_hard_header_ops,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700529 dev->ethtool_ops = &macvlan_ethtool_ops;
Herbert Xu8a357472010-07-21 21:44:31 +0000530}
531EXPORT_SYMBOL_GPL(macvlan_common_setup);
532
533static void macvlan_setup(struct net_device *dev)
534{
535 macvlan_common_setup(dev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700536 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;
Jiri Pirkoab95bfe2010-06-01 21:52:08 +0000543 int err;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700544
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]);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +0000556
Jiri Pirkoa35e2c12010-06-15 03:27:57 +0000557 err = netdev_rx_handler_register(dev, macvlan_handle_frame, port);
558 if (err)
Jiri Pirkoab95bfe2010-06-01 21:52:08 +0000559 kfree(port);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +0000560
Jiri Pirkoa35e2c12010-06-15 03:27:57 +0000561 dev->priv_flags |= IFF_MACVLAN_PORT;
Jiri Pirkoab95bfe2010-06-01 21:52:08 +0000562 return err;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700563}
564
Jiri Pirko8b37ef02010-06-07 01:36:29 +0000565static 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
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700573static void macvlan_port_destroy(struct net_device *dev)
574{
Jiri Pirkoa35e2c12010-06-15 03:27:57 +0000575 struct macvlan_port *port = macvlan_port_get(dev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700576
Jiri Pirkoa35e2c12010-06-15 03:27:57 +0000577 dev->priv_flags &= ~IFF_MACVLAN_PORT;
Jiri Pirkoab95bfe2010-06-01 21:52:08 +0000578 netdev_rx_handler_unregister(dev);
Jiri Pirko8b37ef02010-06-07 01:36:29 +0000579 call_rcu(&port->rcu, macvlan_port_rcu_free);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700580}
581
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700582static 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 }
Arnd Bergmann27c0b1a2009-11-26 06:07:11 +0000590
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 }
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700601 return 0;
602}
603
Eric Dumazet2c114552009-09-03 00:11:45 +0000604static 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
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000623int 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))
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700628{
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
Jiri Pirkoa35e2c12010-06-15 03:27:57 +0000657 if (!macvlan_port_exists(lowerdev)) {
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700658 err = macvlan_port_create(lowerdev);
659 if (err < 0)
660 return err;
661 }
Jiri Pirkoa35e2c12010-06-15 03:27:57 +0000662 port = macvlan_port_get(lowerdev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700663
664 vlan->lowerdev = lowerdev;
665 vlan->dev = dev;
666 vlan->port = port;
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000667 vlan->receive = receive;
668 vlan->forward = forward;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700669
Arnd Bergmann27c0b1a2009-11-26 06:07:11 +0000670 vlan->mode = MACVLAN_MODE_VEPA;
671 if (data && data[IFLA_MACVLAN_MODE])
672 vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
673
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700674 err = register_netdevice(dev);
675 if (err < 0)
Jiri Pirkof16d3d52010-05-24 07:02:25 +0000676 goto destroy_port;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700677
678 list_add_tail(&vlan->list, &port->vlans);
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -0800679 netif_stacked_transfer_operstate(lowerdev, dev);
Jiri Pirkof16d3d52010-05-24 07:02:25 +0000680
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700681 return 0;
Jiri Pirkof16d3d52010-05-24 07:02:25 +0000682
683destroy_port:
684 if (list_empty(&port->vlans))
685 macvlan_port_destroy(lowerdev);
686
687 return err;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700688}
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000689EXPORT_SYMBOL_GPL(macvlan_common_newlink);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700690
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000691static 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)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700700{
701 struct macvlan_dev *vlan = netdev_priv(dev);
702 struct macvlan_port *port = vlan->port;
703
704 list_del(&vlan->list);
Eric Dumazet23289a32009-10-27 07:06:36 +0000705 unregister_netdevice_queue(dev, head);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700706
707 if (list_empty(&port->vlans))
Patrick McHardy73120962008-05-08 01:13:31 -0700708 macvlan_port_destroy(port->dev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700709}
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000710EXPORT_SYMBOL_GPL(macvlan_dellink);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700711
Arnd Bergmann27c0b1a2009-11-26 06:07:11 +0000712static 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
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000742int 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;
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000747 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 = {
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700759 .kind = "macvlan",
Herbert Xu8a357472010-07-21 21:44:31 +0000760 .setup = macvlan_setup,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700761 .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
Jiri Pirkoa35e2c12010-06-15 03:27:57 +0000772 if (!macvlan_port_exists(dev))
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700773 return NOTIFY_DONE;
774
Jiri Pirkoa35e2c12010-06-15 03:27:57 +0000775 port = macvlan_port_get(dev);
776
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700777 switch (event) {
778 case NETDEV_CHANGE:
779 list_for_each_entry(vlan, &port->vlans, list)
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -0800780 netif_stacked_transfer_operstate(vlan->lowerdev,
781 vlan->dev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700782 break;
783 case NETDEV_FEAT_CHANGE:
784 list_for_each_entry(vlan, &port->vlans, list) {
785 vlan->dev->features = dev->features & MACVLAN_FEATURES;
Patrick McHardy8c2acc52009-11-23 14:18:53 -0800786 vlan->dev->gso_max_size = dev->gso_max_size;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700787 netdev_features_change(vlan->dev);
788 }
789 break;
790 case NETDEV_UNREGISTER:
David Lamparter3b27e102010-09-17 03:22:19 +0000791 /* twiddle thumbs on netns device moves */
792 if (dev->reg_state != NETREG_UNREGISTERING)
793 break;
794
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700795 list_for_each_entry_safe(vlan, next, &port->vlans, list)
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000796 vlan->dev->rtnl_link_ops->dellink(vlan->dev, NULL);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700797 break;
Jiri Pirko1c01fe12010-03-10 10:30:19 +0000798 case NETDEV_PRE_TYPE_CHANGE:
799 /* Forbid underlaying device to change its type. */
800 return NOTIFY_BAD;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700801 }
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);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700814
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000815 err = macvlan_link_register(&macvlan_link_ops);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700816 if (err < 0)
817 goto err1;
818 return 0;
819err1:
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700820 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);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700827 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");