blob: f651a816b280bfcb04d1d6fdda8d12e2819b4ce5 [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>
23#include <linux/list.h>
24#include <linux/notifier.h>
25#include <linux/netdevice.h>
26#include <linux/etherdevice.h>
27#include <linux/ethtool.h>
28#include <linux/if_arp.h>
29#include <linux/if_link.h>
30#include <linux/if_macvlan.h>
31#include <net/rtnetlink.h>
32
33#define MACVLAN_HASH_SIZE (1 << BITS_PER_BYTE)
34
35struct macvlan_port {
36 struct net_device *dev;
37 struct hlist_head vlan_hash[MACVLAN_HASH_SIZE];
38 struct list_head vlans;
39};
40
41struct macvlan_dev {
42 struct net_device *dev;
43 struct list_head list;
44 struct hlist_node hlist;
45 struct macvlan_port *port;
46 struct net_device *lowerdev;
47};
48
49
50static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
51 const unsigned char *addr)
52{
53 struct macvlan_dev *vlan;
54 struct hlist_node *n;
55
56 hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[addr[5]], hlist) {
57 if (!compare_ether_addr(vlan->dev->dev_addr, addr))
58 return vlan;
59 }
60 return NULL;
61}
62
63static void macvlan_broadcast(struct sk_buff *skb,
64 const struct macvlan_port *port)
65{
66 const struct ethhdr *eth = eth_hdr(skb);
67 const struct macvlan_dev *vlan;
68 struct hlist_node *n;
69 struct net_device *dev;
70 struct sk_buff *nskb;
71 unsigned int i;
72
73 for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
74 hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[i], hlist) {
75 dev = vlan->dev;
Patrick McHardyb863ceb2007-07-14 18:55:06 -070076
77 nskb = skb_clone(skb, GFP_ATOMIC);
78 if (nskb == NULL) {
79 dev->stats.rx_errors++;
80 dev->stats.rx_dropped++;
81 continue;
82 }
83
84 dev->stats.rx_bytes += skb->len + ETH_HLEN;
85 dev->stats.rx_packets++;
86 dev->stats.multicast++;
87 dev->last_rx = jiffies;
88
89 nskb->dev = dev;
90 if (!compare_ether_addr(eth->h_dest, dev->broadcast))
91 nskb->pkt_type = PACKET_BROADCAST;
92 else
93 nskb->pkt_type = PACKET_MULTICAST;
94
95 netif_rx(nskb);
96 }
97 }
98}
99
100/* called under rcu_read_lock() from netif_receive_skb */
101static struct sk_buff *macvlan_handle_frame(struct sk_buff *skb)
102{
103 const struct ethhdr *eth = eth_hdr(skb);
104 const struct macvlan_port *port;
105 const struct macvlan_dev *vlan;
106 struct net_device *dev;
107
108 port = rcu_dereference(skb->dev->macvlan_port);
109 if (port == NULL)
110 return skb;
111
112 if (is_multicast_ether_addr(eth->h_dest)) {
113 macvlan_broadcast(skb, port);
114 return skb;
115 }
116
117 vlan = macvlan_hash_lookup(port, eth->h_dest);
118 if (vlan == NULL)
119 return skb;
120
121 dev = vlan->dev;
122 if (unlikely(!(dev->flags & IFF_UP))) {
123 kfree_skb(skb);
124 return NULL;
125 }
126
127 skb = skb_share_check(skb, GFP_ATOMIC);
128 if (skb == NULL) {
129 dev->stats.rx_errors++;
130 dev->stats.rx_dropped++;
131 return NULL;
132 }
133
134 dev->stats.rx_bytes += skb->len + ETH_HLEN;
135 dev->stats.rx_packets++;
136 dev->last_rx = jiffies;
137
138 skb->dev = dev;
139 skb->pkt_type = PACKET_HOST;
140
141 netif_rx(skb);
142 return NULL;
143}
144
145static int macvlan_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
146{
147 const struct macvlan_dev *vlan = netdev_priv(dev);
148 unsigned int len = skb->len;
149 int ret;
150
151 skb->dev = vlan->lowerdev;
152 ret = dev_queue_xmit(skb);
153
154 if (likely(ret == NET_XMIT_SUCCESS)) {
155 dev->stats.tx_packets++;
156 dev->stats.tx_bytes += len;
157 } else {
158 dev->stats.tx_errors++;
159 dev->stats.tx_aborted_errors++;
160 }
161 return NETDEV_TX_OK;
162}
163
164static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700165 unsigned short type, const void *daddr,
166 const void *saddr, unsigned len)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700167{
168 const struct macvlan_dev *vlan = netdev_priv(dev);
169 struct net_device *lowerdev = vlan->lowerdev;
170
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700171 return dev_hard_header(skb, lowerdev, type, daddr,
172 saddr ? : dev->dev_addr, len);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700173}
174
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700175static const struct header_ops macvlan_hard_header_ops = {
176 .create = macvlan_hard_header,
177 .rebuild = eth_rebuild_header,
178 .parse = eth_header_parse,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700179 .cache = eth_header_cache,
180 .cache_update = eth_header_cache_update,
181};
182
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700183static int macvlan_open(struct net_device *dev)
184{
185 struct macvlan_dev *vlan = netdev_priv(dev);
186 struct macvlan_port *port = vlan->port;
187 struct net_device *lowerdev = vlan->lowerdev;
188 int err;
189
190 err = dev_unicast_add(lowerdev, dev->dev_addr, ETH_ALEN);
191 if (err < 0)
192 return err;
193 if (dev->flags & IFF_ALLMULTI)
194 dev_set_allmulti(lowerdev, 1);
195
196 hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[dev->dev_addr[5]]);
197 return 0;
198}
199
200static int macvlan_stop(struct net_device *dev)
201{
202 struct macvlan_dev *vlan = netdev_priv(dev);
203 struct net_device *lowerdev = vlan->lowerdev;
204
205 dev_mc_unsync(lowerdev, dev);
206 if (dev->flags & IFF_ALLMULTI)
207 dev_set_allmulti(lowerdev, -1);
208
209 dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN);
210
211 hlist_del_rcu(&vlan->hlist);
212 synchronize_rcu();
213 return 0;
214}
215
Patrick McHardyad5d20a2007-11-19 22:00:42 -0800216static int macvlan_set_mac_address(struct net_device *dev, void *p)
217{
218 struct macvlan_dev *vlan = netdev_priv(dev);
219 struct net_device *lowerdev = vlan->lowerdev;
220 struct sockaddr *addr = p;
221 int err;
222
223 if (!is_valid_ether_addr(addr->sa_data))
224 return -EADDRNOTAVAIL;
225
226 if (!(dev->flags & IFF_UP))
227 goto out;
228
229 err = dev_unicast_add(lowerdev, addr->sa_data, ETH_ALEN);
230 if (err < 0)
231 return err;
232 dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN);
233
234out:
235 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
236 return 0;
237}
238
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700239static void macvlan_change_rx_flags(struct net_device *dev, int change)
240{
241 struct macvlan_dev *vlan = netdev_priv(dev);
242 struct net_device *lowerdev = vlan->lowerdev;
243
244 if (change & IFF_ALLMULTI)
245 dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
246}
247
248static void macvlan_set_multicast_list(struct net_device *dev)
249{
250 struct macvlan_dev *vlan = netdev_priv(dev);
251
252 dev_mc_sync(vlan->lowerdev, dev);
253}
254
255static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
256{
257 struct macvlan_dev *vlan = netdev_priv(dev);
258
259 if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
260 return -EINVAL;
261 dev->mtu = new_mtu;
262 return 0;
263}
264
265/*
266 * macvlan network devices have devices nesting below it and are a special
267 * "super class" of normal network devices; split their locks off into a
268 * separate class since they always nest.
269 */
270static struct lock_class_key macvlan_netdev_xmit_lock_key;
271
272#define MACVLAN_FEATURES \
273 (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
274 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
275 NETIF_F_TSO_ECN | NETIF_F_TSO6)
276
277#define MACVLAN_STATE_MASK \
278 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
279
280static int macvlan_init(struct net_device *dev)
281{
282 struct macvlan_dev *vlan = netdev_priv(dev);
283 const struct net_device *lowerdev = vlan->lowerdev;
284
285 dev->state = (dev->state & ~MACVLAN_STATE_MASK) |
286 (lowerdev->state & MACVLAN_STATE_MASK);
287 dev->features = lowerdev->features & MACVLAN_FEATURES;
288 dev->iflink = lowerdev->ifindex;
289
290 lockdep_set_class(&dev->_xmit_lock, &macvlan_netdev_xmit_lock_key);
291 return 0;
292}
293
294static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
295 struct ethtool_drvinfo *drvinfo)
296{
297 snprintf(drvinfo->driver, 32, "macvlan");
298 snprintf(drvinfo->version, 32, "0.1");
299}
300
301static u32 macvlan_ethtool_get_rx_csum(struct net_device *dev)
302{
303 const struct macvlan_dev *vlan = netdev_priv(dev);
304 struct net_device *lowerdev = vlan->lowerdev;
305
306 if (lowerdev->ethtool_ops->get_rx_csum == NULL)
307 return 0;
308 return lowerdev->ethtool_ops->get_rx_csum(lowerdev);
309}
310
311static const struct ethtool_ops macvlan_ethtool_ops = {
312 .get_link = ethtool_op_get_link,
313 .get_rx_csum = macvlan_ethtool_get_rx_csum,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700314 .get_drvinfo = macvlan_ethtool_get_drvinfo,
315};
316
317static void macvlan_setup(struct net_device *dev)
318{
319 ether_setup(dev);
320
321 dev->init = macvlan_init;
322 dev->open = macvlan_open;
323 dev->stop = macvlan_stop;
324 dev->change_mtu = macvlan_change_mtu;
325 dev->change_rx_flags = macvlan_change_rx_flags;
Patrick McHardyad5d20a2007-11-19 22:00:42 -0800326 dev->set_mac_address = macvlan_set_mac_address;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700327 dev->set_multicast_list = macvlan_set_multicast_list;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700328 dev->hard_start_xmit = macvlan_hard_start_xmit;
329 dev->destructor = free_netdev;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700330 dev->header_ops = &macvlan_hard_header_ops,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700331 dev->ethtool_ops = &macvlan_ethtool_ops;
332 dev->tx_queue_len = 0;
333}
334
335static int macvlan_port_create(struct net_device *dev)
336{
337 struct macvlan_port *port;
338 unsigned int i;
339
340 if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
341 return -EINVAL;
342
343 port = kzalloc(sizeof(*port), GFP_KERNEL);
344 if (port == NULL)
345 return -ENOMEM;
346
347 port->dev = dev;
348 INIT_LIST_HEAD(&port->vlans);
349 for (i = 0; i < MACVLAN_HASH_SIZE; i++)
350 INIT_HLIST_HEAD(&port->vlan_hash[i]);
351 rcu_assign_pointer(dev->macvlan_port, port);
352 return 0;
353}
354
355static void macvlan_port_destroy(struct net_device *dev)
356{
357 struct macvlan_port *port = dev->macvlan_port;
358
359 rcu_assign_pointer(dev->macvlan_port, NULL);
360 synchronize_rcu();
361 kfree(port);
362}
363
364static void macvlan_transfer_operstate(struct net_device *dev)
365{
366 struct macvlan_dev *vlan = netdev_priv(dev);
367 const struct net_device *lowerdev = vlan->lowerdev;
368
369 if (lowerdev->operstate == IF_OPER_DORMANT)
370 netif_dormant_on(dev);
371 else
372 netif_dormant_off(dev);
373
374 if (netif_carrier_ok(lowerdev)) {
375 if (!netif_carrier_ok(dev))
376 netif_carrier_on(dev);
377 } else {
Patrick McHardyf12ca5f2008-01-21 00:47:08 -0800378 if (netif_carrier_ok(dev))
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700379 netif_carrier_off(dev);
380 }
381}
382
383static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
384{
385 if (tb[IFLA_ADDRESS]) {
386 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
387 return -EINVAL;
388 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
389 return -EADDRNOTAVAIL;
390 }
391 return 0;
392}
393
394static int macvlan_newlink(struct net_device *dev,
395 struct nlattr *tb[], struct nlattr *data[])
396{
397 struct macvlan_dev *vlan = netdev_priv(dev);
398 struct macvlan_port *port;
399 struct net_device *lowerdev;
400 int err;
401
402 if (!tb[IFLA_LINK])
403 return -EINVAL;
404
Eric W. Biederman881d9662007-09-17 11:56:21 -0700405 lowerdev = __dev_get_by_index(dev->nd_net, nla_get_u32(tb[IFLA_LINK]));
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700406 if (lowerdev == NULL)
407 return -ENODEV;
408
Patrick McHardya6ca5f12008-01-10 22:39:28 -0800409 /* Don't allow macvlans on top of other macvlans - its not really
410 * wrong, but lockdep can't handle it and its not useful for anything
411 * you couldn't do directly on top of the real device.
412 */
413 if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops)
414 return -ENODEV;
415
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700416 if (!tb[IFLA_MTU])
417 dev->mtu = lowerdev->mtu;
418 else if (dev->mtu > lowerdev->mtu)
419 return -EINVAL;
420
421 if (!tb[IFLA_ADDRESS])
422 random_ether_addr(dev->dev_addr);
423
424 if (lowerdev->macvlan_port == NULL) {
425 err = macvlan_port_create(lowerdev);
426 if (err < 0)
427 return err;
428 }
429 port = lowerdev->macvlan_port;
430
431 vlan->lowerdev = lowerdev;
432 vlan->dev = dev;
433 vlan->port = port;
434
435 err = register_netdevice(dev);
436 if (err < 0)
437 return err;
438
439 list_add_tail(&vlan->list, &port->vlans);
440 macvlan_transfer_operstate(dev);
441 return 0;
442}
443
444static void macvlan_dellink(struct net_device *dev)
445{
446 struct macvlan_dev *vlan = netdev_priv(dev);
447 struct macvlan_port *port = vlan->port;
448
449 list_del(&vlan->list);
450 unregister_netdevice(dev);
451
452 if (list_empty(&port->vlans))
453 macvlan_port_destroy(dev);
454}
455
456static struct rtnl_link_ops macvlan_link_ops __read_mostly = {
457 .kind = "macvlan",
458 .priv_size = sizeof(struct macvlan_dev),
459 .setup = macvlan_setup,
460 .validate = macvlan_validate,
461 .newlink = macvlan_newlink,
462 .dellink = macvlan_dellink,
463};
464
465static int macvlan_device_event(struct notifier_block *unused,
466 unsigned long event, void *ptr)
467{
468 struct net_device *dev = ptr;
469 struct macvlan_dev *vlan, *next;
470 struct macvlan_port *port;
471
472 port = dev->macvlan_port;
473 if (port == NULL)
474 return NOTIFY_DONE;
475
476 switch (event) {
477 case NETDEV_CHANGE:
478 list_for_each_entry(vlan, &port->vlans, list)
479 macvlan_transfer_operstate(vlan->dev);
480 break;
481 case NETDEV_FEAT_CHANGE:
482 list_for_each_entry(vlan, &port->vlans, list) {
483 vlan->dev->features = dev->features & MACVLAN_FEATURES;
484 netdev_features_change(vlan->dev);
485 }
486 break;
487 case NETDEV_UNREGISTER:
488 list_for_each_entry_safe(vlan, next, &port->vlans, list)
489 macvlan_dellink(vlan->dev);
490 break;
491 }
492 return NOTIFY_DONE;
493}
494
495static struct notifier_block macvlan_notifier_block __read_mostly = {
496 .notifier_call = macvlan_device_event,
497};
498
499static int __init macvlan_init_module(void)
500{
501 int err;
502
503 register_netdevice_notifier(&macvlan_notifier_block);
504 macvlan_handle_frame_hook = macvlan_handle_frame;
505
506 err = rtnl_link_register(&macvlan_link_ops);
507 if (err < 0)
508 goto err1;
509 return 0;
510err1:
Rami Rosen52913242008-01-31 16:56:03 -0800511 macvlan_handle_frame_hook = NULL;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700512 unregister_netdevice_notifier(&macvlan_notifier_block);
513 return err;
514}
515
516static void __exit macvlan_cleanup_module(void)
517{
518 rtnl_link_unregister(&macvlan_link_ops);
519 macvlan_handle_frame_hook = NULL;
520 unregister_netdevice_notifier(&macvlan_notifier_block);
521}
522
523module_init(macvlan_init_module);
524module_exit(macvlan_cleanup_module);
525
526MODULE_LICENSE("GPL");
527MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
528MODULE_DESCRIPTION("Driver for MAC address based VLANs");
529MODULE_ALIAS_RTNL_LINK("macvlan");