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) {
76 if (unlikely(!(dev->flags & IFF_UP)))
79 nskb = skb_clone(skb, GFP_ATOMIC);
81 dev->stats.rx_errors++;
82 dev->stats.rx_dropped++;
86 dev->stats.rx_bytes += skb->len + ETH_HLEN;
87 dev->stats.rx_packets++;
88 dev->stats.multicast++;
89 dev->last_rx = jiffies;
92 if (!compare_ether_addr(eth->h_dest, dev->broadcast))
93 nskb->pkt_type = PACKET_BROADCAST;
95 nskb->pkt_type = PACKET_MULTICAST;
102 /* called under rcu_read_lock() from netif_receive_skb */
103 static struct sk_buff *macvlan_handle_frame(struct sk_buff *skb)
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;
110 port = rcu_dereference(skb->dev->macvlan_port);
114 if (is_multicast_ether_addr(eth->h_dest)) {
115 macvlan_broadcast(skb, port);
119 vlan = macvlan_hash_lookup(port, eth->h_dest);
124 if (unlikely(!(dev->flags & IFF_UP))) {
129 skb = skb_share_check(skb, GFP_ATOMIC);
131 dev->stats.rx_errors++;
132 dev->stats.rx_dropped++;
136 dev->stats.rx_bytes += skb->len + ETH_HLEN;
137 dev->stats.rx_packets++;
138 dev->last_rx = jiffies;
141 skb->pkt_type = PACKET_HOST;
147 static int macvlan_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
149 const struct macvlan_dev *vlan = netdev_priv(dev);
150 unsigned int len = skb->len;
153 skb->dev = vlan->lowerdev;
154 ret = dev_queue_xmit(skb);
156 if (likely(ret == NET_XMIT_SUCCESS)) {
157 dev->stats.tx_packets++;
158 dev->stats.tx_bytes += len;
160 dev->stats.tx_errors++;
161 dev->stats.tx_aborted_errors++;
166 static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
167 unsigned short type, const void *daddr,
168 const void *saddr, unsigned len)
170 const struct macvlan_dev *vlan = netdev_priv(dev);
171 struct net_device *lowerdev = vlan->lowerdev;
173 return dev_hard_header(skb, lowerdev, type, daddr,
174 saddr ? : dev->dev_addr, len);
177 static const struct header_ops macvlan_hard_header_ops = {
178 .create = macvlan_hard_header,
179 .rebuild = eth_rebuild_header,
180 .parse = eth_header_parse,
181 .rebuild = eth_rebuild_header,
182 .cache = eth_header_cache,
183 .cache_update = eth_header_cache_update,
186 static int macvlan_open(struct net_device *dev)
188 struct macvlan_dev *vlan = netdev_priv(dev);
189 struct macvlan_port *port = vlan->port;
190 struct net_device *lowerdev = vlan->lowerdev;
193 err = dev_unicast_add(lowerdev, dev->dev_addr, ETH_ALEN);
196 if (dev->flags & IFF_ALLMULTI)
197 dev_set_allmulti(lowerdev, 1);
199 hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[dev->dev_addr[5]]);
203 static int macvlan_stop(struct net_device *dev)
205 struct macvlan_dev *vlan = netdev_priv(dev);
206 struct net_device *lowerdev = vlan->lowerdev;
208 dev_mc_unsync(lowerdev, dev);
209 if (dev->flags & IFF_ALLMULTI)
210 dev_set_allmulti(lowerdev, -1);
212 dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN);
214 hlist_del_rcu(&vlan->hlist);
219 static void macvlan_change_rx_flags(struct net_device *dev, int change)
221 struct macvlan_dev *vlan = netdev_priv(dev);
222 struct net_device *lowerdev = vlan->lowerdev;
224 if (change & IFF_ALLMULTI)
225 dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
228 static void macvlan_set_multicast_list(struct net_device *dev)
230 struct macvlan_dev *vlan = netdev_priv(dev);
232 dev_mc_sync(vlan->lowerdev, dev);
235 static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
237 struct macvlan_dev *vlan = netdev_priv(dev);
239 if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
246 * macvlan network devices have devices nesting below it and are a special
247 * "super class" of normal network devices; split their locks off into a
248 * separate class since they always nest.
250 static struct lock_class_key macvlan_netdev_xmit_lock_key;
252 #define MACVLAN_FEATURES \
253 (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
254 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
255 NETIF_F_TSO_ECN | NETIF_F_TSO6)
257 #define MACVLAN_STATE_MASK \
258 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
260 static int macvlan_init(struct net_device *dev)
262 struct macvlan_dev *vlan = netdev_priv(dev);
263 const struct net_device *lowerdev = vlan->lowerdev;
265 dev->state = (dev->state & ~MACVLAN_STATE_MASK) |
266 (lowerdev->state & MACVLAN_STATE_MASK);
267 dev->features = lowerdev->features & MACVLAN_FEATURES;
268 dev->iflink = lowerdev->ifindex;
270 lockdep_set_class(&dev->_xmit_lock, &macvlan_netdev_xmit_lock_key);
274 static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
275 struct ethtool_drvinfo *drvinfo)
277 snprintf(drvinfo->driver, 32, "macvlan");
278 snprintf(drvinfo->version, 32, "0.1");
281 static u32 macvlan_ethtool_get_rx_csum(struct net_device *dev)
283 const struct macvlan_dev *vlan = netdev_priv(dev);
284 struct net_device *lowerdev = vlan->lowerdev;
286 if (lowerdev->ethtool_ops->get_rx_csum == NULL)
288 return lowerdev->ethtool_ops->get_rx_csum(lowerdev);
291 static const struct ethtool_ops macvlan_ethtool_ops = {
292 .get_link = ethtool_op_get_link,
293 .get_rx_csum = macvlan_ethtool_get_rx_csum,
294 .get_drvinfo = macvlan_ethtool_get_drvinfo,
297 static void macvlan_setup(struct net_device *dev)
301 dev->init = macvlan_init;
302 dev->open = macvlan_open;
303 dev->stop = macvlan_stop;
304 dev->change_mtu = macvlan_change_mtu;
305 dev->change_rx_flags = macvlan_change_rx_flags;
306 dev->set_multicast_list = macvlan_set_multicast_list;
307 dev->hard_start_xmit = macvlan_hard_start_xmit;
308 dev->destructor = free_netdev;
309 dev->header_ops = &macvlan_hard_header_ops,
310 dev->ethtool_ops = &macvlan_ethtool_ops;
311 dev->tx_queue_len = 0;
314 static int macvlan_port_create(struct net_device *dev)
316 struct macvlan_port *port;
319 if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
322 port = kzalloc(sizeof(*port), GFP_KERNEL);
327 INIT_LIST_HEAD(&port->vlans);
328 for (i = 0; i < MACVLAN_HASH_SIZE; i++)
329 INIT_HLIST_HEAD(&port->vlan_hash[i]);
330 rcu_assign_pointer(dev->macvlan_port, port);
334 static void macvlan_port_destroy(struct net_device *dev)
336 struct macvlan_port *port = dev->macvlan_port;
338 rcu_assign_pointer(dev->macvlan_port, NULL);
343 static void macvlan_transfer_operstate(struct net_device *dev)
345 struct macvlan_dev *vlan = netdev_priv(dev);
346 const struct net_device *lowerdev = vlan->lowerdev;
348 if (lowerdev->operstate == IF_OPER_DORMANT)
349 netif_dormant_on(dev);
351 netif_dormant_off(dev);
353 if (netif_carrier_ok(lowerdev)) {
354 if (!netif_carrier_ok(dev))
355 netif_carrier_on(dev);
357 if (netif_carrier_ok(lowerdev))
358 netif_carrier_off(dev);
362 static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
364 if (tb[IFLA_ADDRESS]) {
365 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
367 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
368 return -EADDRNOTAVAIL;
373 static int macvlan_newlink(struct net_device *dev,
374 struct nlattr *tb[], struct nlattr *data[])
376 struct macvlan_dev *vlan = netdev_priv(dev);
377 struct macvlan_port *port;
378 struct net_device *lowerdev;
384 lowerdev = __dev_get_by_index(dev->nd_net, nla_get_u32(tb[IFLA_LINK]));
385 if (lowerdev == NULL)
389 dev->mtu = lowerdev->mtu;
390 else if (dev->mtu > lowerdev->mtu)
393 if (!tb[IFLA_ADDRESS])
394 random_ether_addr(dev->dev_addr);
396 if (lowerdev->macvlan_port == NULL) {
397 err = macvlan_port_create(lowerdev);
401 port = lowerdev->macvlan_port;
403 vlan->lowerdev = lowerdev;
407 err = register_netdevice(dev);
411 list_add_tail(&vlan->list, &port->vlans);
412 macvlan_transfer_operstate(dev);
416 static void macvlan_dellink(struct net_device *dev)
418 struct macvlan_dev *vlan = netdev_priv(dev);
419 struct macvlan_port *port = vlan->port;
421 list_del(&vlan->list);
422 unregister_netdevice(dev);
424 if (list_empty(&port->vlans))
425 macvlan_port_destroy(dev);
428 static struct rtnl_link_ops macvlan_link_ops __read_mostly = {
430 .priv_size = sizeof(struct macvlan_dev),
431 .setup = macvlan_setup,
432 .validate = macvlan_validate,
433 .newlink = macvlan_newlink,
434 .dellink = macvlan_dellink,
437 static int macvlan_device_event(struct notifier_block *unused,
438 unsigned long event, void *ptr)
440 struct net_device *dev = ptr;
441 struct macvlan_dev *vlan, *next;
442 struct macvlan_port *port;
444 port = dev->macvlan_port;
450 list_for_each_entry(vlan, &port->vlans, list)
451 macvlan_transfer_operstate(vlan->dev);
453 case NETDEV_FEAT_CHANGE:
454 list_for_each_entry(vlan, &port->vlans, list) {
455 vlan->dev->features = dev->features & MACVLAN_FEATURES;
456 netdev_features_change(vlan->dev);
459 case NETDEV_UNREGISTER:
460 list_for_each_entry_safe(vlan, next, &port->vlans, list)
461 macvlan_dellink(vlan->dev);
467 static struct notifier_block macvlan_notifier_block __read_mostly = {
468 .notifier_call = macvlan_device_event,
471 static int __init macvlan_init_module(void)
475 register_netdevice_notifier(&macvlan_notifier_block);
476 macvlan_handle_frame_hook = macvlan_handle_frame;
478 err = rtnl_link_register(&macvlan_link_ops);
483 macvlan_handle_frame_hook = macvlan_handle_frame;
484 unregister_netdevice_notifier(&macvlan_notifier_block);
488 static void __exit macvlan_cleanup_module(void)
490 rtnl_link_unregister(&macvlan_link_ops);
491 macvlan_handle_frame_hook = NULL;
492 unregister_netdevice_notifier(&macvlan_notifier_block);
495 module_init(macvlan_init_module);
496 module_exit(macvlan_cleanup_module);
498 MODULE_LICENSE("GPL");
499 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
500 MODULE_DESCRIPTION("Driver for MAC address based VLANs");
501 MODULE_ALIAS_RTNL_LINK("macvlan");