blob: defcd8a85744cf11aeea39aff124e59d64ecaa5c [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>
Jiri Pirko87002b02011-12-08 04:11:17 +000029#include <linux/if_vlan.h>
Patrick McHardyb863ceb2007-07-14 18:55:06 -070030#include <linux/if_link.h>
31#include <linux/if_macvlan.h>
Eric Dumazetcd431e72013-02-05 20:22:50 +000032#include <linux/hash.h>
Patrick McHardyb863ceb2007-07-14 18:55:06 -070033#include <net/rtnetlink.h>
Arnd Bergmann618e1b72009-11-26 06:07:10 +000034#include <net/xfrm.h>
Patrick McHardyb863ceb2007-07-14 18:55:06 -070035
36#define MACVLAN_HASH_SIZE (1 << BITS_PER_BYTE)
37
38struct macvlan_port {
39 struct net_device *dev;
40 struct hlist_head vlan_hash[MACVLAN_HASH_SIZE];
41 struct list_head vlans;
Jiri Pirko8b37ef02010-06-07 01:36:29 +000042 struct rcu_head rcu;
Sridhar Samudralaeb06acd2010-10-28 13:10:50 +000043 bool passthru;
Eric W. Biedermand5cd92442011-03-21 18:22:22 -070044 int count;
Patrick McHardyb863ceb2007-07-14 18:55:06 -070045};
46
Eric W. Biedermand5cd92442011-03-21 18:22:22 -070047static void macvlan_port_destroy(struct net_device *dev);
48
Jiri Pirkoa35e2c12010-06-15 03:27:57 +000049#define macvlan_port_get_rcu(dev) \
50 ((struct macvlan_port *) rcu_dereference(dev->rx_handler_data))
51#define macvlan_port_get(dev) ((struct macvlan_port *) dev->rx_handler_data)
52#define macvlan_port_exists(dev) (dev->priv_flags & IFF_MACVLAN_PORT)
53
Patrick McHardyb863ceb2007-07-14 18:55:06 -070054static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
55 const unsigned char *addr)
56{
57 struct macvlan_dev *vlan;
58 struct hlist_node *n;
59
60 hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[addr[5]], hlist) {
Joe Perchesa6700db2012-05-09 17:04:04 +000061 if (ether_addr_equal_64bits(vlan->dev->dev_addr, addr))
Patrick McHardyb863ceb2007-07-14 18:55:06 -070062 return vlan;
63 }
64 return NULL;
65}
66
Eric Biedermanf9ac30f2009-03-13 13:16:13 -070067static void macvlan_hash_add(struct macvlan_dev *vlan)
68{
69 struct macvlan_port *port = vlan->port;
70 const unsigned char *addr = vlan->dev->dev_addr;
71
72 hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]);
73}
74
Eric Dumazet449f4542011-05-19 12:24:16 +000075static void macvlan_hash_del(struct macvlan_dev *vlan, bool sync)
Eric Biedermanf9ac30f2009-03-13 13:16:13 -070076{
77 hlist_del_rcu(&vlan->hlist);
Eric Dumazet449f4542011-05-19 12:24:16 +000078 if (sync)
79 synchronize_rcu();
Eric Biedermanf9ac30f2009-03-13 13:16:13 -070080}
81
82static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
83 const unsigned char *addr)
84{
Eric Dumazet449f4542011-05-19 12:24:16 +000085 macvlan_hash_del(vlan, true);
Eric Biedermanf9ac30f2009-03-13 13:16:13 -070086 /* Now that we are unhashed it is safe to change the device
87 * address without confusing packet delivery.
88 */
89 memcpy(vlan->dev->dev_addr, addr, ETH_ALEN);
90 macvlan_hash_add(vlan);
91}
92
93static int macvlan_addr_busy(const struct macvlan_port *port,
94 const unsigned char *addr)
95{
96 /* Test to see if the specified multicast address is
97 * currently in use by the underlying device or
98 * another macvlan.
99 */
Joe Perchesa6700db2012-05-09 17:04:04 +0000100 if (ether_addr_equal_64bits(port->dev->dev_addr, addr))
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700101 return 1;
102
103 if (macvlan_hash_lookup(port, addr))
104 return 1;
105
106 return 0;
107}
108
Arnd Bergmanna1e514c2009-11-26 06:07:09 +0000109
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000110static int macvlan_broadcast_one(struct sk_buff *skb,
111 const struct macvlan_dev *vlan,
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000112 const struct ethhdr *eth, bool local)
Arnd Bergmanna1e514c2009-11-26 06:07:09 +0000113{
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000114 struct net_device *dev = vlan->dev;
Arnd Bergmanna1e514c2009-11-26 06:07:09 +0000115 if (!skb)
116 return NET_RX_DROP;
117
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000118 if (local)
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000119 return vlan->forward(dev, skb);
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000120
Arnd Bergmanna1e514c2009-11-26 06:07:09 +0000121 skb->dev = dev;
Joe Perchesa6700db2012-05-09 17:04:04 +0000122 if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast))
Arnd Bergmanna1e514c2009-11-26 06:07:09 +0000123 skb->pkt_type = PACKET_BROADCAST;
124 else
125 skb->pkt_type = PACKET_MULTICAST;
126
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000127 return vlan->receive(skb);
Arnd Bergmanna1e514c2009-11-26 06:07:09 +0000128}
129
Eric Dumazet3807ff52013-02-07 16:41:02 +0000130static u32 macvlan_hash_mix(const struct macvlan_dev *vlan)
131{
132 return (u32)(((unsigned long)vlan) >> L1_CACHE_SHIFT);
133}
134
135
136static unsigned int mc_hash(const struct macvlan_dev *vlan,
137 const unsigned char *addr)
Eric Dumazetcd431e72013-02-05 20:22:50 +0000138{
139 u32 val = __get_unaligned_cpu32(addr + 2);
140
Eric Dumazet3807ff52013-02-07 16:41:02 +0000141 val ^= macvlan_hash_mix(vlan);
Eric Dumazetcd431e72013-02-05 20:22:50 +0000142 return hash_32(val, MACVLAN_MC_FILTER_BITS);
143}
144
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700145static void macvlan_broadcast(struct sk_buff *skb,
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000146 const struct macvlan_port *port,
147 struct net_device *src,
148 enum macvlan_mode mode)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700149{
150 const struct ethhdr *eth = eth_hdr(skb);
151 const struct macvlan_dev *vlan;
152 struct hlist_node *n;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700153 struct sk_buff *nskb;
154 unsigned int i;
Arnd Bergmanna1e514c2009-11-26 06:07:09 +0000155 int err;
Eric Dumazet3807ff52013-02-07 16:41:02 +0000156 unsigned int hash;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700157
Patrick McHardyefbbced2008-11-26 15:30:48 -0800158 if (skb->protocol == htons(ETH_P_PAUSE))
159 return;
160
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700161 for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
162 hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[i], hlist) {
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000163 if (vlan->dev == src || !(vlan->mode & mode))
164 continue;
165
Eric Dumazet3807ff52013-02-07 16:41:02 +0000166 hash = mc_hash(vlan, eth->h_dest);
Eric Dumazetcd431e72013-02-05 20:22:50 +0000167 if (!test_bit(hash, vlan->mc_filter))
168 continue;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700169 nskb = skb_clone(skb, GFP_ATOMIC);
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000170 err = macvlan_broadcast_one(nskb, vlan, eth,
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000171 mode == MACVLAN_MODE_BRIDGE);
Arnd Bergmanna1e514c2009-11-26 06:07:09 +0000172 macvlan_count_rx(vlan, skb->len + ETH_HLEN,
173 err == NET_RX_SUCCESS, 1);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700174 }
175 }
176}
177
178/* called under rcu_read_lock() from netif_receive_skb */
Jiri Pirko8a4eb572011-03-12 03:14:39 +0000179static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700180{
Jiri Pirkoab95bfe2010-06-01 21:52:08 +0000181 struct macvlan_port *port;
Jiri Pirko8a4eb572011-03-12 03:14:39 +0000182 struct sk_buff *skb = *pskb;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700183 const struct ethhdr *eth = eth_hdr(skb);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700184 const struct macvlan_dev *vlan;
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000185 const struct macvlan_dev *src;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700186 struct net_device *dev;
Sridhar Samudralaba018772010-07-27 09:10:07 +0000187 unsigned int len = 0;
188 int ret = NET_RX_DROP;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700189
Jiri Pirkoa35e2c12010-06-15 03:27:57 +0000190 port = macvlan_port_get_rcu(skb->dev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700191 if (is_multicast_ether_addr(eth->h_dest)) {
Eric Dumazetbc416d92011-10-06 10:28:31 +0000192 skb = ip_check_defrag(skb, IP_DEFRAG_MACVLAN);
193 if (!skb)
194 return RX_HANDLER_CONSUMED;
Eric Dumazet4ec7ac12012-01-23 05:38:59 +0000195 eth = eth_hdr(skb);
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000196 src = macvlan_hash_lookup(port, eth->h_source);
197 if (!src)
198 /* frame comes from an external address */
199 macvlan_broadcast(skb, port, NULL,
200 MACVLAN_MODE_PRIVATE |
201 MACVLAN_MODE_VEPA |
Sridhar Samudralaeb06acd2010-10-28 13:10:50 +0000202 MACVLAN_MODE_PASSTHRU|
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000203 MACVLAN_MODE_BRIDGE);
204 else if (src->mode == MACVLAN_MODE_VEPA)
205 /* flood to everyone except source */
206 macvlan_broadcast(skb, port, src->dev,
207 MACVLAN_MODE_VEPA |
208 MACVLAN_MODE_BRIDGE);
209 else if (src->mode == MACVLAN_MODE_BRIDGE)
210 /*
211 * flood only to VEPA ports, bridge ports
212 * already saw the frame on the way out.
213 */
214 macvlan_broadcast(skb, port, src->dev,
215 MACVLAN_MODE_VEPA);
stephen hemminger729e72a2011-11-02 12:11:53 +0000216 else {
217 /* forward to original port. */
218 vlan = src;
219 ret = macvlan_broadcast_one(skb, vlan, eth, 0);
220 goto out;
221 }
222
Jiri Pirko8a4eb572011-03-12 03:14:39 +0000223 return RX_HANDLER_PASS;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700224 }
225
Sridhar Samudralaeb06acd2010-10-28 13:10:50 +0000226 if (port->passthru)
227 vlan = list_first_entry(&port->vlans, struct macvlan_dev, list);
228 else
229 vlan = macvlan_hash_lookup(port, eth->h_dest);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700230 if (vlan == NULL)
Jiri Pirko8a4eb572011-03-12 03:14:39 +0000231 return RX_HANDLER_PASS;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700232
233 dev = vlan->dev;
234 if (unlikely(!(dev->flags & IFF_UP))) {
235 kfree_skb(skb);
Jiri Pirko8a4eb572011-03-12 03:14:39 +0000236 return RX_HANDLER_CONSUMED;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700237 }
Arnd Bergmanna1e514c2009-11-26 06:07:09 +0000238 len = skb->len + ETH_HLEN;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700239 skb = skb_share_check(skb, GFP_ATOMIC);
Arnd Bergmanna1e514c2009-11-26 06:07:09 +0000240 if (!skb)
Sridhar Samudralaba018772010-07-27 09:10:07 +0000241 goto out;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700242
243 skb->dev = dev;
244 skb->pkt_type = PACKET_HOST;
245
Sridhar Samudralaba018772010-07-27 09:10:07 +0000246 ret = vlan->receive(skb);
247
248out:
249 macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, 0);
Jiri Pirko8a4eb572011-03-12 03:14:39 +0000250 return RX_HANDLER_CONSUMED;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700251}
252
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000253static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
254{
255 const struct macvlan_dev *vlan = netdev_priv(dev);
256 const struct macvlan_port *port = vlan->port;
257 const struct macvlan_dev *dest;
Daniel Lezcano12a28562011-03-14 06:08:07 +0000258 __u8 ip_summed = skb->ip_summed;
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000259
260 if (vlan->mode == MACVLAN_MODE_BRIDGE) {
261 const struct ethhdr *eth = (void *)skb->data;
Daniel Lezcano12a28562011-03-14 06:08:07 +0000262 skb->ip_summed = CHECKSUM_UNNECESSARY;
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000263
264 /* send to other bridge ports directly */
265 if (is_multicast_ether_addr(eth->h_dest)) {
266 macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE);
267 goto xmit_world;
268 }
269
270 dest = macvlan_hash_lookup(port, eth->h_dest);
271 if (dest && dest->mode == MACVLAN_MODE_BRIDGE) {
David Warda37dd332011-05-19 02:53:20 +0000272 /* send to lowerdev first for its network taps */
David Wardcb2d0f32011-09-18 12:53:20 +0000273 dev_forward_skb(vlan->lowerdev, skb);
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000274
275 return NET_XMIT_SUCCESS;
276 }
277 }
278
279xmit_world:
Daniel Lezcano12a28562011-03-14 06:08:07 +0000280 skb->ip_summed = ip_summed;
David S. Miller59b99972012-05-10 23:03:34 -0400281 skb->dev = vlan->lowerdev;
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000282 return dev_queue_xmit(skb);
283}
284
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000285netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
286 struct net_device *dev)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700287{
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700288 unsigned int len = skb->len;
289 int ret;
Eric Dumazet8ffab512010-11-10 21:14:04 +0000290 const struct macvlan_dev *vlan = netdev_priv(dev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700291
Arnd Bergmann618e1b72009-11-26 06:07:10 +0000292 ret = macvlan_queue_xmit(skb, dev);
Eric Dumazet2d6c9ff2010-05-10 04:51:02 +0000293 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
Eric Dumazet8ffab512010-11-10 21:14:04 +0000294 struct macvlan_pcpu_stats *pcpu_stats;
Eric Dumazet2c114552009-09-03 00:11:45 +0000295
Eric Dumazet8ffab512010-11-10 21:14:04 +0000296 pcpu_stats = this_cpu_ptr(vlan->pcpu_stats);
297 u64_stats_update_begin(&pcpu_stats->syncp);
298 pcpu_stats->tx_packets++;
299 pcpu_stats->tx_bytes += len;
300 u64_stats_update_end(&pcpu_stats->syncp);
301 } else {
302 this_cpu_inc(vlan->pcpu_stats->tx_dropped);
303 }
Patrick McHardycbbef5e2009-11-10 06:14:24 +0000304 return ret;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700305}
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000306EXPORT_SYMBOL_GPL(macvlan_start_xmit);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700307
308static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700309 unsigned short type, const void *daddr,
310 const void *saddr, unsigned len)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700311{
312 const struct macvlan_dev *vlan = netdev_priv(dev);
313 struct net_device *lowerdev = vlan->lowerdev;
314
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700315 return dev_hard_header(skb, lowerdev, type, daddr,
316 saddr ? : dev->dev_addr, len);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700317}
318
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700319static const struct header_ops macvlan_hard_header_ops = {
320 .create = macvlan_hard_header,
321 .rebuild = eth_rebuild_header,
322 .parse = eth_header_parse,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700323 .cache = eth_header_cache,
324 .cache_update = eth_header_cache_update,
325};
326
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700327static int macvlan_open(struct net_device *dev)
328{
329 struct macvlan_dev *vlan = netdev_priv(dev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700330 struct net_device *lowerdev = vlan->lowerdev;
331 int err;
332
Sridhar Samudralaeb06acd2010-10-28 13:10:50 +0000333 if (vlan->port->passthru) {
John Fastabenddf8ef8f2012-04-15 06:44:37 +0000334 if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC))
335 dev_set_promiscuity(lowerdev, 1);
Sridhar Samudralaeb06acd2010-10-28 13:10:50 +0000336 goto hash_add;
337 }
338
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700339 err = -EBUSY;
340 if (macvlan_addr_busy(vlan->port, dev->dev_addr))
341 goto out;
342
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000343 err = dev_uc_add(lowerdev, dev->dev_addr);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700344 if (err < 0)
Wang Chenb89fb7d2008-07-14 20:57:07 -0700345 goto out;
346 if (dev->flags & IFF_ALLMULTI) {
347 err = dev_set_allmulti(lowerdev, 1);
348 if (err < 0)
349 goto del_unicast;
350 }
Sridhar Samudralaeb06acd2010-10-28 13:10:50 +0000351
352hash_add:
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700353 macvlan_hash_add(vlan);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700354 return 0;
Wang Chenb89fb7d2008-07-14 20:57:07 -0700355
356del_unicast:
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000357 dev_uc_del(lowerdev, dev->dev_addr);
Wang Chenb89fb7d2008-07-14 20:57:07 -0700358out:
359 return err;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700360}
361
362static int macvlan_stop(struct net_device *dev)
363{
364 struct macvlan_dev *vlan = netdev_priv(dev);
365 struct net_device *lowerdev = vlan->lowerdev;
366
John Fastabenddf8ef8f2012-04-15 06:44:37 +0000367 dev_uc_unsync(lowerdev, dev);
368 dev_mc_unsync(lowerdev, dev);
369
Sridhar Samudralaeb06acd2010-10-28 13:10:50 +0000370 if (vlan->port->passthru) {
John Fastabenddf8ef8f2012-04-15 06:44:37 +0000371 if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC))
372 dev_set_promiscuity(lowerdev, -1);
Sridhar Samudralaeb06acd2010-10-28 13:10:50 +0000373 goto hash_del;
374 }
375
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700376 if (dev->flags & IFF_ALLMULTI)
377 dev_set_allmulti(lowerdev, -1);
378
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000379 dev_uc_del(lowerdev, dev->dev_addr);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700380
Sridhar Samudralaeb06acd2010-10-28 13:10:50 +0000381hash_del:
Eric Dumazet449f4542011-05-19 12:24:16 +0000382 macvlan_hash_del(vlan, !dev->dismantle);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700383 return 0;
384}
385
Patrick McHardyad5d20a2007-11-19 22:00:42 -0800386static int macvlan_set_mac_address(struct net_device *dev, void *p)
387{
388 struct macvlan_dev *vlan = netdev_priv(dev);
389 struct net_device *lowerdev = vlan->lowerdev;
390 struct sockaddr *addr = p;
391 int err;
392
393 if (!is_valid_ether_addr(addr->sa_data))
394 return -EADDRNOTAVAIL;
395
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700396 if (!(dev->flags & IFF_UP)) {
397 /* Just copy in the new address */
398 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
399 } else {
400 /* Rehash and update the device filters */
401 if (macvlan_addr_busy(vlan->port, addr->sa_data))
402 return -EBUSY;
Patrick McHardyad5d20a2007-11-19 22:00:42 -0800403
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000404 err = dev_uc_add(lowerdev, addr->sa_data);
Jiri Pirkoccffad252009-05-22 23:22:17 +0000405 if (err)
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700406 return err;
Patrick McHardyad5d20a2007-11-19 22:00:42 -0800407
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000408 dev_uc_del(lowerdev, dev->dev_addr);
Eric Biedermanf9ac30f2009-03-13 13:16:13 -0700409
410 macvlan_hash_change_addr(vlan, addr->sa_data);
411 }
Patrick McHardyad5d20a2007-11-19 22:00:42 -0800412 return 0;
413}
414
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700415static void macvlan_change_rx_flags(struct net_device *dev, int change)
416{
417 struct macvlan_dev *vlan = netdev_priv(dev);
418 struct net_device *lowerdev = vlan->lowerdev;
419
420 if (change & IFF_ALLMULTI)
421 dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
422}
423
John Fastabenddf8ef8f2012-04-15 06:44:37 +0000424static void macvlan_set_mac_lists(struct net_device *dev)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700425{
426 struct macvlan_dev *vlan = netdev_priv(dev);
427
Eric Dumazetcd431e72013-02-05 20:22:50 +0000428 if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
429 bitmap_fill(vlan->mc_filter, MACVLAN_MC_FILTER_SZ);
430 } else {
431 struct netdev_hw_addr *ha;
432 DECLARE_BITMAP(filter, MACVLAN_MC_FILTER_SZ);
433
434 bitmap_zero(filter, MACVLAN_MC_FILTER_SZ);
435 netdev_for_each_mc_addr(ha, dev) {
Eric Dumazet3807ff52013-02-07 16:41:02 +0000436 __set_bit(mc_hash(vlan, ha->addr), filter);
Eric Dumazetcd431e72013-02-05 20:22:50 +0000437 }
Eric Dumazetd5270432013-02-07 16:02:57 +0000438
Eric Dumazet3807ff52013-02-07 16:41:02 +0000439 __set_bit(mc_hash(vlan, dev->broadcast), filter);
Eric Dumazetd5270432013-02-07 16:02:57 +0000440
Eric Dumazetcd431e72013-02-05 20:22:50 +0000441 bitmap_copy(vlan->mc_filter, filter, MACVLAN_MC_FILTER_SZ);
442 }
John Fastabenddf8ef8f2012-04-15 06:44:37 +0000443 dev_uc_sync(vlan->lowerdev, dev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700444 dev_mc_sync(vlan->lowerdev, dev);
445}
446
447static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
448{
449 struct macvlan_dev *vlan = netdev_priv(dev);
450
451 if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
452 return -EINVAL;
453 dev->mtu = new_mtu;
454 return 0;
455}
456
457/*
458 * macvlan network devices have devices nesting below it and are a special
459 * "super class" of normal network devices; split their locks off into a
460 * separate class since they always nest.
461 */
462static struct lock_class_key macvlan_netdev_xmit_lock_key;
David S. Millercf508b12008-07-22 14:16:42 -0700463static struct lock_class_key macvlan_netdev_addr_lock_key;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700464
465#define MACVLAN_FEATURES \
466 (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
467 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
John Fastabend8d13e672011-06-06 04:27:16 +0000468 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
469 NETIF_F_HW_VLAN_FILTER)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700470
471#define MACVLAN_STATE_MASK \
472 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
473
David S. Millere8a04642008-07-17 00:34:19 -0700474static void macvlan_set_lockdep_class_one(struct net_device *dev,
475 struct netdev_queue *txq,
476 void *_unused)
David S. Millerc773e842008-07-08 23:13:53 -0700477{
478 lockdep_set_class(&txq->_xmit_lock,
479 &macvlan_netdev_xmit_lock_key);
480}
481
482static void macvlan_set_lockdep_class(struct net_device *dev)
483{
David S. Millercf508b12008-07-22 14:16:42 -0700484 lockdep_set_class(&dev->addr_list_lock,
485 &macvlan_netdev_addr_lock_key);
David S. Millere8a04642008-07-17 00:34:19 -0700486 netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL);
David S. Millerc773e842008-07-08 23:13:53 -0700487}
488
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700489static int macvlan_init(struct net_device *dev)
490{
491 struct macvlan_dev *vlan = netdev_priv(dev);
492 const struct net_device *lowerdev = vlan->lowerdev;
493
494 dev->state = (dev->state & ~MACVLAN_STATE_MASK) |
495 (lowerdev->state & MACVLAN_STATE_MASK);
496 dev->features = lowerdev->features & MACVLAN_FEATURES;
Eric Dumazet8ffab512010-11-10 21:14:04 +0000497 dev->features |= NETIF_F_LLTX;
Patrick McHardy8c2acc52009-11-23 14:18:53 -0800498 dev->gso_max_size = lowerdev->gso_max_size;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700499 dev->iflink = lowerdev->ifindex;
sg.tweak@gmail.comef5c8992009-06-10 09:55:02 +0000500 dev->hard_header_len = lowerdev->hard_header_len;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700501
David S. Millerc773e842008-07-08 23:13:53 -0700502 macvlan_set_lockdep_class(dev);
503
Eric Dumazet8ffab512010-11-10 21:14:04 +0000504 vlan->pcpu_stats = alloc_percpu(struct macvlan_pcpu_stats);
505 if (!vlan->pcpu_stats)
Eric Dumazetfccaf712009-11-17 08:53:49 +0000506 return -ENOMEM;
507
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700508 return 0;
509}
510
Eric Dumazetfccaf712009-11-17 08:53:49 +0000511static void macvlan_uninit(struct net_device *dev)
512{
513 struct macvlan_dev *vlan = netdev_priv(dev);
Eric W. Biedermand5cd92442011-03-21 18:22:22 -0700514 struct macvlan_port *port = vlan->port;
Eric Dumazetfccaf712009-11-17 08:53:49 +0000515
Eric Dumazet8ffab512010-11-10 21:14:04 +0000516 free_percpu(vlan->pcpu_stats);
Eric W. Biedermand5cd92442011-03-21 18:22:22 -0700517
518 port->count -= 1;
519 if (!port->count)
520 macvlan_port_destroy(port->dev);
Eric Dumazetfccaf712009-11-17 08:53:49 +0000521}
522
Eric Dumazet28172732010-07-07 14:58:56 -0700523static struct rtnl_link_stats64 *macvlan_dev_get_stats64(struct net_device *dev,
524 struct rtnl_link_stats64 *stats)
Eric Dumazetfccaf712009-11-17 08:53:49 +0000525{
Eric Dumazetfccaf712009-11-17 08:53:49 +0000526 struct macvlan_dev *vlan = netdev_priv(dev);
527
Eric Dumazet8ffab512010-11-10 21:14:04 +0000528 if (vlan->pcpu_stats) {
529 struct macvlan_pcpu_stats *p;
530 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
531 u32 rx_errors = 0, tx_dropped = 0;
Eric Dumazetbc661542010-06-24 00:54:21 +0000532 unsigned int start;
Eric Dumazetfccaf712009-11-17 08:53:49 +0000533 int i;
534
535 for_each_possible_cpu(i) {
Eric Dumazet8ffab512010-11-10 21:14:04 +0000536 p = per_cpu_ptr(vlan->pcpu_stats, i);
Eric Dumazetbc661542010-06-24 00:54:21 +0000537 do {
538 start = u64_stats_fetch_begin_bh(&p->syncp);
539 rx_packets = p->rx_packets;
540 rx_bytes = p->rx_bytes;
541 rx_multicast = p->rx_multicast;
Eric Dumazet8ffab512010-11-10 21:14:04 +0000542 tx_packets = p->tx_packets;
543 tx_bytes = p->tx_bytes;
Eric Dumazetbc661542010-06-24 00:54:21 +0000544 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
Eric Dumazet8ffab512010-11-10 21:14:04 +0000545
546 stats->rx_packets += rx_packets;
547 stats->rx_bytes += rx_bytes;
548 stats->multicast += rx_multicast;
549 stats->tx_packets += tx_packets;
550 stats->tx_bytes += tx_bytes;
551 /* rx_errors & tx_dropped are u32, updated
552 * without syncp protection.
553 */
554 rx_errors += p->rx_errors;
555 tx_dropped += p->tx_dropped;
Eric Dumazetfccaf712009-11-17 08:53:49 +0000556 }
Eric Dumazet8ffab512010-11-10 21:14:04 +0000557 stats->rx_errors = rx_errors;
558 stats->rx_dropped = rx_errors;
559 stats->tx_dropped = tx_dropped;
Eric Dumazetfccaf712009-11-17 08:53:49 +0000560 }
561 return stats;
562}
563
Jiri Pirko8e586132011-12-08 19:52:37 -0500564static int macvlan_vlan_rx_add_vid(struct net_device *dev,
John Fastabend8d13e672011-06-06 04:27:16 +0000565 unsigned short vid)
566{
567 struct macvlan_dev *vlan = netdev_priv(dev);
568 struct net_device *lowerdev = vlan->lowerdev;
John Fastabend8d13e672011-06-06 04:27:16 +0000569
Jiri Pirko87002b02011-12-08 04:11:17 +0000570 return vlan_vid_add(lowerdev, vid);
John Fastabend8d13e672011-06-06 04:27:16 +0000571}
572
Jiri Pirko8e586132011-12-08 19:52:37 -0500573static int macvlan_vlan_rx_kill_vid(struct net_device *dev,
John Fastabend8d13e672011-06-06 04:27:16 +0000574 unsigned short vid)
575{
576 struct macvlan_dev *vlan = netdev_priv(dev);
577 struct net_device *lowerdev = vlan->lowerdev;
John Fastabend8d13e672011-06-06 04:27:16 +0000578
Jiri Pirko87002b02011-12-08 04:11:17 +0000579 vlan_vid_del(lowerdev, vid);
Jiri Pirko8e586132011-12-08 19:52:37 -0500580 return 0;
John Fastabend8d13e672011-06-06 04:27:16 +0000581}
582
stephen hemmingeredc7d572012-10-01 12:32:33 +0000583static int macvlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
John Fastabenddf8ef8f2012-04-15 06:44:37 +0000584 struct net_device *dev,
stephen hemminger6b6e2722012-09-17 10:03:26 +0000585 const unsigned char *addr,
John Fastabenddf8ef8f2012-04-15 06:44:37 +0000586 u16 flags)
587{
588 struct macvlan_dev *vlan = netdev_priv(dev);
589 int err = -EINVAL;
590
591 if (!vlan->port->passthru)
592 return -EOPNOTSUPP;
593
594 if (is_unicast_ether_addr(addr))
595 err = dev_uc_add_excl(dev, addr);
596 else if (is_multicast_ether_addr(addr))
597 err = dev_mc_add_excl(dev, addr);
598
599 return err;
600}
601
Vlad Yasevich1690be62013-02-13 12:00:18 +0000602static int macvlan_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
John Fastabenddf8ef8f2012-04-15 06:44:37 +0000603 struct net_device *dev,
stephen hemminger6b6e2722012-09-17 10:03:26 +0000604 const unsigned char *addr)
John Fastabenddf8ef8f2012-04-15 06:44:37 +0000605{
606 struct macvlan_dev *vlan = netdev_priv(dev);
607 int err = -EINVAL;
608
609 if (!vlan->port->passthru)
610 return -EOPNOTSUPP;
611
612 if (is_unicast_ether_addr(addr))
613 err = dev_uc_del(dev, addr);
614 else if (is_multicast_ether_addr(addr))
615 err = dev_mc_del(dev, addr);
616
617 return err;
618}
619
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700620static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
621 struct ethtool_drvinfo *drvinfo)
622{
Jiri Pirko7826d432013-01-06 00:44:26 +0000623 strlcpy(drvinfo->driver, "macvlan", sizeof(drvinfo->driver));
624 strlcpy(drvinfo->version, "0.1", sizeof(drvinfo->version));
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700625}
626
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700627static int macvlan_ethtool_get_settings(struct net_device *dev,
628 struct ethtool_cmd *cmd)
629{
630 const struct macvlan_dev *vlan = netdev_priv(dev);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000631
632 return __ethtool_get_settings(vlan->lowerdev, cmd);
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700633}
634
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700635static const struct ethtool_ops macvlan_ethtool_ops = {
636 .get_link = ethtool_op_get_link,
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700637 .get_settings = macvlan_ethtool_get_settings,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700638 .get_drvinfo = macvlan_ethtool_get_drvinfo,
639};
640
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800641static const struct net_device_ops macvlan_netdev_ops = {
642 .ndo_init = macvlan_init,
Eric Dumazetfccaf712009-11-17 08:53:49 +0000643 .ndo_uninit = macvlan_uninit,
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800644 .ndo_open = macvlan_open,
645 .ndo_stop = macvlan_stop,
Stephen Hemminger00829822008-11-20 20:14:53 -0800646 .ndo_start_xmit = macvlan_start_xmit,
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800647 .ndo_change_mtu = macvlan_change_mtu,
648 .ndo_change_rx_flags = macvlan_change_rx_flags,
649 .ndo_set_mac_address = macvlan_set_mac_address,
John Fastabenddf8ef8f2012-04-15 06:44:37 +0000650 .ndo_set_rx_mode = macvlan_set_mac_lists,
Eric Dumazetbc661542010-06-24 00:54:21 +0000651 .ndo_get_stats64 = macvlan_dev_get_stats64,
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800652 .ndo_validate_addr = eth_validate_addr,
John Fastabend8d13e672011-06-06 04:27:16 +0000653 .ndo_vlan_rx_add_vid = macvlan_vlan_rx_add_vid,
654 .ndo_vlan_rx_kill_vid = macvlan_vlan_rx_kill_vid,
John Fastabenddf8ef8f2012-04-15 06:44:37 +0000655 .ndo_fdb_add = macvlan_fdb_add,
656 .ndo_fdb_del = macvlan_fdb_del,
657 .ndo_fdb_dump = ndo_dflt_fdb_dump,
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800658};
659
Herbert Xu8a357472010-07-21 21:44:31 +0000660void macvlan_common_setup(struct net_device *dev)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700661{
662 ether_setup(dev);
663
Neil Horman550fd082011-07-26 06:05:38 +0000664 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800665 dev->netdev_ops = &macvlan_netdev_ops;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700666 dev->destructor = free_netdev;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700667 dev->header_ops = &macvlan_hard_header_ops,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700668 dev->ethtool_ops = &macvlan_ethtool_ops;
Herbert Xu8a357472010-07-21 21:44:31 +0000669}
670EXPORT_SYMBOL_GPL(macvlan_common_setup);
671
672static void macvlan_setup(struct net_device *dev)
673{
674 macvlan_common_setup(dev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700675 dev->tx_queue_len = 0;
676}
677
678static int macvlan_port_create(struct net_device *dev)
679{
680 struct macvlan_port *port;
681 unsigned int i;
Jiri Pirkoab95bfe2010-06-01 21:52:08 +0000682 int err;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700683
684 if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
685 return -EINVAL;
686
687 port = kzalloc(sizeof(*port), GFP_KERNEL);
688 if (port == NULL)
689 return -ENOMEM;
690
Sridhar Samudralaeb06acd2010-10-28 13:10:50 +0000691 port->passthru = false;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700692 port->dev = dev;
693 INIT_LIST_HEAD(&port->vlans);
694 for (i = 0; i < MACVLAN_HASH_SIZE; i++)
695 INIT_HLIST_HEAD(&port->vlan_hash[i]);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +0000696
Jiri Pirkoa35e2c12010-06-15 03:27:57 +0000697 err = netdev_rx_handler_register(dev, macvlan_handle_frame, port);
698 if (err)
Jiri Pirkoab95bfe2010-06-01 21:52:08 +0000699 kfree(port);
Eric Dumazetd9351562011-05-20 14:59:23 -0400700 else
701 dev->priv_flags |= IFF_MACVLAN_PORT;
Jiri Pirkoab95bfe2010-06-01 21:52:08 +0000702 return err;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700703}
704
705static void macvlan_port_destroy(struct net_device *dev)
706{
Jiri Pirkoa35e2c12010-06-15 03:27:57 +0000707 struct macvlan_port *port = macvlan_port_get(dev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700708
Jiri Pirkoa35e2c12010-06-15 03:27:57 +0000709 dev->priv_flags &= ~IFF_MACVLAN_PORT;
Jiri Pirkoab95bfe2010-06-01 21:52:08 +0000710 netdev_rx_handler_unregister(dev);
Lai Jiangshan6e070ae2011-03-18 12:00:07 +0800711 kfree_rcu(port, rcu);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700712}
713
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700714static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
715{
716 if (tb[IFLA_ADDRESS]) {
717 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
718 return -EINVAL;
719 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
720 return -EADDRNOTAVAIL;
721 }
Arnd Bergmann27c0b1a2009-11-26 06:07:11 +0000722
723 if (data && data[IFLA_MACVLAN_MODE]) {
724 switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) {
725 case MACVLAN_MODE_PRIVATE:
726 case MACVLAN_MODE_VEPA:
727 case MACVLAN_MODE_BRIDGE:
Sridhar Samudralaeb06acd2010-10-28 13:10:50 +0000728 case MACVLAN_MODE_PASSTHRU:
Arnd Bergmann27c0b1a2009-11-26 06:07:11 +0000729 break;
730 default:
731 return -EINVAL;
732 }
733 }
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700734 return 0;
735}
736
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000737int macvlan_common_newlink(struct net *src_net, struct net_device *dev,
738 struct nlattr *tb[], struct nlattr *data[],
739 int (*receive)(struct sk_buff *skb),
740 int (*forward)(struct net_device *dev,
741 struct sk_buff *skb))
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700742{
743 struct macvlan_dev *vlan = netdev_priv(dev);
744 struct macvlan_port *port;
745 struct net_device *lowerdev;
746 int err;
747
748 if (!tb[IFLA_LINK])
749 return -EINVAL;
750
Eric W. Biederman81adee42009-11-08 00:53:51 -0800751 lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700752 if (lowerdev == NULL)
753 return -ENODEV;
754
Eric Biedermanb0832a22009-03-13 13:15:37 -0700755 /* When creating macvlans on top of other macvlans - use
756 * the real device as the lowerdev.
Patrick McHardya6ca5f12008-01-10 22:39:28 -0800757 */
Eric Biedermanb0832a22009-03-13 13:15:37 -0700758 if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) {
759 struct macvlan_dev *lowervlan = netdev_priv(lowerdev);
760 lowerdev = lowervlan->lowerdev;
761 }
Patrick McHardya6ca5f12008-01-10 22:39:28 -0800762
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700763 if (!tb[IFLA_MTU])
764 dev->mtu = lowerdev->mtu;
765 else if (dev->mtu > lowerdev->mtu)
766 return -EINVAL;
767
768 if (!tb[IFLA_ADDRESS])
Danny Kukawka7ce5d222012-02-15 06:45:40 +0000769 eth_hw_addr_random(dev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700770
Jiri Pirkoa35e2c12010-06-15 03:27:57 +0000771 if (!macvlan_port_exists(lowerdev)) {
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700772 err = macvlan_port_create(lowerdev);
773 if (err < 0)
774 return err;
775 }
Jiri Pirkoa35e2c12010-06-15 03:27:57 +0000776 port = macvlan_port_get(lowerdev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700777
Sridhar Samudralaeb06acd2010-10-28 13:10:50 +0000778 /* Only 1 macvlan device can be created in passthru mode */
779 if (port->passthru)
780 return -EINVAL;
781
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700782 vlan->lowerdev = lowerdev;
783 vlan->dev = dev;
784 vlan->port = port;
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000785 vlan->receive = receive;
786 vlan->forward = forward;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700787
Arnd Bergmann27c0b1a2009-11-26 06:07:11 +0000788 vlan->mode = MACVLAN_MODE_VEPA;
789 if (data && data[IFLA_MACVLAN_MODE])
790 vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
791
John Fastabenddf8ef8f2012-04-15 06:44:37 +0000792 if (data && data[IFLA_MACVLAN_FLAGS])
793 vlan->flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
794
Sridhar Samudralaeb06acd2010-10-28 13:10:50 +0000795 if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
Eric W. Biedermand5cd92442011-03-21 18:22:22 -0700796 if (port->count)
Sridhar Samudralaeb06acd2010-10-28 13:10:50 +0000797 return -EINVAL;
798 port->passthru = true;
799 memcpy(dev->dev_addr, lowerdev->dev_addr, ETH_ALEN);
800 }
801
Jiri Pirko7cd43db2013-01-03 22:48:50 +0000802 err = netdev_upper_dev_link(lowerdev, dev);
803 if (err)
804 goto destroy_port;
805
Eric W. Biedermand5cd92442011-03-21 18:22:22 -0700806 port->count += 1;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700807 err = register_netdevice(dev);
808 if (err < 0)
Jiri Pirko7cd43db2013-01-03 22:48:50 +0000809 goto upper_dev_unlink;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700810
811 list_add_tail(&vlan->list, &port->vlans);
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -0800812 netif_stacked_transfer_operstate(lowerdev, dev);
Jiri Pirkof16d3d52010-05-24 07:02:25 +0000813
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700814 return 0;
Jiri Pirkof16d3d52010-05-24 07:02:25 +0000815
Jiri Pirko7cd43db2013-01-03 22:48:50 +0000816upper_dev_unlink:
817 netdev_upper_dev_unlink(lowerdev, dev);
Jiri Pirkof16d3d52010-05-24 07:02:25 +0000818destroy_port:
Eric W. Biedermand5cd92442011-03-21 18:22:22 -0700819 port->count -= 1;
820 if (!port->count)
Jiri Pirkof16d3d52010-05-24 07:02:25 +0000821 macvlan_port_destroy(lowerdev);
822
823 return err;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700824}
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000825EXPORT_SYMBOL_GPL(macvlan_common_newlink);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700826
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000827static int macvlan_newlink(struct net *src_net, struct net_device *dev,
828 struct nlattr *tb[], struct nlattr *data[])
829{
830 return macvlan_common_newlink(src_net, dev, tb, data,
831 netif_rx,
832 dev_forward_skb);
833}
834
835void macvlan_dellink(struct net_device *dev, struct list_head *head)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700836{
837 struct macvlan_dev *vlan = netdev_priv(dev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700838
839 list_del(&vlan->list);
Eric Dumazet23289a32009-10-27 07:06:36 +0000840 unregister_netdevice_queue(dev, head);
Jiri Pirko7cd43db2013-01-03 22:48:50 +0000841 netdev_upper_dev_unlink(vlan->lowerdev, dev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700842}
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000843EXPORT_SYMBOL_GPL(macvlan_dellink);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700844
Arnd Bergmann27c0b1a2009-11-26 06:07:11 +0000845static int macvlan_changelink(struct net_device *dev,
846 struct nlattr *tb[], struct nlattr *data[])
847{
848 struct macvlan_dev *vlan = netdev_priv(dev);
849 if (data && data[IFLA_MACVLAN_MODE])
850 vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
John Fastabenddf8ef8f2012-04-15 06:44:37 +0000851 if (data && data[IFLA_MACVLAN_FLAGS]) {
852 __u16 flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
853 bool promisc = (flags ^ vlan->flags) & MACVLAN_FLAG_NOPROMISC;
854
855 if (promisc && (flags & MACVLAN_FLAG_NOPROMISC))
856 dev_set_promiscuity(vlan->lowerdev, -1);
857 else if (promisc && !(flags & MACVLAN_FLAG_NOPROMISC))
858 dev_set_promiscuity(vlan->lowerdev, 1);
859 vlan->flags = flags;
860 }
Arnd Bergmann27c0b1a2009-11-26 06:07:11 +0000861 return 0;
862}
863
864static size_t macvlan_get_size(const struct net_device *dev)
865{
Eric Dumazet01fe9442013-01-17 13:30:49 -0800866 return (0
867 + nla_total_size(4) /* IFLA_MACVLAN_MODE */
868 + nla_total_size(2) /* IFLA_MACVLAN_FLAGS */
869 );
Arnd Bergmann27c0b1a2009-11-26 06:07:11 +0000870}
871
872static int macvlan_fill_info(struct sk_buff *skb,
873 const struct net_device *dev)
874{
875 struct macvlan_dev *vlan = netdev_priv(dev);
876
David S. Milleread9a762012-04-01 20:23:06 -0400877 if (nla_put_u32(skb, IFLA_MACVLAN_MODE, vlan->mode))
878 goto nla_put_failure;
John Fastabenddf8ef8f2012-04-15 06:44:37 +0000879 if (nla_put_u16(skb, IFLA_MACVLAN_FLAGS, vlan->flags))
880 goto nla_put_failure;
Arnd Bergmann27c0b1a2009-11-26 06:07:11 +0000881 return 0;
882
883nla_put_failure:
884 return -EMSGSIZE;
885}
886
887static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = {
John Fastabenddf8ef8f2012-04-15 06:44:37 +0000888 [IFLA_MACVLAN_MODE] = { .type = NLA_U32 },
889 [IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 },
Arnd Bergmann27c0b1a2009-11-26 06:07:11 +0000890};
891
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000892int macvlan_link_register(struct rtnl_link_ops *ops)
893{
894 /* common fields */
895 ops->priv_size = sizeof(struct macvlan_dev);
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000896 ops->validate = macvlan_validate;
897 ops->maxtype = IFLA_MACVLAN_MAX;
898 ops->policy = macvlan_policy;
899 ops->changelink = macvlan_changelink;
900 ops->get_size = macvlan_get_size;
901 ops->fill_info = macvlan_fill_info;
902
903 return rtnl_link_register(ops);
904};
905EXPORT_SYMBOL_GPL(macvlan_link_register);
906
907static struct rtnl_link_ops macvlan_link_ops = {
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700908 .kind = "macvlan",
Herbert Xu8a357472010-07-21 21:44:31 +0000909 .setup = macvlan_setup,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700910 .newlink = macvlan_newlink,
911 .dellink = macvlan_dellink,
912};
913
914static int macvlan_device_event(struct notifier_block *unused,
915 unsigned long event, void *ptr)
916{
917 struct net_device *dev = ptr;
918 struct macvlan_dev *vlan, *next;
919 struct macvlan_port *port;
Eric Dumazet226bd342011-05-08 23:17:57 +0000920 LIST_HEAD(list_kill);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700921
Jiri Pirkoa35e2c12010-06-15 03:27:57 +0000922 if (!macvlan_port_exists(dev))
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700923 return NOTIFY_DONE;
924
Jiri Pirkoa35e2c12010-06-15 03:27:57 +0000925 port = macvlan_port_get(dev);
926
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700927 switch (event) {
928 case NETDEV_CHANGE:
929 list_for_each_entry(vlan, &port->vlans, list)
Patrick Mullaneyfc4a7482009-12-03 15:59:22 -0800930 netif_stacked_transfer_operstate(vlan->lowerdev,
931 vlan->dev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700932 break;
933 case NETDEV_FEAT_CHANGE:
934 list_for_each_entry(vlan, &port->vlans, list) {
935 vlan->dev->features = dev->features & MACVLAN_FEATURES;
Patrick McHardy8c2acc52009-11-23 14:18:53 -0800936 vlan->dev->gso_max_size = dev->gso_max_size;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700937 netdev_features_change(vlan->dev);
938 }
939 break;
940 case NETDEV_UNREGISTER:
David Lamparter3b27e102010-09-17 03:22:19 +0000941 /* twiddle thumbs on netns device moves */
942 if (dev->reg_state != NETREG_UNREGISTERING)
943 break;
944
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700945 list_for_each_entry_safe(vlan, next, &port->vlans, list)
Eric Dumazet226bd342011-05-08 23:17:57 +0000946 vlan->dev->rtnl_link_ops->dellink(vlan->dev, &list_kill);
947 unregister_netdevice_many(&list_kill);
948 list_del(&list_kill);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700949 break;
Jiri Pirko1c01fe12010-03-10 10:30:19 +0000950 case NETDEV_PRE_TYPE_CHANGE:
951 /* Forbid underlaying device to change its type. */
952 return NOTIFY_BAD;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700953 }
954 return NOTIFY_DONE;
955}
956
957static struct notifier_block macvlan_notifier_block __read_mostly = {
958 .notifier_call = macvlan_device_event,
959};
960
961static int __init macvlan_init_module(void)
962{
963 int err;
964
965 register_netdevice_notifier(&macvlan_notifier_block);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700966
Arnd Bergmannfc0663d2010-01-30 12:23:40 +0000967 err = macvlan_link_register(&macvlan_link_ops);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700968 if (err < 0)
969 goto err1;
970 return 0;
971err1:
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700972 unregister_netdevice_notifier(&macvlan_notifier_block);
973 return err;
974}
975
976static void __exit macvlan_cleanup_module(void)
977{
978 rtnl_link_unregister(&macvlan_link_ops);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700979 unregister_netdevice_notifier(&macvlan_notifier_block);
980}
981
982module_init(macvlan_init_module);
983module_exit(macvlan_cleanup_module);
984
985MODULE_LICENSE("GPL");
986MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
987MODULE_DESCRIPTION("Driver for MAC address based VLANs");
988MODULE_ALIAS_RTNL_LINK("macvlan");