macvlan: don't broadcast PAUSE frames to macvlan devices
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / macvlan.c
blob590039cbb146f5a42b888055cc836ea576f23726
1 /*
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:
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 * ---
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/rculist.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)
35 struct macvlan_port {
36 struct net_device *dev;
37 struct hlist_head vlan_hash[MACVLAN_HASH_SIZE];
38 struct list_head vlans;
41 struct 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;
50 static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
51 const unsigned char *addr)
53 struct macvlan_dev *vlan;
54 struct hlist_node *n;
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;
60 return NULL;
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;
68 struct hlist_node *n;
69 struct net_device *dev;
70 struct sk_buff *nskb;
71 unsigned int i;
73 if (skb->protocol == htons(ETH_P_PAUSE))
74 return;
76 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;
80 nskb = skb_clone(skb, GFP_ATOMIC);
81 if (nskb == NULL) {
82 dev->stats.rx_errors++;
83 dev->stats.rx_dropped++;
84 continue;
87 dev->stats.rx_bytes += skb->len + ETH_HLEN;
88 dev->stats.rx_packets++;
89 dev->stats.multicast++;
90 dev->last_rx = jiffies;
92 nskb->dev = dev;
93 if (!compare_ether_addr(eth->h_dest, dev->broadcast))
94 nskb->pkt_type = PACKET_BROADCAST;
95 else
96 nskb->pkt_type = PACKET_MULTICAST;
98 netif_rx(nskb);
103 /* called under rcu_read_lock() from netif_receive_skb */
104 static struct sk_buff *macvlan_handle_frame(struct sk_buff *skb)
106 const struct ethhdr *eth = eth_hdr(skb);
107 const struct macvlan_port *port;
108 const struct macvlan_dev *vlan;
109 struct net_device *dev;
111 port = rcu_dereference(skb->dev->macvlan_port);
112 if (port == NULL)
113 return skb;
115 if (is_multicast_ether_addr(eth->h_dest)) {
116 macvlan_broadcast(skb, port);
117 return skb;
120 vlan = macvlan_hash_lookup(port, eth->h_dest);
121 if (vlan == NULL)
122 return skb;
124 dev = vlan->dev;
125 if (unlikely(!(dev->flags & IFF_UP))) {
126 kfree_skb(skb);
127 return NULL;
130 skb = skb_share_check(skb, GFP_ATOMIC);
131 if (skb == NULL) {
132 dev->stats.rx_errors++;
133 dev->stats.rx_dropped++;
134 return NULL;
137 dev->stats.rx_bytes += skb->len + ETH_HLEN;
138 dev->stats.rx_packets++;
139 dev->last_rx = jiffies;
141 skb->dev = dev;
142 skb->pkt_type = PACKET_HOST;
144 netif_rx(skb);
145 return NULL;
148 static int macvlan_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
150 const struct macvlan_dev *vlan = netdev_priv(dev);
151 unsigned int len = skb->len;
152 int ret;
154 skb->dev = vlan->lowerdev;
155 ret = dev_queue_xmit(skb);
157 if (likely(ret == NET_XMIT_SUCCESS)) {
158 dev->stats.tx_packets++;
159 dev->stats.tx_bytes += len;
160 } else {
161 dev->stats.tx_errors++;
162 dev->stats.tx_aborted_errors++;
164 return NETDEV_TX_OK;
167 static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
168 unsigned short type, const void *daddr,
169 const void *saddr, unsigned len)
171 const struct macvlan_dev *vlan = netdev_priv(dev);
172 struct net_device *lowerdev = vlan->lowerdev;
174 return dev_hard_header(skb, lowerdev, type, daddr,
175 saddr ? : dev->dev_addr, len);
178 static const struct header_ops macvlan_hard_header_ops = {
179 .create = macvlan_hard_header,
180 .rebuild = eth_rebuild_header,
181 .parse = eth_header_parse,
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;
191 int err;
193 err = dev_unicast_add(lowerdev, dev->dev_addr, ETH_ALEN);
194 if (err < 0)
195 goto out;
196 if (dev->flags & IFF_ALLMULTI) {
197 err = dev_set_allmulti(lowerdev, 1);
198 if (err < 0)
199 goto del_unicast;
202 hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[dev->dev_addr[5]]);
203 return 0;
205 del_unicast:
206 dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN);
207 out:
208 return err;
211 static int macvlan_stop(struct net_device *dev)
213 struct macvlan_dev *vlan = netdev_priv(dev);
214 struct net_device *lowerdev = vlan->lowerdev;
216 dev_mc_unsync(lowerdev, dev);
217 if (dev->flags & IFF_ALLMULTI)
218 dev_set_allmulti(lowerdev, -1);
220 dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN);
222 hlist_del_rcu(&vlan->hlist);
223 synchronize_rcu();
224 return 0;
227 static int macvlan_set_mac_address(struct net_device *dev, void *p)
229 struct macvlan_dev *vlan = netdev_priv(dev);
230 struct net_device *lowerdev = vlan->lowerdev;
231 struct sockaddr *addr = p;
232 int err;
234 if (!is_valid_ether_addr(addr->sa_data))
235 return -EADDRNOTAVAIL;
237 if (!(dev->flags & IFF_UP))
238 goto out;
240 err = dev_unicast_add(lowerdev, addr->sa_data, ETH_ALEN);
241 if (err < 0)
242 return err;
243 dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN);
245 out:
246 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
247 return 0;
250 static void macvlan_change_rx_flags(struct net_device *dev, int change)
252 struct macvlan_dev *vlan = netdev_priv(dev);
253 struct net_device *lowerdev = vlan->lowerdev;
255 if (change & IFF_ALLMULTI)
256 dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
259 static void macvlan_set_multicast_list(struct net_device *dev)
261 struct macvlan_dev *vlan = netdev_priv(dev);
263 dev_mc_sync(vlan->lowerdev, dev);
266 static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
268 struct macvlan_dev *vlan = netdev_priv(dev);
270 if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
271 return -EINVAL;
272 dev->mtu = new_mtu;
273 return 0;
277 * macvlan network devices have devices nesting below it and are a special
278 * "super class" of normal network devices; split their locks off into a
279 * separate class since they always nest.
281 static struct lock_class_key macvlan_netdev_xmit_lock_key;
282 static struct lock_class_key macvlan_netdev_addr_lock_key;
284 #define MACVLAN_FEATURES \
285 (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
286 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
287 NETIF_F_TSO_ECN | NETIF_F_TSO6)
289 #define MACVLAN_STATE_MASK \
290 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
292 static void macvlan_set_lockdep_class_one(struct net_device *dev,
293 struct netdev_queue *txq,
294 void *_unused)
296 lockdep_set_class(&txq->_xmit_lock,
297 &macvlan_netdev_xmit_lock_key);
300 static void macvlan_set_lockdep_class(struct net_device *dev)
302 lockdep_set_class(&dev->addr_list_lock,
303 &macvlan_netdev_addr_lock_key);
304 netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL);
307 static int macvlan_init(struct net_device *dev)
309 struct macvlan_dev *vlan = netdev_priv(dev);
310 const struct net_device *lowerdev = vlan->lowerdev;
312 dev->state = (dev->state & ~MACVLAN_STATE_MASK) |
313 (lowerdev->state & MACVLAN_STATE_MASK);
314 dev->features = lowerdev->features & MACVLAN_FEATURES;
315 dev->iflink = lowerdev->ifindex;
317 macvlan_set_lockdep_class(dev);
319 return 0;
322 static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
323 struct ethtool_drvinfo *drvinfo)
325 snprintf(drvinfo->driver, 32, "macvlan");
326 snprintf(drvinfo->version, 32, "0.1");
329 static u32 macvlan_ethtool_get_rx_csum(struct net_device *dev)
331 const struct macvlan_dev *vlan = netdev_priv(dev);
332 struct net_device *lowerdev = vlan->lowerdev;
334 if (lowerdev->ethtool_ops->get_rx_csum == NULL)
335 return 0;
336 return lowerdev->ethtool_ops->get_rx_csum(lowerdev);
339 static const struct ethtool_ops macvlan_ethtool_ops = {
340 .get_link = ethtool_op_get_link,
341 .get_rx_csum = macvlan_ethtool_get_rx_csum,
342 .get_drvinfo = macvlan_ethtool_get_drvinfo,
345 static void macvlan_setup(struct net_device *dev)
347 ether_setup(dev);
349 dev->init = macvlan_init;
350 dev->open = macvlan_open;
351 dev->stop = macvlan_stop;
352 dev->change_mtu = macvlan_change_mtu;
353 dev->change_rx_flags = macvlan_change_rx_flags;
354 dev->set_mac_address = macvlan_set_mac_address;
355 dev->set_multicast_list = macvlan_set_multicast_list;
356 dev->hard_start_xmit = macvlan_hard_start_xmit;
357 dev->destructor = free_netdev;
358 dev->header_ops = &macvlan_hard_header_ops,
359 dev->ethtool_ops = &macvlan_ethtool_ops;
360 dev->tx_queue_len = 0;
363 static int macvlan_port_create(struct net_device *dev)
365 struct macvlan_port *port;
366 unsigned int i;
368 if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
369 return -EINVAL;
371 port = kzalloc(sizeof(*port), GFP_KERNEL);
372 if (port == NULL)
373 return -ENOMEM;
375 port->dev = dev;
376 INIT_LIST_HEAD(&port->vlans);
377 for (i = 0; i < MACVLAN_HASH_SIZE; i++)
378 INIT_HLIST_HEAD(&port->vlan_hash[i]);
379 rcu_assign_pointer(dev->macvlan_port, port);
380 return 0;
383 static void macvlan_port_destroy(struct net_device *dev)
385 struct macvlan_port *port = dev->macvlan_port;
387 rcu_assign_pointer(dev->macvlan_port, NULL);
388 synchronize_rcu();
389 kfree(port);
392 static void macvlan_transfer_operstate(struct net_device *dev)
394 struct macvlan_dev *vlan = netdev_priv(dev);
395 const struct net_device *lowerdev = vlan->lowerdev;
397 if (lowerdev->operstate == IF_OPER_DORMANT)
398 netif_dormant_on(dev);
399 else
400 netif_dormant_off(dev);
402 if (netif_carrier_ok(lowerdev)) {
403 if (!netif_carrier_ok(dev))
404 netif_carrier_on(dev);
405 } else {
406 if (netif_carrier_ok(dev))
407 netif_carrier_off(dev);
411 static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
413 if (tb[IFLA_ADDRESS]) {
414 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
415 return -EINVAL;
416 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
417 return -EADDRNOTAVAIL;
419 return 0;
422 static int macvlan_newlink(struct net_device *dev,
423 struct nlattr *tb[], struct nlattr *data[])
425 struct macvlan_dev *vlan = netdev_priv(dev);
426 struct macvlan_port *port;
427 struct net_device *lowerdev;
428 int err;
430 if (!tb[IFLA_LINK])
431 return -EINVAL;
433 lowerdev = __dev_get_by_index(dev_net(dev), nla_get_u32(tb[IFLA_LINK]));
434 if (lowerdev == NULL)
435 return -ENODEV;
437 /* Don't allow macvlans on top of other macvlans - its not really
438 * wrong, but lockdep can't handle it and its not useful for anything
439 * you couldn't do directly on top of the real device.
441 if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops)
442 return -ENODEV;
444 if (!tb[IFLA_MTU])
445 dev->mtu = lowerdev->mtu;
446 else if (dev->mtu > lowerdev->mtu)
447 return -EINVAL;
449 if (!tb[IFLA_ADDRESS])
450 random_ether_addr(dev->dev_addr);
452 if (lowerdev->macvlan_port == NULL) {
453 err = macvlan_port_create(lowerdev);
454 if (err < 0)
455 return err;
457 port = lowerdev->macvlan_port;
459 vlan->lowerdev = lowerdev;
460 vlan->dev = dev;
461 vlan->port = port;
463 err = register_netdevice(dev);
464 if (err < 0)
465 return err;
467 list_add_tail(&vlan->list, &port->vlans);
468 macvlan_transfer_operstate(dev);
469 return 0;
472 static void macvlan_dellink(struct net_device *dev)
474 struct macvlan_dev *vlan = netdev_priv(dev);
475 struct macvlan_port *port = vlan->port;
477 list_del(&vlan->list);
478 unregister_netdevice(dev);
480 if (list_empty(&port->vlans))
481 macvlan_port_destroy(port->dev);
484 static struct rtnl_link_ops macvlan_link_ops __read_mostly = {
485 .kind = "macvlan",
486 .priv_size = sizeof(struct macvlan_dev),
487 .setup = macvlan_setup,
488 .validate = macvlan_validate,
489 .newlink = macvlan_newlink,
490 .dellink = macvlan_dellink,
493 static int macvlan_device_event(struct notifier_block *unused,
494 unsigned long event, void *ptr)
496 struct net_device *dev = ptr;
497 struct macvlan_dev *vlan, *next;
498 struct macvlan_port *port;
500 port = dev->macvlan_port;
501 if (port == NULL)
502 return NOTIFY_DONE;
504 switch (event) {
505 case NETDEV_CHANGE:
506 list_for_each_entry(vlan, &port->vlans, list)
507 macvlan_transfer_operstate(vlan->dev);
508 break;
509 case NETDEV_FEAT_CHANGE:
510 list_for_each_entry(vlan, &port->vlans, list) {
511 vlan->dev->features = dev->features & MACVLAN_FEATURES;
512 netdev_features_change(vlan->dev);
514 break;
515 case NETDEV_UNREGISTER:
516 list_for_each_entry_safe(vlan, next, &port->vlans, list)
517 macvlan_dellink(vlan->dev);
518 break;
520 return NOTIFY_DONE;
523 static struct notifier_block macvlan_notifier_block __read_mostly = {
524 .notifier_call = macvlan_device_event,
527 static int __init macvlan_init_module(void)
529 int err;
531 register_netdevice_notifier(&macvlan_notifier_block);
532 macvlan_handle_frame_hook = macvlan_handle_frame;
534 err = rtnl_link_register(&macvlan_link_ops);
535 if (err < 0)
536 goto err1;
537 return 0;
538 err1:
539 macvlan_handle_frame_hook = NULL;
540 unregister_netdevice_notifier(&macvlan_notifier_block);
541 return err;
544 static void __exit macvlan_cleanup_module(void)
546 rtnl_link_unregister(&macvlan_link_ops);
547 macvlan_handle_frame_hook = NULL;
548 unregister_netdevice_notifier(&macvlan_notifier_block);
551 module_init(macvlan_init_module);
552 module_exit(macvlan_cleanup_module);
554 MODULE_LICENSE("GPL");
555 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
556 MODULE_DESCRIPTION("Driver for MAC address based VLANs");
557 MODULE_ALIAS_RTNL_LINK("macvlan");