Commit | Line | Data |
---|---|---|
b863ceb7 PM |
1 | /* |
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> | |
82524746 | 23 | #include <linux/rculist.h> |
b863ceb7 PM |
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 | ||
35 | struct macvlan_port { | |
36 | struct net_device *dev; | |
37 | struct hlist_head vlan_hash[MACVLAN_HASH_SIZE]; | |
38 | struct list_head vlans; | |
39 | }; | |
40 | ||
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; | |
47 | }; | |
48 | ||
49 | ||
50 | static 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 | ||
63 | static 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; | |
b863ceb7 PM |
76 | |
77 | nskb = skb_clone(skb, GFP_ATOMIC); | |
78 | if (nskb == NULL) { | |
79 | dev->stats.rx_errors++; | |
80 | dev->stats.rx_dropped++; | |
81 | continue; | |
82 | } | |
83 | ||
84 | dev->stats.rx_bytes += skb->len + ETH_HLEN; | |
85 | dev->stats.rx_packets++; | |
86 | dev->stats.multicast++; | |
87 | dev->last_rx = jiffies; | |
88 | ||
89 | nskb->dev = dev; | |
90 | if (!compare_ether_addr(eth->h_dest, dev->broadcast)) | |
91 | nskb->pkt_type = PACKET_BROADCAST; | |
92 | else | |
93 | nskb->pkt_type = PACKET_MULTICAST; | |
94 | ||
95 | netif_rx(nskb); | |
96 | } | |
97 | } | |
98 | } | |
99 | ||
100 | /* called under rcu_read_lock() from netif_receive_skb */ | |
101 | static struct sk_buff *macvlan_handle_frame(struct sk_buff *skb) | |
102 | { | |
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; | |
107 | ||
108 | port = rcu_dereference(skb->dev->macvlan_port); | |
109 | if (port == NULL) | |
110 | return skb; | |
111 | ||
112 | if (is_multicast_ether_addr(eth->h_dest)) { | |
113 | macvlan_broadcast(skb, port); | |
114 | return skb; | |
115 | } | |
116 | ||
117 | vlan = macvlan_hash_lookup(port, eth->h_dest); | |
118 | if (vlan == NULL) | |
119 | return skb; | |
120 | ||
121 | dev = vlan->dev; | |
122 | if (unlikely(!(dev->flags & IFF_UP))) { | |
123 | kfree_skb(skb); | |
124 | return NULL; | |
125 | } | |
126 | ||
127 | skb = skb_share_check(skb, GFP_ATOMIC); | |
128 | if (skb == NULL) { | |
129 | dev->stats.rx_errors++; | |
130 | dev->stats.rx_dropped++; | |
131 | return NULL; | |
132 | } | |
133 | ||
134 | dev->stats.rx_bytes += skb->len + ETH_HLEN; | |
135 | dev->stats.rx_packets++; | |
136 | dev->last_rx = jiffies; | |
137 | ||
138 | skb->dev = dev; | |
139 | skb->pkt_type = PACKET_HOST; | |
140 | ||
141 | netif_rx(skb); | |
142 | return NULL; | |
143 | } | |
144 | ||
145 | static int macvlan_hard_start_xmit(struct sk_buff *skb, struct net_device *dev) | |
146 | { | |
147 | const struct macvlan_dev *vlan = netdev_priv(dev); | |
148 | unsigned int len = skb->len; | |
149 | int ret; | |
150 | ||
151 | skb->dev = vlan->lowerdev; | |
152 | ret = dev_queue_xmit(skb); | |
153 | ||
154 | if (likely(ret == NET_XMIT_SUCCESS)) { | |
155 | dev->stats.tx_packets++; | |
156 | dev->stats.tx_bytes += len; | |
157 | } else { | |
158 | dev->stats.tx_errors++; | |
159 | dev->stats.tx_aborted_errors++; | |
160 | } | |
161 | return NETDEV_TX_OK; | |
162 | } | |
163 | ||
164 | static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev, | |
3b04ddde SH |
165 | unsigned short type, const void *daddr, |
166 | const void *saddr, unsigned len) | |
b863ceb7 PM |
167 | { |
168 | const struct macvlan_dev *vlan = netdev_priv(dev); | |
169 | struct net_device *lowerdev = vlan->lowerdev; | |
170 | ||
0c4e8581 SH |
171 | return dev_hard_header(skb, lowerdev, type, daddr, |
172 | saddr ? : dev->dev_addr, len); | |
b863ceb7 PM |
173 | } |
174 | ||
3b04ddde SH |
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, | |
3b04ddde SH |
179 | .cache = eth_header_cache, |
180 | .cache_update = eth_header_cache_update, | |
181 | }; | |
182 | ||
b863ceb7 PM |
183 | static int macvlan_open(struct net_device *dev) |
184 | { | |
185 | struct macvlan_dev *vlan = netdev_priv(dev); | |
186 | struct macvlan_port *port = vlan->port; | |
187 | struct net_device *lowerdev = vlan->lowerdev; | |
188 | int err; | |
189 | ||
190 | err = dev_unicast_add(lowerdev, dev->dev_addr, ETH_ALEN); | |
191 | if (err < 0) | |
b89fb7da WC |
192 | goto out; |
193 | if (dev->flags & IFF_ALLMULTI) { | |
194 | err = dev_set_allmulti(lowerdev, 1); | |
195 | if (err < 0) | |
196 | goto del_unicast; | |
197 | } | |
b863ceb7 PM |
198 | |
199 | hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[dev->dev_addr[5]]); | |
200 | return 0; | |
b89fb7da WC |
201 | |
202 | del_unicast: | |
203 | dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN); | |
204 | out: | |
205 | return err; | |
b863ceb7 PM |
206 | } |
207 | ||
208 | static int macvlan_stop(struct net_device *dev) | |
209 | { | |
210 | struct macvlan_dev *vlan = netdev_priv(dev); | |
211 | struct net_device *lowerdev = vlan->lowerdev; | |
212 | ||
213 | dev_mc_unsync(lowerdev, dev); | |
214 | if (dev->flags & IFF_ALLMULTI) | |
215 | dev_set_allmulti(lowerdev, -1); | |
216 | ||
217 | dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN); | |
218 | ||
219 | hlist_del_rcu(&vlan->hlist); | |
220 | synchronize_rcu(); | |
221 | return 0; | |
222 | } | |
223 | ||
ad5d20a6 PM |
224 | static int macvlan_set_mac_address(struct net_device *dev, void *p) |
225 | { | |
226 | struct macvlan_dev *vlan = netdev_priv(dev); | |
227 | struct net_device *lowerdev = vlan->lowerdev; | |
228 | struct sockaddr *addr = p; | |
229 | int err; | |
230 | ||
231 | if (!is_valid_ether_addr(addr->sa_data)) | |
232 | return -EADDRNOTAVAIL; | |
233 | ||
234 | if (!(dev->flags & IFF_UP)) | |
235 | goto out; | |
236 | ||
237 | err = dev_unicast_add(lowerdev, addr->sa_data, ETH_ALEN); | |
238 | if (err < 0) | |
239 | return err; | |
240 | dev_unicast_delete(lowerdev, dev->dev_addr, ETH_ALEN); | |
241 | ||
242 | out: | |
243 | memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN); | |
244 | return 0; | |
245 | } | |
246 | ||
b863ceb7 PM |
247 | static void macvlan_change_rx_flags(struct net_device *dev, int change) |
248 | { | |
249 | struct macvlan_dev *vlan = netdev_priv(dev); | |
250 | struct net_device *lowerdev = vlan->lowerdev; | |
251 | ||
252 | if (change & IFF_ALLMULTI) | |
253 | dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1); | |
254 | } | |
255 | ||
256 | static void macvlan_set_multicast_list(struct net_device *dev) | |
257 | { | |
258 | struct macvlan_dev *vlan = netdev_priv(dev); | |
259 | ||
260 | dev_mc_sync(vlan->lowerdev, dev); | |
261 | } | |
262 | ||
263 | static int macvlan_change_mtu(struct net_device *dev, int new_mtu) | |
264 | { | |
265 | struct macvlan_dev *vlan = netdev_priv(dev); | |
266 | ||
267 | if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu) | |
268 | return -EINVAL; | |
269 | dev->mtu = new_mtu; | |
270 | return 0; | |
271 | } | |
272 | ||
273 | /* | |
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. | |
277 | */ | |
278 | static struct lock_class_key macvlan_netdev_xmit_lock_key; | |
279 | ||
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) | |
284 | ||
285 | #define MACVLAN_STATE_MASK \ | |
286 | ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT)) | |
287 | ||
e8a0464c DM |
288 | static void macvlan_set_lockdep_class_one(struct net_device *dev, |
289 | struct netdev_queue *txq, | |
290 | void *_unused) | |
c773e847 DM |
291 | { |
292 | lockdep_set_class(&txq->_xmit_lock, | |
293 | &macvlan_netdev_xmit_lock_key); | |
294 | } | |
295 | ||
296 | static void macvlan_set_lockdep_class(struct net_device *dev) | |
297 | { | |
e8a0464c | 298 | netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL); |
c773e847 DM |
299 | } |
300 | ||
b863ceb7 PM |
301 | static int macvlan_init(struct net_device *dev) |
302 | { | |
303 | struct macvlan_dev *vlan = netdev_priv(dev); | |
304 | const struct net_device *lowerdev = vlan->lowerdev; | |
305 | ||
306 | dev->state = (dev->state & ~MACVLAN_STATE_MASK) | | |
307 | (lowerdev->state & MACVLAN_STATE_MASK); | |
308 | dev->features = lowerdev->features & MACVLAN_FEATURES; | |
309 | dev->iflink = lowerdev->ifindex; | |
310 | ||
c773e847 DM |
311 | macvlan_set_lockdep_class(dev); |
312 | ||
b863ceb7 PM |
313 | return 0; |
314 | } | |
315 | ||
316 | static void macvlan_ethtool_get_drvinfo(struct net_device *dev, | |
317 | struct ethtool_drvinfo *drvinfo) | |
318 | { | |
319 | snprintf(drvinfo->driver, 32, "macvlan"); | |
320 | snprintf(drvinfo->version, 32, "0.1"); | |
321 | } | |
322 | ||
323 | static u32 macvlan_ethtool_get_rx_csum(struct net_device *dev) | |
324 | { | |
325 | const struct macvlan_dev *vlan = netdev_priv(dev); | |
326 | struct net_device *lowerdev = vlan->lowerdev; | |
327 | ||
328 | if (lowerdev->ethtool_ops->get_rx_csum == NULL) | |
329 | return 0; | |
330 | return lowerdev->ethtool_ops->get_rx_csum(lowerdev); | |
331 | } | |
332 | ||
333 | static const struct ethtool_ops macvlan_ethtool_ops = { | |
334 | .get_link = ethtool_op_get_link, | |
335 | .get_rx_csum = macvlan_ethtool_get_rx_csum, | |
b863ceb7 PM |
336 | .get_drvinfo = macvlan_ethtool_get_drvinfo, |
337 | }; | |
338 | ||
339 | static void macvlan_setup(struct net_device *dev) | |
340 | { | |
341 | ether_setup(dev); | |
342 | ||
343 | dev->init = macvlan_init; | |
344 | dev->open = macvlan_open; | |
345 | dev->stop = macvlan_stop; | |
346 | dev->change_mtu = macvlan_change_mtu; | |
347 | dev->change_rx_flags = macvlan_change_rx_flags; | |
ad5d20a6 | 348 | dev->set_mac_address = macvlan_set_mac_address; |
b863ceb7 | 349 | dev->set_multicast_list = macvlan_set_multicast_list; |
b863ceb7 PM |
350 | dev->hard_start_xmit = macvlan_hard_start_xmit; |
351 | dev->destructor = free_netdev; | |
3b04ddde | 352 | dev->header_ops = &macvlan_hard_header_ops, |
b863ceb7 PM |
353 | dev->ethtool_ops = &macvlan_ethtool_ops; |
354 | dev->tx_queue_len = 0; | |
355 | } | |
356 | ||
357 | static int macvlan_port_create(struct net_device *dev) | |
358 | { | |
359 | struct macvlan_port *port; | |
360 | unsigned int i; | |
361 | ||
362 | if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK) | |
363 | return -EINVAL; | |
364 | ||
365 | port = kzalloc(sizeof(*port), GFP_KERNEL); | |
366 | if (port == NULL) | |
367 | return -ENOMEM; | |
368 | ||
369 | port->dev = dev; | |
370 | INIT_LIST_HEAD(&port->vlans); | |
371 | for (i = 0; i < MACVLAN_HASH_SIZE; i++) | |
372 | INIT_HLIST_HEAD(&port->vlan_hash[i]); | |
373 | rcu_assign_pointer(dev->macvlan_port, port); | |
374 | return 0; | |
375 | } | |
376 | ||
377 | static void macvlan_port_destroy(struct net_device *dev) | |
378 | { | |
379 | struct macvlan_port *port = dev->macvlan_port; | |
380 | ||
381 | rcu_assign_pointer(dev->macvlan_port, NULL); | |
382 | synchronize_rcu(); | |
383 | kfree(port); | |
384 | } | |
385 | ||
386 | static void macvlan_transfer_operstate(struct net_device *dev) | |
387 | { | |
388 | struct macvlan_dev *vlan = netdev_priv(dev); | |
389 | const struct net_device *lowerdev = vlan->lowerdev; | |
390 | ||
391 | if (lowerdev->operstate == IF_OPER_DORMANT) | |
392 | netif_dormant_on(dev); | |
393 | else | |
394 | netif_dormant_off(dev); | |
395 | ||
396 | if (netif_carrier_ok(lowerdev)) { | |
397 | if (!netif_carrier_ok(dev)) | |
398 | netif_carrier_on(dev); | |
399 | } else { | |
f12ca5f9 | 400 | if (netif_carrier_ok(dev)) |
b863ceb7 PM |
401 | netif_carrier_off(dev); |
402 | } | |
403 | } | |
404 | ||
405 | static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[]) | |
406 | { | |
407 | if (tb[IFLA_ADDRESS]) { | |
408 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) | |
409 | return -EINVAL; | |
410 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) | |
411 | return -EADDRNOTAVAIL; | |
412 | } | |
413 | return 0; | |
414 | } | |
415 | ||
416 | static int macvlan_newlink(struct net_device *dev, | |
417 | struct nlattr *tb[], struct nlattr *data[]) | |
418 | { | |
419 | struct macvlan_dev *vlan = netdev_priv(dev); | |
420 | struct macvlan_port *port; | |
421 | struct net_device *lowerdev; | |
422 | int err; | |
423 | ||
424 | if (!tb[IFLA_LINK]) | |
425 | return -EINVAL; | |
426 | ||
c346dca1 | 427 | lowerdev = __dev_get_by_index(dev_net(dev), nla_get_u32(tb[IFLA_LINK])); |
b863ceb7 PM |
428 | if (lowerdev == NULL) |
429 | return -ENODEV; | |
430 | ||
a6ca5f1d PM |
431 | /* Don't allow macvlans on top of other macvlans - its not really |
432 | * wrong, but lockdep can't handle it and its not useful for anything | |
433 | * you couldn't do directly on top of the real device. | |
434 | */ | |
435 | if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) | |
436 | return -ENODEV; | |
437 | ||
b863ceb7 PM |
438 | if (!tb[IFLA_MTU]) |
439 | dev->mtu = lowerdev->mtu; | |
440 | else if (dev->mtu > lowerdev->mtu) | |
441 | return -EINVAL; | |
442 | ||
443 | if (!tb[IFLA_ADDRESS]) | |
444 | random_ether_addr(dev->dev_addr); | |
445 | ||
446 | if (lowerdev->macvlan_port == NULL) { | |
447 | err = macvlan_port_create(lowerdev); | |
448 | if (err < 0) | |
449 | return err; | |
450 | } | |
451 | port = lowerdev->macvlan_port; | |
452 | ||
453 | vlan->lowerdev = lowerdev; | |
454 | vlan->dev = dev; | |
455 | vlan->port = port; | |
456 | ||
457 | err = register_netdevice(dev); | |
458 | if (err < 0) | |
459 | return err; | |
460 | ||
461 | list_add_tail(&vlan->list, &port->vlans); | |
462 | macvlan_transfer_operstate(dev); | |
463 | return 0; | |
464 | } | |
465 | ||
466 | static void macvlan_dellink(struct net_device *dev) | |
467 | { | |
468 | struct macvlan_dev *vlan = netdev_priv(dev); | |
469 | struct macvlan_port *port = vlan->port; | |
470 | ||
471 | list_del(&vlan->list); | |
472 | unregister_netdevice(dev); | |
473 | ||
474 | if (list_empty(&port->vlans)) | |
73120964 | 475 | macvlan_port_destroy(port->dev); |
b863ceb7 PM |
476 | } |
477 | ||
478 | static struct rtnl_link_ops macvlan_link_ops __read_mostly = { | |
479 | .kind = "macvlan", | |
480 | .priv_size = sizeof(struct macvlan_dev), | |
481 | .setup = macvlan_setup, | |
482 | .validate = macvlan_validate, | |
483 | .newlink = macvlan_newlink, | |
484 | .dellink = macvlan_dellink, | |
485 | }; | |
486 | ||
487 | static int macvlan_device_event(struct notifier_block *unused, | |
488 | unsigned long event, void *ptr) | |
489 | { | |
490 | struct net_device *dev = ptr; | |
491 | struct macvlan_dev *vlan, *next; | |
492 | struct macvlan_port *port; | |
493 | ||
494 | port = dev->macvlan_port; | |
495 | if (port == NULL) | |
496 | return NOTIFY_DONE; | |
497 | ||
498 | switch (event) { | |
499 | case NETDEV_CHANGE: | |
500 | list_for_each_entry(vlan, &port->vlans, list) | |
501 | macvlan_transfer_operstate(vlan->dev); | |
502 | break; | |
503 | case NETDEV_FEAT_CHANGE: | |
504 | list_for_each_entry(vlan, &port->vlans, list) { | |
505 | vlan->dev->features = dev->features & MACVLAN_FEATURES; | |
506 | netdev_features_change(vlan->dev); | |
507 | } | |
508 | break; | |
509 | case NETDEV_UNREGISTER: | |
510 | list_for_each_entry_safe(vlan, next, &port->vlans, list) | |
511 | macvlan_dellink(vlan->dev); | |
512 | break; | |
513 | } | |
514 | return NOTIFY_DONE; | |
515 | } | |
516 | ||
517 | static struct notifier_block macvlan_notifier_block __read_mostly = { | |
518 | .notifier_call = macvlan_device_event, | |
519 | }; | |
520 | ||
521 | static int __init macvlan_init_module(void) | |
522 | { | |
523 | int err; | |
524 | ||
525 | register_netdevice_notifier(&macvlan_notifier_block); | |
526 | macvlan_handle_frame_hook = macvlan_handle_frame; | |
527 | ||
528 | err = rtnl_link_register(&macvlan_link_ops); | |
529 | if (err < 0) | |
530 | goto err1; | |
531 | return 0; | |
532 | err1: | |
52913246 | 533 | macvlan_handle_frame_hook = NULL; |
b863ceb7 PM |
534 | unregister_netdevice_notifier(&macvlan_notifier_block); |
535 | return err; | |
536 | } | |
537 | ||
538 | static void __exit macvlan_cleanup_module(void) | |
539 | { | |
540 | rtnl_link_unregister(&macvlan_link_ops); | |
541 | macvlan_handle_frame_hook = NULL; | |
542 | unregister_netdevice_notifier(&macvlan_notifier_block); | |
543 | } | |
544 | ||
545 | module_init(macvlan_init_module); | |
546 | module_exit(macvlan_cleanup_module); | |
547 | ||
548 | MODULE_LICENSE("GPL"); | |
549 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
550 | MODULE_DESCRIPTION("Driver for MAC address based VLANs"); | |
551 | MODULE_ALIAS_RTNL_LINK("macvlan"); |