blob: 7e24b50486869c8295e19d8b7339b7d56d6ef909 [file] [log] [blame]
Patrick McHardyb863ceb2007-07-14 18:55:06 -07001/*
2 * Copyright (c) 2007 Patrick McHardy <kaber@trash.net>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * The code this is based on carried the following copyright notice:
10 * ---
11 * (C) Copyright 2001-2006
12 * Alex Zeffertt, Cambridge Broadband Ltd, ajz@cambridgebroadband.com
13 * Re-worked by Ben Greear <greearb@candelatech.com>
14 * ---
15 */
16#include <linux/kernel.h>
17#include <linux/types.h>
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/errno.h>
21#include <linux/slab.h>
22#include <linux/string.h>
Franck Bui-Huu82524742008-05-12 21:21:05 +020023#include <linux/rculist.h>
Patrick McHardyb863ceb2007-07-14 18:55:06 -070024#include <linux/notifier.h>
25#include <linux/netdevice.h>
26#include <linux/etherdevice.h>
27#include <linux/ethtool.h>
28#include <linux/if_arp.h>
29#include <linux/if_link.h>
30#include <linux/if_macvlan.h>
31#include <net/rtnetlink.h>
32
33#define MACVLAN_HASH_SIZE (1 << BITS_PER_BYTE)
34
35struct macvlan_port {
36 struct net_device *dev;
37 struct hlist_head vlan_hash[MACVLAN_HASH_SIZE];
38 struct list_head vlans;
39};
40
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
Patrick McHardyefbbced2008-11-26 15:30:48 -080073 if (skb->protocol == htons(ETH_P_PAUSE))
74 return;
75
Patrick McHardyb863ceb2007-07-14 18:55:06 -070076 for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
77 hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[i], hlist) {
78 dev = vlan->dev;
Patrick McHardyb863ceb2007-07-14 18:55:06 -070079
80 nskb = skb_clone(skb, GFP_ATOMIC);
81 if (nskb == NULL) {
82 dev->stats.rx_errors++;
83 dev->stats.rx_dropped++;
84 continue;
85 }
86
87 dev->stats.rx_bytes += skb->len + ETH_HLEN;
88 dev->stats.rx_packets++;
89 dev->stats.multicast++;
Patrick McHardyb863ceb2007-07-14 18:55:06 -070090
91 nskb->dev = dev;
92 if (!compare_ether_addr(eth->h_dest, dev->broadcast))
93 nskb->pkt_type = PACKET_BROADCAST;
94 else
95 nskb->pkt_type = PACKET_MULTICAST;
96
97 netif_rx(nskb);
98 }
99 }
100}
101
102/* called under rcu_read_lock() from netif_receive_skb */
103static struct sk_buff *macvlan_handle_frame(struct sk_buff *skb)
104{
105 const struct ethhdr *eth = eth_hdr(skb);
106 const struct macvlan_port *port;
107 const struct macvlan_dev *vlan;
108 struct net_device *dev;
109
110 port = rcu_dereference(skb->dev->macvlan_port);
111 if (port == NULL)
112 return skb;
113
114 if (is_multicast_ether_addr(eth->h_dest)) {
115 macvlan_broadcast(skb, port);
116 return skb;
117 }
118
119 vlan = macvlan_hash_lookup(port, eth->h_dest);
120 if (vlan == NULL)
121 return skb;
122
123 dev = vlan->dev;
124 if (unlikely(!(dev->flags & IFF_UP))) {
125 kfree_skb(skb);
126 return NULL;
127 }
128
129 skb = skb_share_check(skb, GFP_ATOMIC);
130 if (skb == NULL) {
131 dev->stats.rx_errors++;
132 dev->stats.rx_dropped++;
133 return NULL;
134 }
135
136 dev->stats.rx_bytes += skb->len + ETH_HLEN;
137 dev->stats.rx_packets++;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700138
139 skb->dev = dev;
140 skb->pkt_type = PACKET_HOST;
141
142 netif_rx(skb);
143 return NULL;
144}
145
Stephen Hemminger00829822008-11-20 20:14:53 -0800146static int macvlan_start_xmit(struct sk_buff *skb, struct net_device *dev)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700147{
148 const struct macvlan_dev *vlan = netdev_priv(dev);
149 unsigned int len = skb->len;
150 int ret;
151
152 skb->dev = vlan->lowerdev;
153 ret = dev_queue_xmit(skb);
154
155 if (likely(ret == NET_XMIT_SUCCESS)) {
156 dev->stats.tx_packets++;
157 dev->stats.tx_bytes += len;
158 } else {
159 dev->stats.tx_errors++;
160 dev->stats.tx_aborted_errors++;
161 }
162 return NETDEV_TX_OK;
163}
164
165static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700166 unsigned short type, const void *daddr,
167 const void *saddr, unsigned len)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700168{
169 const struct macvlan_dev *vlan = netdev_priv(dev);
170 struct net_device *lowerdev = vlan->lowerdev;
171
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700172 return dev_hard_header(skb, lowerdev, type, daddr,
173 saddr ? : dev->dev_addr, len);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700174}
175
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700176static const struct header_ops macvlan_hard_header_ops = {
177 .create = macvlan_hard_header,
178 .rebuild = eth_rebuild_header,
179 .parse = eth_header_parse,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700180 .cache = eth_header_cache,
181 .cache_update = eth_header_cache_update,
182};
183
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700184static int macvlan_open(struct net_device *dev)
185{
186 struct macvlan_dev *vlan = netdev_priv(dev);
187 struct macvlan_port *port = vlan->port;
188 struct net_device *lowerdev = vlan->lowerdev;
189 int err;
190
191 err = dev_unicast_add(lowerdev, dev->dev_addr, ETH_ALEN);
192 if (err < 0)
Wang Chenb89fb7d2008-07-14 20:57:07 -0700193 goto out;
194 if (dev->flags & IFF_ALLMULTI) {
195 err = dev_set_allmulti(lowerdev, 1);
196 if (err < 0)
197 goto del_unicast;
198 }
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700199
200 hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[dev->dev_addr[5]]);
201 return 0;
Wang Chenb89fb7d2008-07-14 20:57:07 -0700202
203del_unicast:
204 dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN);
205out:
206 return err;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700207}
208
209static int macvlan_stop(struct net_device *dev)
210{
211 struct macvlan_dev *vlan = netdev_priv(dev);
212 struct net_device *lowerdev = vlan->lowerdev;
213
214 dev_mc_unsync(lowerdev, dev);
215 if (dev->flags & IFF_ALLMULTI)
216 dev_set_allmulti(lowerdev, -1);
217
218 dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN);
219
220 hlist_del_rcu(&vlan->hlist);
221 synchronize_rcu();
222 return 0;
223}
224
Patrick McHardyad5d20a2007-11-19 22:00:42 -0800225static int macvlan_set_mac_address(struct net_device *dev, void *p)
226{
227 struct macvlan_dev *vlan = netdev_priv(dev);
228 struct net_device *lowerdev = vlan->lowerdev;
229 struct sockaddr *addr = p;
230 int err;
231
232 if (!is_valid_ether_addr(addr->sa_data))
233 return -EADDRNOTAVAIL;
234
235 if (!(dev->flags & IFF_UP))
236 goto out;
237
238 err = dev_unicast_add(lowerdev, addr->sa_data, ETH_ALEN);
239 if (err < 0)
240 return err;
241 dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN);
242
243out:
244 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
245 return 0;
246}
247
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700248static void macvlan_change_rx_flags(struct net_device *dev, int change)
249{
250 struct macvlan_dev *vlan = netdev_priv(dev);
251 struct net_device *lowerdev = vlan->lowerdev;
252
253 if (change & IFF_ALLMULTI)
254 dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
255}
256
257static void macvlan_set_multicast_list(struct net_device *dev)
258{
259 struct macvlan_dev *vlan = netdev_priv(dev);
260
261 dev_mc_sync(vlan->lowerdev, dev);
262}
263
264static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
265{
266 struct macvlan_dev *vlan = netdev_priv(dev);
267
268 if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
269 return -EINVAL;
270 dev->mtu = new_mtu;
271 return 0;
272}
273
274/*
275 * macvlan network devices have devices nesting below it and are a special
276 * "super class" of normal network devices; split their locks off into a
277 * separate class since they always nest.
278 */
279static struct lock_class_key macvlan_netdev_xmit_lock_key;
David S. Millercf508b12008-07-22 14:16:42 -0700280static struct lock_class_key macvlan_netdev_addr_lock_key;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700281
282#define MACVLAN_FEATURES \
283 (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
284 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
285 NETIF_F_TSO_ECN | NETIF_F_TSO6)
286
287#define MACVLAN_STATE_MASK \
288 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
289
David S. Millere8a04642008-07-17 00:34:19 -0700290static void macvlan_set_lockdep_class_one(struct net_device *dev,
291 struct netdev_queue *txq,
292 void *_unused)
David S. Millerc773e842008-07-08 23:13:53 -0700293{
294 lockdep_set_class(&txq->_xmit_lock,
295 &macvlan_netdev_xmit_lock_key);
296}
297
298static void macvlan_set_lockdep_class(struct net_device *dev)
299{
David S. Millercf508b12008-07-22 14:16:42 -0700300 lockdep_set_class(&dev->addr_list_lock,
301 &macvlan_netdev_addr_lock_key);
David S. Millere8a04642008-07-17 00:34:19 -0700302 netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL);
David S. Millerc773e842008-07-08 23:13:53 -0700303}
304
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700305static int macvlan_init(struct net_device *dev)
306{
307 struct macvlan_dev *vlan = netdev_priv(dev);
308 const struct net_device *lowerdev = vlan->lowerdev;
309
310 dev->state = (dev->state & ~MACVLAN_STATE_MASK) |
311 (lowerdev->state & MACVLAN_STATE_MASK);
312 dev->features = lowerdev->features & MACVLAN_FEATURES;
313 dev->iflink = lowerdev->ifindex;
314
David S. Millerc773e842008-07-08 23:13:53 -0700315 macvlan_set_lockdep_class(dev);
316
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700317 return 0;
318}
319
320static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
321 struct ethtool_drvinfo *drvinfo)
322{
323 snprintf(drvinfo->driver, 32, "macvlan");
324 snprintf(drvinfo->version, 32, "0.1");
325}
326
327static u32 macvlan_ethtool_get_rx_csum(struct net_device *dev)
328{
329 const struct macvlan_dev *vlan = netdev_priv(dev);
330 struct net_device *lowerdev = vlan->lowerdev;
331
332 if (lowerdev->ethtool_ops->get_rx_csum == NULL)
333 return 0;
334 return lowerdev->ethtool_ops->get_rx_csum(lowerdev);
335}
336
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700337static int macvlan_ethtool_get_settings(struct net_device *dev,
338 struct ethtool_cmd *cmd)
339{
340 const struct macvlan_dev *vlan = netdev_priv(dev);
341 struct net_device *lowerdev = vlan->lowerdev;
342
343 if (!lowerdev->ethtool_ops->get_settings)
344 return -EOPNOTSUPP;
345
346 return lowerdev->ethtool_ops->get_settings(lowerdev, cmd);
347}
348
349static u32 macvlan_ethtool_get_flags(struct net_device *dev)
350{
351 const struct macvlan_dev *vlan = netdev_priv(dev);
352 struct net_device *lowerdev = vlan->lowerdev;
353
354 if (!lowerdev->ethtool_ops->get_flags)
355 return 0;
356 return lowerdev->ethtool_ops->get_flags(lowerdev);
357}
358
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700359static const struct ethtool_ops macvlan_ethtool_ops = {
360 .get_link = ethtool_op_get_link,
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700361 .get_settings = macvlan_ethtool_get_settings,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700362 .get_rx_csum = macvlan_ethtool_get_rx_csum,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700363 .get_drvinfo = macvlan_ethtool_get_drvinfo,
Stephen Hemminger9edb8bb2008-10-29 15:31:53 -0700364 .get_flags = macvlan_ethtool_get_flags,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700365};
366
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800367static const struct net_device_ops macvlan_netdev_ops = {
368 .ndo_init = macvlan_init,
369 .ndo_open = macvlan_open,
370 .ndo_stop = macvlan_stop,
Stephen Hemminger00829822008-11-20 20:14:53 -0800371 .ndo_start_xmit = macvlan_start_xmit,
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800372 .ndo_change_mtu = macvlan_change_mtu,
373 .ndo_change_rx_flags = macvlan_change_rx_flags,
374 .ndo_set_mac_address = macvlan_set_mac_address,
375 .ndo_set_multicast_list = macvlan_set_multicast_list,
376 .ndo_validate_addr = eth_validate_addr,
377};
378
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700379static void macvlan_setup(struct net_device *dev)
380{
381 ether_setup(dev);
382
Stephen Hemminger54a30c92008-11-19 21:51:06 -0800383 dev->netdev_ops = &macvlan_netdev_ops;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700384 dev->destructor = free_netdev;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700385 dev->header_ops = &macvlan_hard_header_ops,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700386 dev->ethtool_ops = &macvlan_ethtool_ops;
387 dev->tx_queue_len = 0;
388}
389
390static int macvlan_port_create(struct net_device *dev)
391{
392 struct macvlan_port *port;
393 unsigned int i;
394
395 if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
396 return -EINVAL;
397
398 port = kzalloc(sizeof(*port), GFP_KERNEL);
399 if (port == NULL)
400 return -ENOMEM;
401
402 port->dev = dev;
403 INIT_LIST_HEAD(&port->vlans);
404 for (i = 0; i < MACVLAN_HASH_SIZE; i++)
405 INIT_HLIST_HEAD(&port->vlan_hash[i]);
406 rcu_assign_pointer(dev->macvlan_port, port);
407 return 0;
408}
409
410static void macvlan_port_destroy(struct net_device *dev)
411{
412 struct macvlan_port *port = dev->macvlan_port;
413
414 rcu_assign_pointer(dev->macvlan_port, NULL);
415 synchronize_rcu();
416 kfree(port);
417}
418
419static void macvlan_transfer_operstate(struct net_device *dev)
420{
421 struct macvlan_dev *vlan = netdev_priv(dev);
422 const struct net_device *lowerdev = vlan->lowerdev;
423
424 if (lowerdev->operstate == IF_OPER_DORMANT)
425 netif_dormant_on(dev);
426 else
427 netif_dormant_off(dev);
428
429 if (netif_carrier_ok(lowerdev)) {
430 if (!netif_carrier_ok(dev))
431 netif_carrier_on(dev);
432 } else {
Patrick McHardyf12ca5f2008-01-21 00:47:08 -0800433 if (netif_carrier_ok(dev))
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700434 netif_carrier_off(dev);
435 }
436}
437
438static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
439{
440 if (tb[IFLA_ADDRESS]) {
441 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
442 return -EINVAL;
443 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
444 return -EADDRNOTAVAIL;
445 }
446 return 0;
447}
448
449static int macvlan_newlink(struct net_device *dev,
450 struct nlattr *tb[], struct nlattr *data[])
451{
452 struct macvlan_dev *vlan = netdev_priv(dev);
453 struct macvlan_port *port;
454 struct net_device *lowerdev;
455 int err;
456
457 if (!tb[IFLA_LINK])
458 return -EINVAL;
459
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900460 lowerdev = __dev_get_by_index(dev_net(dev), nla_get_u32(tb[IFLA_LINK]));
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700461 if (lowerdev == NULL)
462 return -ENODEV;
463
Patrick McHardya6ca5f12008-01-10 22:39:28 -0800464 /* Don't allow macvlans on top of other macvlans - its not really
465 * wrong, but lockdep can't handle it and its not useful for anything
466 * you couldn't do directly on top of the real device.
467 */
468 if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops)
469 return -ENODEV;
470
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700471 if (!tb[IFLA_MTU])
472 dev->mtu = lowerdev->mtu;
473 else if (dev->mtu > lowerdev->mtu)
474 return -EINVAL;
475
476 if (!tb[IFLA_ADDRESS])
477 random_ether_addr(dev->dev_addr);
478
479 if (lowerdev->macvlan_port == NULL) {
480 err = macvlan_port_create(lowerdev);
481 if (err < 0)
482 return err;
483 }
484 port = lowerdev->macvlan_port;
485
486 vlan->lowerdev = lowerdev;
487 vlan->dev = dev;
488 vlan->port = port;
489
490 err = register_netdevice(dev);
491 if (err < 0)
492 return err;
493
494 list_add_tail(&vlan->list, &port->vlans);
495 macvlan_transfer_operstate(dev);
496 return 0;
497}
498
499static void macvlan_dellink(struct net_device *dev)
500{
501 struct macvlan_dev *vlan = netdev_priv(dev);
502 struct macvlan_port *port = vlan->port;
503
504 list_del(&vlan->list);
505 unregister_netdevice(dev);
506
507 if (list_empty(&port->vlans))
Patrick McHardy73120962008-05-08 01:13:31 -0700508 macvlan_port_destroy(port->dev);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700509}
510
511static struct rtnl_link_ops macvlan_link_ops __read_mostly = {
512 .kind = "macvlan",
513 .priv_size = sizeof(struct macvlan_dev),
514 .setup = macvlan_setup,
515 .validate = macvlan_validate,
516 .newlink = macvlan_newlink,
517 .dellink = macvlan_dellink,
518};
519
520static int macvlan_device_event(struct notifier_block *unused,
521 unsigned long event, void *ptr)
522{
523 struct net_device *dev = ptr;
524 struct macvlan_dev *vlan, *next;
525 struct macvlan_port *port;
526
527 port = dev->macvlan_port;
528 if (port == NULL)
529 return NOTIFY_DONE;
530
531 switch (event) {
532 case NETDEV_CHANGE:
533 list_for_each_entry(vlan, &port->vlans, list)
534 macvlan_transfer_operstate(vlan->dev);
535 break;
536 case NETDEV_FEAT_CHANGE:
537 list_for_each_entry(vlan, &port->vlans, list) {
538 vlan->dev->features = dev->features & MACVLAN_FEATURES;
539 netdev_features_change(vlan->dev);
540 }
541 break;
542 case NETDEV_UNREGISTER:
543 list_for_each_entry_safe(vlan, next, &port->vlans, list)
544 macvlan_dellink(vlan->dev);
545 break;
546 }
547 return NOTIFY_DONE;
548}
549
550static struct notifier_block macvlan_notifier_block __read_mostly = {
551 .notifier_call = macvlan_device_event,
552};
553
554static int __init macvlan_init_module(void)
555{
556 int err;
557
558 register_netdevice_notifier(&macvlan_notifier_block);
559 macvlan_handle_frame_hook = macvlan_handle_frame;
560
561 err = rtnl_link_register(&macvlan_link_ops);
562 if (err < 0)
563 goto err1;
564 return 0;
565err1:
Rami Rosen52913242008-01-31 16:56:03 -0800566 macvlan_handle_frame_hook = NULL;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700567 unregister_netdevice_notifier(&macvlan_notifier_block);
568 return err;
569}
570
571static void __exit macvlan_cleanup_module(void)
572{
573 rtnl_link_unregister(&macvlan_link_ops);
574 macvlan_handle_frame_hook = NULL;
575 unregister_netdevice_notifier(&macvlan_notifier_block);
576}
577
578module_init(macvlan_init_module);
579module_exit(macvlan_cleanup_module);
580
581MODULE_LICENSE("GPL");
582MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
583MODULE_DESCRIPTION("Driver for MAC address based VLANs");
584MODULE_ALIAS_RTNL_LINK("macvlan");