2 * Copyright (c) 2007 Patrick McHardy <kaber@trash.net>
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.
9 * The code this is based on carried the following copyright notice:
11 * (C) Copyright 2001-2006
12 * Alex Zeffertt, Cambridge Broadband Ltd, ajz@cambridgebroadband.com
13 * Re-worked by Ben Greear <greearb@candelatech.com>
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>
33 #define MACVLAN_HASH_SIZE (1 << BITS_PER_BYTE)
36 struct net_device *dev;
37 struct hlist_head vlan_hash[MACVLAN_HASH_SIZE];
38 struct list_head vlans;
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;
50 static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
51 const unsigned char *addr)
53 struct macvlan_dev *vlan;
56 hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[addr[5]], hlist) {
57 if (!compare_ether_addr(vlan->dev->dev_addr, addr))
63 static void macvlan_broadcast(struct sk_buff *skb,
64 const struct macvlan_port *port)
66 const struct ethhdr *eth = eth_hdr(skb);
67 const struct macvlan_dev *vlan;
69 struct net_device *dev;
73 for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
74 hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[i], hlist) {
77 nskb = skb_clone(skb, GFP_ATOMIC);
79 dev->stats.rx_errors++;
80 dev->stats.rx_dropped++;
84 dev->stats.rx_bytes += skb->len + ETH_HLEN;
85 dev->stats.rx_packets++;
86 dev->stats.multicast++;
87 dev->last_rx = jiffies;
90 if (!compare_ether_addr(eth->h_dest, dev->broadcast))
91 nskb->pkt_type = PACKET_BROADCAST;
93 nskb->pkt_type = PACKET_MULTICAST;
100 /* called under rcu_read_lock() from netif_receive_skb */
101 static struct sk_buff *macvlan_handle_frame(struct sk_buff *skb)
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;
108 port = rcu_dereference(skb->dev->macvlan_port);
112 if (is_multicast_ether_addr(eth->h_dest)) {
113 macvlan_broadcast(skb, port);
117 vlan = macvlan_hash_lookup(port, eth->h_dest);
122 if (unlikely(!(dev->flags & IFF_UP))) {
127 skb = skb_share_check(skb, GFP_ATOMIC);
129 dev->stats.rx_errors++;
130 dev->stats.rx_dropped++;
134 dev->stats.rx_bytes += skb->len + ETH_HLEN;
135 dev->stats.rx_packets++;
136 dev->last_rx = jiffies;
139 skb->pkt_type = PACKET_HOST;
145 static int macvlan_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
147 const struct macvlan_dev *vlan = netdev_priv(dev);
148 unsigned int len = skb->len;
151 skb->dev = vlan->lowerdev;
152 ret = dev_queue_xmit(skb);
154 if (likely(ret == NET_XMIT_SUCCESS)) {
155 dev->stats.tx_packets++;
156 dev->stats.tx_bytes += len;
158 dev->stats.tx_errors++;
159 dev->stats.tx_aborted_errors++;
164 static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
165 unsigned short type, const void *daddr,
166 const void *saddr, unsigned len)
168 const struct macvlan_dev *vlan = netdev_priv(dev);
169 struct net_device *lowerdev = vlan->lowerdev;
171 return dev_hard_header(skb, lowerdev, type, daddr,
172 saddr ? : dev->dev_addr, len);
175 static const struct header_ops macvlan_hard_header_ops = {
176 .create = macvlan_hard_header,
177 .rebuild = eth_rebuild_header,
178 .parse = eth_header_parse,
179 .cache = eth_header_cache,
180 .cache_update = eth_header_cache_update,
183 static int macvlan_open(struct net_device *dev)
185 struct macvlan_dev *vlan = netdev_priv(dev);
186 struct macvlan_port *port = vlan->port;
187 struct net_device *lowerdev = vlan->lowerdev;
190 err = dev_unicast_add(lowerdev, dev->dev_addr, ETH_ALEN);
193 if (dev->flags & IFF_ALLMULTI) {
194 err = dev_set_allmulti(lowerdev, 1);
199 hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[dev->dev_addr[5]]);
203 dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN);
208 static int macvlan_stop(struct net_device *dev)
210 struct macvlan_dev *vlan = netdev_priv(dev);
211 struct net_device *lowerdev = vlan->lowerdev;
213 dev_mc_unsync(lowerdev, dev);
214 if (dev->flags & IFF_ALLMULTI)
215 dev_set_allmulti(lowerdev, -1);
217 dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN);
219 hlist_del_rcu(&vlan->hlist);
224 static int macvlan_set_mac_address(struct net_device *dev, void *p)
226 struct macvlan_dev *vlan = netdev_priv(dev);
227 struct net_device *lowerdev = vlan->lowerdev;
228 struct sockaddr *addr = p;
231 if (!is_valid_ether_addr(addr->sa_data))
232 return -EADDRNOTAVAIL;
234 if (!(dev->flags & IFF_UP))
237 err = dev_unicast_add(lowerdev, addr->sa_data, ETH_ALEN);
240 dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN);
243 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
247 static void macvlan_change_rx_flags(struct net_device *dev, int change)
249 struct macvlan_dev *vlan = netdev_priv(dev);
250 struct net_device *lowerdev = vlan->lowerdev;
252 if (change & IFF_ALLMULTI)
253 dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
256 static void macvlan_set_multicast_list(struct net_device *dev)
258 struct macvlan_dev *vlan = netdev_priv(dev);
260 dev_mc_sync(vlan->lowerdev, dev);
263 static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
265 struct macvlan_dev *vlan = netdev_priv(dev);
267 if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
274 * macvlan network devices have devices nesting below it and are a special
275 * "super class" of normal network devices; split their locks off into a
276 * separate class since they always nest.
278 static struct lock_class_key macvlan_netdev_xmit_lock_key;
280 #define MACVLAN_FEATURES \
281 (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
282 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
283 NETIF_F_TSO_ECN | NETIF_F_TSO6)
285 #define MACVLAN_STATE_MASK \
286 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
288 static void macvlan_set_lockdep_class_one(struct netdev_queue *txq)
290 lockdep_set_class(&txq->_xmit_lock,
291 &macvlan_netdev_xmit_lock_key);
294 static void macvlan_set_lockdep_class(struct net_device *dev)
296 macvlan_set_lockdep_class_one(&dev->tx_queue);
299 static int macvlan_init(struct net_device *dev)
301 struct macvlan_dev *vlan = netdev_priv(dev);
302 const struct net_device *lowerdev = vlan->lowerdev;
304 dev->state = (dev->state & ~MACVLAN_STATE_MASK) |
305 (lowerdev->state & MACVLAN_STATE_MASK);
306 dev->features = lowerdev->features & MACVLAN_FEATURES;
307 dev->iflink = lowerdev->ifindex;
309 macvlan_set_lockdep_class(dev);
314 static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
315 struct ethtool_drvinfo *drvinfo)
317 snprintf(drvinfo->driver, 32, "macvlan");
318 snprintf(drvinfo->version, 32, "0.1");
321 static u32 macvlan_ethtool_get_rx_csum(struct net_device *dev)
323 const struct macvlan_dev *vlan = netdev_priv(dev);
324 struct net_device *lowerdev = vlan->lowerdev;
326 if (lowerdev->ethtool_ops->get_rx_csum == NULL)
328 return lowerdev->ethtool_ops->get_rx_csum(lowerdev);
331 static const struct ethtool_ops macvlan_ethtool_ops = {
332 .get_link = ethtool_op_get_link,
333 .get_rx_csum = macvlan_ethtool_get_rx_csum,
334 .get_drvinfo = macvlan_ethtool_get_drvinfo,
337 static void macvlan_setup(struct net_device *dev)
341 dev->init = macvlan_init;
342 dev->open = macvlan_open;
343 dev->stop = macvlan_stop;
344 dev->change_mtu = macvlan_change_mtu;
345 dev->change_rx_flags = macvlan_change_rx_flags;
346 dev->set_mac_address = macvlan_set_mac_address;
347 dev->set_multicast_list = macvlan_set_multicast_list;
348 dev->hard_start_xmit = macvlan_hard_start_xmit;
349 dev->destructor = free_netdev;
350 dev->header_ops = &macvlan_hard_header_ops,
351 dev->ethtool_ops = &macvlan_ethtool_ops;
352 dev->tx_queue_len = 0;
355 static int macvlan_port_create(struct net_device *dev)
357 struct macvlan_port *port;
360 if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
363 port = kzalloc(sizeof(*port), GFP_KERNEL);
368 INIT_LIST_HEAD(&port->vlans);
369 for (i = 0; i < MACVLAN_HASH_SIZE; i++)
370 INIT_HLIST_HEAD(&port->vlan_hash[i]);
371 rcu_assign_pointer(dev->macvlan_port, port);
375 static void macvlan_port_destroy(struct net_device *dev)
377 struct macvlan_port *port = dev->macvlan_port;
379 rcu_assign_pointer(dev->macvlan_port, NULL);
384 static void macvlan_transfer_operstate(struct net_device *dev)
386 struct macvlan_dev *vlan = netdev_priv(dev);
387 const struct net_device *lowerdev = vlan->lowerdev;
389 if (lowerdev->operstate == IF_OPER_DORMANT)
390 netif_dormant_on(dev);
392 netif_dormant_off(dev);
394 if (netif_carrier_ok(lowerdev)) {
395 if (!netif_carrier_ok(dev))
396 netif_carrier_on(dev);
398 if (netif_carrier_ok(dev))
399 netif_carrier_off(dev);
403 static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
405 if (tb[IFLA_ADDRESS]) {
406 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
408 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
409 return -EADDRNOTAVAIL;
414 static int macvlan_newlink(struct net_device *dev,
415 struct nlattr *tb[], struct nlattr *data[])
417 struct macvlan_dev *vlan = netdev_priv(dev);
418 struct macvlan_port *port;
419 struct net_device *lowerdev;
425 lowerdev = __dev_get_by_index(dev_net(dev), nla_get_u32(tb[IFLA_LINK]));
426 if (lowerdev == NULL)
429 /* Don't allow macvlans on top of other macvlans - its not really
430 * wrong, but lockdep can't handle it and its not useful for anything
431 * you couldn't do directly on top of the real device.
433 if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops)
437 dev->mtu = lowerdev->mtu;
438 else if (dev->mtu > lowerdev->mtu)
441 if (!tb[IFLA_ADDRESS])
442 random_ether_addr(dev->dev_addr);
444 if (lowerdev->macvlan_port == NULL) {
445 err = macvlan_port_create(lowerdev);
449 port = lowerdev->macvlan_port;
451 vlan->lowerdev = lowerdev;
455 err = register_netdevice(dev);
459 list_add_tail(&vlan->list, &port->vlans);
460 macvlan_transfer_operstate(dev);
464 static void macvlan_dellink(struct net_device *dev)
466 struct macvlan_dev *vlan = netdev_priv(dev);
467 struct macvlan_port *port = vlan->port;
469 list_del(&vlan->list);
470 unregister_netdevice(dev);
472 if (list_empty(&port->vlans))
473 macvlan_port_destroy(port->dev);
476 static struct rtnl_link_ops macvlan_link_ops __read_mostly = {
478 .priv_size = sizeof(struct macvlan_dev),
479 .setup = macvlan_setup,
480 .validate = macvlan_validate,
481 .newlink = macvlan_newlink,
482 .dellink = macvlan_dellink,
485 static int macvlan_device_event(struct notifier_block *unused,
486 unsigned long event, void *ptr)
488 struct net_device *dev = ptr;
489 struct macvlan_dev *vlan, *next;
490 struct macvlan_port *port;
492 port = dev->macvlan_port;
498 list_for_each_entry(vlan, &port->vlans, list)
499 macvlan_transfer_operstate(vlan->dev);
501 case NETDEV_FEAT_CHANGE:
502 list_for_each_entry(vlan, &port->vlans, list) {
503 vlan->dev->features = dev->features & MACVLAN_FEATURES;
504 netdev_features_change(vlan->dev);
507 case NETDEV_UNREGISTER:
508 list_for_each_entry_safe(vlan, next, &port->vlans, list)
509 macvlan_dellink(vlan->dev);
515 static struct notifier_block macvlan_notifier_block __read_mostly = {
516 .notifier_call = macvlan_device_event,
519 static int __init macvlan_init_module(void)
523 register_netdevice_notifier(&macvlan_notifier_block);
524 macvlan_handle_frame_hook = macvlan_handle_frame;
526 err = rtnl_link_register(&macvlan_link_ops);
531 macvlan_handle_frame_hook = NULL;
532 unregister_netdevice_notifier(&macvlan_notifier_block);
536 static void __exit macvlan_cleanup_module(void)
538 rtnl_link_unregister(&macvlan_link_ops);
539 macvlan_handle_frame_hook = NULL;
540 unregister_netdevice_notifier(&macvlan_notifier_block);
543 module_init(macvlan_init_module);
544 module_exit(macvlan_cleanup_module);
546 MODULE_LICENSE("GPL");
547 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
548 MODULE_DESCRIPTION("Driver for MAC address based VLANs");
549 MODULE_ALIAS_RTNL_LINK("macvlan");