blob: 2e4bcd5654c4c0b62db0e795f87c96d73c176207 [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;
76 if (unlikely(!(dev->flags & IFF_UP)))
77 continue;
78
79 nskb = skb_clone(skb, GFP_ATOMIC);
80 if (nskb == NULL) {
81 dev->stats.rx_errors++;
82 dev->stats.rx_dropped++;
83 continue;
84 }
85
86 dev->stats.rx_bytes += skb->len + ETH_HLEN;
87 dev->stats.rx_packets++;
88 dev->stats.multicast++;
89 dev->last_rx = jiffies;
90
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++;
138 dev->last_rx = jiffies;
139
140 skb->dev = dev;
141 skb->pkt_type = PACKET_HOST;
142
143 netif_rx(skb);
144 return NULL;
145}
146
147static int macvlan_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
148{
149 const struct macvlan_dev *vlan = netdev_priv(dev);
150 unsigned int len = skb->len;
151 int ret;
152
153 skb->dev = vlan->lowerdev;
154 ret = dev_queue_xmit(skb);
155
156 if (likely(ret == NET_XMIT_SUCCESS)) {
157 dev->stats.tx_packets++;
158 dev->stats.tx_bytes += len;
159 } else {
160 dev->stats.tx_errors++;
161 dev->stats.tx_aborted_errors++;
162 }
163 return NETDEV_TX_OK;
164}
165
166static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700167 unsigned short type, const void *daddr,
168 const void *saddr, unsigned len)
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700169{
170 const struct macvlan_dev *vlan = netdev_priv(dev);
171 struct net_device *lowerdev = vlan->lowerdev;
172
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700173 return dev_hard_header(skb, lowerdev, type, daddr,
174 saddr ? : dev->dev_addr, len);
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700175}
176
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700177static const struct header_ops macvlan_hard_header_ops = {
178 .create = macvlan_hard_header,
179 .rebuild = eth_rebuild_header,
180 .parse = eth_header_parse,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700181 .cache = eth_header_cache,
182 .cache_update = eth_header_cache_update,
183};
184
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700185static int macvlan_open(struct net_device *dev)
186{
187 struct macvlan_dev *vlan = netdev_priv(dev);
188 struct macvlan_port *port = vlan->port;
189 struct net_device *lowerdev = vlan->lowerdev;
190 int err;
191
192 err = dev_unicast_add(lowerdev, dev->dev_addr, ETH_ALEN);
193 if (err < 0)
194 return err;
195 if (dev->flags & IFF_ALLMULTI)
196 dev_set_allmulti(lowerdev, 1);
197
198 hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[dev->dev_addr[5]]);
199 return 0;
200}
201
202static int macvlan_stop(struct net_device *dev)
203{
204 struct macvlan_dev *vlan = netdev_priv(dev);
205 struct net_device *lowerdev = vlan->lowerdev;
206
207 dev_mc_unsync(lowerdev, dev);
208 if (dev->flags & IFF_ALLMULTI)
209 dev_set_allmulti(lowerdev, -1);
210
211 dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN);
212
213 hlist_del_rcu(&vlan->hlist);
214 synchronize_rcu();
215 return 0;
216}
217
218static void macvlan_change_rx_flags(struct net_device *dev, int change)
219{
220 struct macvlan_dev *vlan = netdev_priv(dev);
221 struct net_device *lowerdev = vlan->lowerdev;
222
223 if (change & IFF_ALLMULTI)
224 dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
225}
226
227static void macvlan_set_multicast_list(struct net_device *dev)
228{
229 struct macvlan_dev *vlan = netdev_priv(dev);
230
231 dev_mc_sync(vlan->lowerdev, dev);
232}
233
234static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
235{
236 struct macvlan_dev *vlan = netdev_priv(dev);
237
238 if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
239 return -EINVAL;
240 dev->mtu = new_mtu;
241 return 0;
242}
243
244/*
245 * macvlan network devices have devices nesting below it and are a special
246 * "super class" of normal network devices; split their locks off into a
247 * separate class since they always nest.
248 */
249static struct lock_class_key macvlan_netdev_xmit_lock_key;
250
251#define MACVLAN_FEATURES \
252 (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
253 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
254 NETIF_F_TSO_ECN | NETIF_F_TSO6)
255
256#define MACVLAN_STATE_MASK \
257 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
258
259static int macvlan_init(struct net_device *dev)
260{
261 struct macvlan_dev *vlan = netdev_priv(dev);
262 const struct net_device *lowerdev = vlan->lowerdev;
263
264 dev->state = (dev->state & ~MACVLAN_STATE_MASK) |
265 (lowerdev->state & MACVLAN_STATE_MASK);
266 dev->features = lowerdev->features & MACVLAN_FEATURES;
267 dev->iflink = lowerdev->ifindex;
268
269 lockdep_set_class(&dev->_xmit_lock, &macvlan_netdev_xmit_lock_key);
270 return 0;
271}
272
273static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
274 struct ethtool_drvinfo *drvinfo)
275{
276 snprintf(drvinfo->driver, 32, "macvlan");
277 snprintf(drvinfo->version, 32, "0.1");
278}
279
280static u32 macvlan_ethtool_get_rx_csum(struct net_device *dev)
281{
282 const struct macvlan_dev *vlan = netdev_priv(dev);
283 struct net_device *lowerdev = vlan->lowerdev;
284
285 if (lowerdev->ethtool_ops->get_rx_csum == NULL)
286 return 0;
287 return lowerdev->ethtool_ops->get_rx_csum(lowerdev);
288}
289
290static const struct ethtool_ops macvlan_ethtool_ops = {
291 .get_link = ethtool_op_get_link,
292 .get_rx_csum = macvlan_ethtool_get_rx_csum,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700293 .get_drvinfo = macvlan_ethtool_get_drvinfo,
294};
295
296static void macvlan_setup(struct net_device *dev)
297{
298 ether_setup(dev);
299
300 dev->init = macvlan_init;
301 dev->open = macvlan_open;
302 dev->stop = macvlan_stop;
303 dev->change_mtu = macvlan_change_mtu;
304 dev->change_rx_flags = macvlan_change_rx_flags;
305 dev->set_multicast_list = macvlan_set_multicast_list;
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700306 dev->hard_start_xmit = macvlan_hard_start_xmit;
307 dev->destructor = free_netdev;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700308 dev->header_ops = &macvlan_hard_header_ops,
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700309 dev->ethtool_ops = &macvlan_ethtool_ops;
310 dev->tx_queue_len = 0;
311}
312
313static int macvlan_port_create(struct net_device *dev)
314{
315 struct macvlan_port *port;
316 unsigned int i;
317
318 if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
319 return -EINVAL;
320
321 port = kzalloc(sizeof(*port), GFP_KERNEL);
322 if (port == NULL)
323 return -ENOMEM;
324
325 port->dev = dev;
326 INIT_LIST_HEAD(&port->vlans);
327 for (i = 0; i < MACVLAN_HASH_SIZE; i++)
328 INIT_HLIST_HEAD(&port->vlan_hash[i]);
329 rcu_assign_pointer(dev->macvlan_port, port);
330 return 0;
331}
332
333static void macvlan_port_destroy(struct net_device *dev)
334{
335 struct macvlan_port *port = dev->macvlan_port;
336
337 rcu_assign_pointer(dev->macvlan_port, NULL);
338 synchronize_rcu();
339 kfree(port);
340}
341
342static void macvlan_transfer_operstate(struct net_device *dev)
343{
344 struct macvlan_dev *vlan = netdev_priv(dev);
345 const struct net_device *lowerdev = vlan->lowerdev;
346
347 if (lowerdev->operstate == IF_OPER_DORMANT)
348 netif_dormant_on(dev);
349 else
350 netif_dormant_off(dev);
351
352 if (netif_carrier_ok(lowerdev)) {
353 if (!netif_carrier_ok(dev))
354 netif_carrier_on(dev);
355 } else {
356 if (netif_carrier_ok(lowerdev))
357 netif_carrier_off(dev);
358 }
359}
360
361static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
362{
363 if (tb[IFLA_ADDRESS]) {
364 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
365 return -EINVAL;
366 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
367 return -EADDRNOTAVAIL;
368 }
369 return 0;
370}
371
372static int macvlan_newlink(struct net_device *dev,
373 struct nlattr *tb[], struct nlattr *data[])
374{
375 struct macvlan_dev *vlan = netdev_priv(dev);
376 struct macvlan_port *port;
377 struct net_device *lowerdev;
378 int err;
379
380 if (!tb[IFLA_LINK])
381 return -EINVAL;
382
Eric W. Biederman881d9662007-09-17 11:56:21 -0700383 lowerdev = __dev_get_by_index(dev->nd_net, nla_get_u32(tb[IFLA_LINK]));
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700384 if (lowerdev == NULL)
385 return -ENODEV;
386
387 if (!tb[IFLA_MTU])
388 dev->mtu = lowerdev->mtu;
389 else if (dev->mtu > lowerdev->mtu)
390 return -EINVAL;
391
392 if (!tb[IFLA_ADDRESS])
393 random_ether_addr(dev->dev_addr);
394
395 if (lowerdev->macvlan_port == NULL) {
396 err = macvlan_port_create(lowerdev);
397 if (err < 0)
398 return err;
399 }
400 port = lowerdev->macvlan_port;
401
402 vlan->lowerdev = lowerdev;
403 vlan->dev = dev;
404 vlan->port = port;
405
406 err = register_netdevice(dev);
407 if (err < 0)
408 return err;
409
410 list_add_tail(&vlan->list, &port->vlans);
411 macvlan_transfer_operstate(dev);
412 return 0;
413}
414
415static void macvlan_dellink(struct net_device *dev)
416{
417 struct macvlan_dev *vlan = netdev_priv(dev);
418 struct macvlan_port *port = vlan->port;
419
420 list_del(&vlan->list);
421 unregister_netdevice(dev);
422
423 if (list_empty(&port->vlans))
424 macvlan_port_destroy(dev);
425}
426
427static struct rtnl_link_ops macvlan_link_ops __read_mostly = {
428 .kind = "macvlan",
429 .priv_size = sizeof(struct macvlan_dev),
430 .setup = macvlan_setup,
431 .validate = macvlan_validate,
432 .newlink = macvlan_newlink,
433 .dellink = macvlan_dellink,
434};
435
436static int macvlan_device_event(struct notifier_block *unused,
437 unsigned long event, void *ptr)
438{
439 struct net_device *dev = ptr;
440 struct macvlan_dev *vlan, *next;
441 struct macvlan_port *port;
442
443 port = dev->macvlan_port;
444 if (port == NULL)
445 return NOTIFY_DONE;
446
447 switch (event) {
448 case NETDEV_CHANGE:
449 list_for_each_entry(vlan, &port->vlans, list)
450 macvlan_transfer_operstate(vlan->dev);
451 break;
452 case NETDEV_FEAT_CHANGE:
453 list_for_each_entry(vlan, &port->vlans, list) {
454 vlan->dev->features = dev->features & MACVLAN_FEATURES;
455 netdev_features_change(vlan->dev);
456 }
457 break;
458 case NETDEV_UNREGISTER:
459 list_for_each_entry_safe(vlan, next, &port->vlans, list)
460 macvlan_dellink(vlan->dev);
461 break;
462 }
463 return NOTIFY_DONE;
464}
465
466static struct notifier_block macvlan_notifier_block __read_mostly = {
467 .notifier_call = macvlan_device_event,
468};
469
470static int __init macvlan_init_module(void)
471{
472 int err;
473
474 register_netdevice_notifier(&macvlan_notifier_block);
475 macvlan_handle_frame_hook = macvlan_handle_frame;
476
477 err = rtnl_link_register(&macvlan_link_ops);
478 if (err < 0)
479 goto err1;
480 return 0;
481err1:
482 macvlan_handle_frame_hook = macvlan_handle_frame;
483 unregister_netdevice_notifier(&macvlan_notifier_block);
484 return err;
485}
486
487static void __exit macvlan_cleanup_module(void)
488{
489 rtnl_link_unregister(&macvlan_link_ops);
490 macvlan_handle_frame_hook = NULL;
491 unregister_netdevice_notifier(&macvlan_notifier_block);
492}
493
494module_init(macvlan_init_module);
495module_exit(macvlan_cleanup_module);
496
497MODULE_LICENSE("GPL");
498MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
499MODULE_DESCRIPTION("Driver for MAC address based VLANs");
500MODULE_ALIAS_RTNL_LINK("macvlan");