]>
Commit | Line | Data |
---|---|---|
b863ceb7 PM |
1 | /* |
2 | * Copyright (c) 2007 Patrick McHardy <[email protected]> | |
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, [email protected] | |
13 | * Re-worked by Ben Greear <[email protected]> | |
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> | |
618e1b74 | 32 | #include <net/xfrm.h> |
b863ceb7 PM |
33 | |
34 | #define MACVLAN_HASH_SIZE (1 << BITS_PER_BYTE) | |
35 | ||
36 | struct macvlan_port { | |
37 | struct net_device *dev; | |
38 | struct hlist_head vlan_hash[MACVLAN_HASH_SIZE]; | |
39 | struct list_head vlans; | |
40 | }; | |
41 | ||
fccaf710 ED |
42 | /** |
43 | * struct macvlan_rx_stats - MACVLAN percpu rx stats | |
44 | * @rx_packets: number of received packets | |
45 | * @rx_bytes: number of received bytes | |
46 | * @multicast: number of received multicast packets | |
47 | * @rx_errors: number of errors | |
48 | */ | |
49 | struct macvlan_rx_stats { | |
50 | unsigned long rx_packets; | |
51 | unsigned long rx_bytes; | |
52 | unsigned long multicast; | |
53 | unsigned long rx_errors; | |
54 | }; | |
55 | ||
b863ceb7 PM |
56 | struct macvlan_dev { |
57 | struct net_device *dev; | |
58 | struct list_head list; | |
59 | struct hlist_node hlist; | |
60 | struct macvlan_port *port; | |
61 | struct net_device *lowerdev; | |
fccaf710 | 62 | struct macvlan_rx_stats *rx_stats; |
618e1b74 | 63 | enum macvlan_mode mode; |
b863ceb7 PM |
64 | }; |
65 | ||
66 | ||
67 | static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port, | |
68 | const unsigned char *addr) | |
69 | { | |
70 | struct macvlan_dev *vlan; | |
71 | struct hlist_node *n; | |
72 | ||
73 | hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[addr[5]], hlist) { | |
ac06713d | 74 | if (!compare_ether_addr_64bits(vlan->dev->dev_addr, addr)) |
b863ceb7 PM |
75 | return vlan; |
76 | } | |
77 | return NULL; | |
78 | } | |
79 | ||
f9ac30f0 EB |
80 | static void macvlan_hash_add(struct macvlan_dev *vlan) |
81 | { | |
82 | struct macvlan_port *port = vlan->port; | |
83 | const unsigned char *addr = vlan->dev->dev_addr; | |
84 | ||
85 | hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]); | |
86 | } | |
87 | ||
88 | static void macvlan_hash_del(struct macvlan_dev *vlan) | |
89 | { | |
90 | hlist_del_rcu(&vlan->hlist); | |
91 | synchronize_rcu(); | |
92 | } | |
93 | ||
94 | static void macvlan_hash_change_addr(struct macvlan_dev *vlan, | |
95 | const unsigned char *addr) | |
96 | { | |
97 | macvlan_hash_del(vlan); | |
98 | /* Now that we are unhashed it is safe to change the device | |
99 | * address without confusing packet delivery. | |
100 | */ | |
101 | memcpy(vlan->dev->dev_addr, addr, ETH_ALEN); | |
102 | macvlan_hash_add(vlan); | |
103 | } | |
104 | ||
105 | static int macvlan_addr_busy(const struct macvlan_port *port, | |
106 | const unsigned char *addr) | |
107 | { | |
108 | /* Test to see if the specified multicast address is | |
109 | * currently in use by the underlying device or | |
110 | * another macvlan. | |
111 | */ | |
ac06713d | 112 | if (!compare_ether_addr_64bits(port->dev->dev_addr, addr)) |
f9ac30f0 EB |
113 | return 1; |
114 | ||
115 | if (macvlan_hash_lookup(port, addr)) | |
116 | return 1; | |
117 | ||
118 | return 0; | |
119 | } | |
120 | ||
a1e514c5 AB |
121 | static inline void macvlan_count_rx(const struct macvlan_dev *vlan, |
122 | unsigned int len, bool success, | |
123 | bool multicast) | |
124 | { | |
125 | struct macvlan_rx_stats *rx_stats; | |
126 | ||
127 | rx_stats = per_cpu_ptr(vlan->rx_stats, smp_processor_id()); | |
128 | if (likely(success)) { | |
129 | rx_stats->rx_packets++;; | |
130 | rx_stats->rx_bytes += len; | |
131 | if (multicast) | |
132 | rx_stats->multicast++; | |
133 | } else { | |
134 | rx_stats->rx_errors++; | |
135 | } | |
136 | } | |
137 | ||
138 | static int macvlan_broadcast_one(struct sk_buff *skb, struct net_device *dev, | |
618e1b74 | 139 | const struct ethhdr *eth, bool local) |
a1e514c5 AB |
140 | { |
141 | if (!skb) | |
142 | return NET_RX_DROP; | |
143 | ||
618e1b74 AB |
144 | if (local) |
145 | return dev_forward_skb(dev, skb); | |
146 | ||
a1e514c5 AB |
147 | skb->dev = dev; |
148 | if (!compare_ether_addr_64bits(eth->h_dest, | |
149 | dev->broadcast)) | |
150 | skb->pkt_type = PACKET_BROADCAST; | |
151 | else | |
152 | skb->pkt_type = PACKET_MULTICAST; | |
153 | ||
154 | return netif_rx(skb); | |
155 | } | |
156 | ||
b863ceb7 | 157 | static void macvlan_broadcast(struct sk_buff *skb, |
618e1b74 AB |
158 | const struct macvlan_port *port, |
159 | struct net_device *src, | |
160 | enum macvlan_mode mode) | |
b863ceb7 PM |
161 | { |
162 | const struct ethhdr *eth = eth_hdr(skb); | |
163 | const struct macvlan_dev *vlan; | |
164 | struct hlist_node *n; | |
b863ceb7 PM |
165 | struct sk_buff *nskb; |
166 | unsigned int i; | |
a1e514c5 | 167 | int err; |
b863ceb7 | 168 | |
efbbced3 PM |
169 | if (skb->protocol == htons(ETH_P_PAUSE)) |
170 | return; | |
171 | ||
b863ceb7 PM |
172 | for (i = 0; i < MACVLAN_HASH_SIZE; i++) { |
173 | hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[i], hlist) { | |
618e1b74 AB |
174 | if (vlan->dev == src || !(vlan->mode & mode)) |
175 | continue; | |
176 | ||
b863ceb7 | 177 | nskb = skb_clone(skb, GFP_ATOMIC); |
618e1b74 AB |
178 | err = macvlan_broadcast_one(nskb, vlan->dev, eth, |
179 | mode == MACVLAN_MODE_BRIDGE); | |
a1e514c5 AB |
180 | macvlan_count_rx(vlan, skb->len + ETH_HLEN, |
181 | err == NET_RX_SUCCESS, 1); | |
b863ceb7 PM |
182 | } |
183 | } | |
184 | } | |
185 | ||
186 | /* called under rcu_read_lock() from netif_receive_skb */ | |
187 | static struct sk_buff *macvlan_handle_frame(struct sk_buff *skb) | |
188 | { | |
189 | const struct ethhdr *eth = eth_hdr(skb); | |
190 | const struct macvlan_port *port; | |
191 | const struct macvlan_dev *vlan; | |
618e1b74 | 192 | const struct macvlan_dev *src; |
b863ceb7 | 193 | struct net_device *dev; |
a1e514c5 | 194 | unsigned int len; |
b863ceb7 PM |
195 | |
196 | port = rcu_dereference(skb->dev->macvlan_port); | |
197 | if (port == NULL) | |
198 | return skb; | |
199 | ||
200 | if (is_multicast_ether_addr(eth->h_dest)) { | |
618e1b74 AB |
201 | src = macvlan_hash_lookup(port, eth->h_source); |
202 | if (!src) | |
203 | /* frame comes from an external address */ | |
204 | macvlan_broadcast(skb, port, NULL, | |
205 | MACVLAN_MODE_PRIVATE | | |
206 | MACVLAN_MODE_VEPA | | |
207 | MACVLAN_MODE_BRIDGE); | |
208 | else if (src->mode == MACVLAN_MODE_VEPA) | |
209 | /* flood to everyone except source */ | |
210 | macvlan_broadcast(skb, port, src->dev, | |
211 | MACVLAN_MODE_VEPA | | |
212 | MACVLAN_MODE_BRIDGE); | |
213 | else if (src->mode == MACVLAN_MODE_BRIDGE) | |
214 | /* | |
215 | * flood only to VEPA ports, bridge ports | |
216 | * already saw the frame on the way out. | |
217 | */ | |
218 | macvlan_broadcast(skb, port, src->dev, | |
219 | MACVLAN_MODE_VEPA); | |
b863ceb7 PM |
220 | return skb; |
221 | } | |
222 | ||
223 | vlan = macvlan_hash_lookup(port, eth->h_dest); | |
224 | if (vlan == NULL) | |
225 | return skb; | |
226 | ||
227 | dev = vlan->dev; | |
228 | if (unlikely(!(dev->flags & IFF_UP))) { | |
229 | kfree_skb(skb); | |
230 | return NULL; | |
231 | } | |
a1e514c5 | 232 | len = skb->len + ETH_HLEN; |
b863ceb7 | 233 | skb = skb_share_check(skb, GFP_ATOMIC); |
a1e514c5 AB |
234 | macvlan_count_rx(vlan, len, skb != NULL, 0); |
235 | if (!skb) | |
b863ceb7 | 236 | return NULL; |
b863ceb7 PM |
237 | |
238 | skb->dev = dev; | |
239 | skb->pkt_type = PACKET_HOST; | |
240 | ||
241 | netif_rx(skb); | |
242 | return NULL; | |
243 | } | |
244 | ||
618e1b74 AB |
245 | static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev) |
246 | { | |
247 | const struct macvlan_dev *vlan = netdev_priv(dev); | |
248 | const struct macvlan_port *port = vlan->port; | |
249 | const struct macvlan_dev *dest; | |
250 | ||
251 | if (vlan->mode == MACVLAN_MODE_BRIDGE) { | |
252 | const struct ethhdr *eth = (void *)skb->data; | |
253 | ||
254 | /* send to other bridge ports directly */ | |
255 | if (is_multicast_ether_addr(eth->h_dest)) { | |
256 | macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE); | |
257 | goto xmit_world; | |
258 | } | |
259 | ||
260 | dest = macvlan_hash_lookup(port, eth->h_dest); | |
261 | if (dest && dest->mode == MACVLAN_MODE_BRIDGE) { | |
262 | unsigned int length = skb->len + ETH_HLEN; | |
263 | int ret = dev_forward_skb(dest->dev, skb); | |
264 | macvlan_count_rx(dest, length, | |
265 | ret == NET_RX_SUCCESS, 0); | |
266 | ||
267 | return NET_XMIT_SUCCESS; | |
268 | } | |
269 | } | |
270 | ||
271 | xmit_world: | |
272 | skb->dev = vlan->lowerdev; | |
273 | return dev_queue_xmit(skb); | |
274 | } | |
275 | ||
424efe9c SH |
276 | static netdev_tx_t macvlan_start_xmit(struct sk_buff *skb, |
277 | struct net_device *dev) | |
b863ceb7 | 278 | { |
2c114553 ED |
279 | int i = skb_get_queue_mapping(skb); |
280 | struct netdev_queue *txq = netdev_get_tx_queue(dev, i); | |
b863ceb7 PM |
281 | unsigned int len = skb->len; |
282 | int ret; | |
283 | ||
618e1b74 | 284 | ret = macvlan_queue_xmit(skb, dev); |
b863ceb7 | 285 | if (likely(ret == NET_XMIT_SUCCESS)) { |
2c114553 ED |
286 | txq->tx_packets++; |
287 | txq->tx_bytes += len; | |
288 | } else | |
289 | txq->tx_dropped++; | |
290 | ||
cbbef5e1 | 291 | return ret; |
b863ceb7 PM |
292 | } |
293 | ||
294 | static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev, | |
3b04ddde SH |
295 | unsigned short type, const void *daddr, |
296 | const void *saddr, unsigned len) | |
b863ceb7 PM |
297 | { |
298 | const struct macvlan_dev *vlan = netdev_priv(dev); | |
299 | struct net_device *lowerdev = vlan->lowerdev; | |
300 | ||
0c4e8581 SH |
301 | return dev_hard_header(skb, lowerdev, type, daddr, |
302 | saddr ? : dev->dev_addr, len); | |
b863ceb7 PM |
303 | } |
304 | ||
3b04ddde SH |
305 | static const struct header_ops macvlan_hard_header_ops = { |
306 | .create = macvlan_hard_header, | |
307 | .rebuild = eth_rebuild_header, | |
308 | .parse = eth_header_parse, | |
3b04ddde SH |
309 | .cache = eth_header_cache, |
310 | .cache_update = eth_header_cache_update, | |
311 | }; | |
312 | ||
b863ceb7 PM |
313 | static int macvlan_open(struct net_device *dev) |
314 | { | |
315 | struct macvlan_dev *vlan = netdev_priv(dev); | |
b863ceb7 PM |
316 | struct net_device *lowerdev = vlan->lowerdev; |
317 | int err; | |
318 | ||
f9ac30f0 EB |
319 | err = -EBUSY; |
320 | if (macvlan_addr_busy(vlan->port, dev->dev_addr)) | |
321 | goto out; | |
322 | ||
ccffad25 | 323 | err = dev_unicast_add(lowerdev, dev->dev_addr); |
b863ceb7 | 324 | if (err < 0) |
b89fb7da WC |
325 | goto out; |
326 | if (dev->flags & IFF_ALLMULTI) { | |
327 | err = dev_set_allmulti(lowerdev, 1); | |
328 | if (err < 0) | |
329 | goto del_unicast; | |
330 | } | |
f9ac30f0 | 331 | macvlan_hash_add(vlan); |
b863ceb7 | 332 | return 0; |
b89fb7da WC |
333 | |
334 | del_unicast: | |
ccffad25 | 335 | dev_unicast_delete(lowerdev, dev->dev_addr); |
b89fb7da WC |
336 | out: |
337 | return err; | |
b863ceb7 PM |
338 | } |
339 | ||
340 | static int macvlan_stop(struct net_device *dev) | |
341 | { | |
342 | struct macvlan_dev *vlan = netdev_priv(dev); | |
343 | struct net_device *lowerdev = vlan->lowerdev; | |
344 | ||
345 | dev_mc_unsync(lowerdev, dev); | |
346 | if (dev->flags & IFF_ALLMULTI) | |
347 | dev_set_allmulti(lowerdev, -1); | |
348 | ||
ccffad25 | 349 | dev_unicast_delete(lowerdev, dev->dev_addr); |
b863ceb7 | 350 | |
f9ac30f0 | 351 | macvlan_hash_del(vlan); |
b863ceb7 PM |
352 | return 0; |
353 | } | |
354 | ||
ad5d20a6 PM |
355 | static int macvlan_set_mac_address(struct net_device *dev, void *p) |
356 | { | |
357 | struct macvlan_dev *vlan = netdev_priv(dev); | |
358 | struct net_device *lowerdev = vlan->lowerdev; | |
359 | struct sockaddr *addr = p; | |
360 | int err; | |
361 | ||
362 | if (!is_valid_ether_addr(addr->sa_data)) | |
363 | return -EADDRNOTAVAIL; | |
364 | ||
f9ac30f0 EB |
365 | if (!(dev->flags & IFF_UP)) { |
366 | /* Just copy in the new address */ | |
367 | memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN); | |
368 | } else { | |
369 | /* Rehash and update the device filters */ | |
370 | if (macvlan_addr_busy(vlan->port, addr->sa_data)) | |
371 | return -EBUSY; | |
ad5d20a6 | 372 | |
ccffad25 JP |
373 | err = dev_unicast_add(lowerdev, addr->sa_data); |
374 | if (err) | |
f9ac30f0 | 375 | return err; |
ad5d20a6 | 376 | |
ccffad25 | 377 | dev_unicast_delete(lowerdev, dev->dev_addr); |
f9ac30f0 EB |
378 | |
379 | macvlan_hash_change_addr(vlan, addr->sa_data); | |
380 | } | |
ad5d20a6 PM |
381 | return 0; |
382 | } | |
383 | ||
b863ceb7 PM |
384 | static void macvlan_change_rx_flags(struct net_device *dev, int change) |
385 | { | |
386 | struct macvlan_dev *vlan = netdev_priv(dev); | |
387 | struct net_device *lowerdev = vlan->lowerdev; | |
388 | ||
389 | if (change & IFF_ALLMULTI) | |
390 | dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1); | |
391 | } | |
392 | ||
393 | static void macvlan_set_multicast_list(struct net_device *dev) | |
394 | { | |
395 | struct macvlan_dev *vlan = netdev_priv(dev); | |
396 | ||
397 | dev_mc_sync(vlan->lowerdev, dev); | |
398 | } | |
399 | ||
400 | static int macvlan_change_mtu(struct net_device *dev, int new_mtu) | |
401 | { | |
402 | struct macvlan_dev *vlan = netdev_priv(dev); | |
403 | ||
404 | if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu) | |
405 | return -EINVAL; | |
406 | dev->mtu = new_mtu; | |
407 | return 0; | |
408 | } | |
409 | ||
410 | /* | |
411 | * macvlan network devices have devices nesting below it and are a special | |
412 | * "super class" of normal network devices; split their locks off into a | |
413 | * separate class since they always nest. | |
414 | */ | |
415 | static struct lock_class_key macvlan_netdev_xmit_lock_key; | |
cf508b12 | 416 | static struct lock_class_key macvlan_netdev_addr_lock_key; |
b863ceb7 PM |
417 | |
418 | #define MACVLAN_FEATURES \ | |
419 | (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \ | |
420 | NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \ | |
421 | NETIF_F_TSO_ECN | NETIF_F_TSO6) | |
422 | ||
423 | #define MACVLAN_STATE_MASK \ | |
424 | ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT)) | |
425 | ||
e8a0464c DM |
426 | static void macvlan_set_lockdep_class_one(struct net_device *dev, |
427 | struct netdev_queue *txq, | |
428 | void *_unused) | |
c773e847 DM |
429 | { |
430 | lockdep_set_class(&txq->_xmit_lock, | |
431 | &macvlan_netdev_xmit_lock_key); | |
432 | } | |
433 | ||
434 | static void macvlan_set_lockdep_class(struct net_device *dev) | |
435 | { | |
cf508b12 DM |
436 | lockdep_set_class(&dev->addr_list_lock, |
437 | &macvlan_netdev_addr_lock_key); | |
e8a0464c | 438 | netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL); |
c773e847 DM |
439 | } |
440 | ||
b863ceb7 PM |
441 | static int macvlan_init(struct net_device *dev) |
442 | { | |
443 | struct macvlan_dev *vlan = netdev_priv(dev); | |
444 | const struct net_device *lowerdev = vlan->lowerdev; | |
445 | ||
446 | dev->state = (dev->state & ~MACVLAN_STATE_MASK) | | |
447 | (lowerdev->state & MACVLAN_STATE_MASK); | |
448 | dev->features = lowerdev->features & MACVLAN_FEATURES; | |
8c2acc53 | 449 | dev->gso_max_size = lowerdev->gso_max_size; |
b863ceb7 | 450 | dev->iflink = lowerdev->ifindex; |
ef5c8996 | 451 | dev->hard_header_len = lowerdev->hard_header_len; |
b863ceb7 | 452 | |
c773e847 DM |
453 | macvlan_set_lockdep_class(dev); |
454 | ||
fccaf710 ED |
455 | vlan->rx_stats = alloc_percpu(struct macvlan_rx_stats); |
456 | if (!vlan->rx_stats) | |
457 | return -ENOMEM; | |
458 | ||
b863ceb7 PM |
459 | return 0; |
460 | } | |
461 | ||
fccaf710 ED |
462 | static void macvlan_uninit(struct net_device *dev) |
463 | { | |
464 | struct macvlan_dev *vlan = netdev_priv(dev); | |
465 | ||
466 | free_percpu(vlan->rx_stats); | |
467 | } | |
468 | ||
469 | static struct net_device_stats *macvlan_dev_get_stats(struct net_device *dev) | |
470 | { | |
471 | struct net_device_stats *stats = &dev->stats; | |
472 | struct macvlan_dev *vlan = netdev_priv(dev); | |
473 | ||
474 | dev_txq_stats_fold(dev, stats); | |
475 | ||
476 | if (vlan->rx_stats) { | |
477 | struct macvlan_rx_stats *p, rx = {0}; | |
478 | int i; | |
479 | ||
480 | for_each_possible_cpu(i) { | |
481 | p = per_cpu_ptr(vlan->rx_stats, i); | |
482 | rx.rx_packets += p->rx_packets; | |
483 | rx.rx_bytes += p->rx_bytes; | |
484 | rx.rx_errors += p->rx_errors; | |
485 | rx.multicast += p->multicast; | |
486 | } | |
487 | stats->rx_packets = rx.rx_packets; | |
488 | stats->rx_bytes = rx.rx_bytes; | |
489 | stats->rx_errors = rx.rx_errors; | |
490 | stats->rx_dropped = rx.rx_errors; | |
491 | stats->multicast = rx.multicast; | |
492 | } | |
493 | return stats; | |
494 | } | |
495 | ||
b863ceb7 PM |
496 | static void macvlan_ethtool_get_drvinfo(struct net_device *dev, |
497 | struct ethtool_drvinfo *drvinfo) | |
498 | { | |
499 | snprintf(drvinfo->driver, 32, "macvlan"); | |
500 | snprintf(drvinfo->version, 32, "0.1"); | |
501 | } | |
502 | ||
503 | static u32 macvlan_ethtool_get_rx_csum(struct net_device *dev) | |
504 | { | |
505 | const struct macvlan_dev *vlan = netdev_priv(dev); | |
b1b67dd4 | 506 | return dev_ethtool_get_rx_csum(vlan->lowerdev); |
b863ceb7 PM |
507 | } |
508 | ||
9edb8bb6 SH |
509 | static int macvlan_ethtool_get_settings(struct net_device *dev, |
510 | struct ethtool_cmd *cmd) | |
511 | { | |
512 | const struct macvlan_dev *vlan = netdev_priv(dev); | |
b1b67dd4 | 513 | return dev_ethtool_get_settings(vlan->lowerdev, cmd); |
9edb8bb6 SH |
514 | } |
515 | ||
516 | static u32 macvlan_ethtool_get_flags(struct net_device *dev) | |
517 | { | |
518 | const struct macvlan_dev *vlan = netdev_priv(dev); | |
b1b67dd4 | 519 | return dev_ethtool_get_flags(vlan->lowerdev); |
9edb8bb6 SH |
520 | } |
521 | ||
b863ceb7 PM |
522 | static const struct ethtool_ops macvlan_ethtool_ops = { |
523 | .get_link = ethtool_op_get_link, | |
9edb8bb6 | 524 | .get_settings = macvlan_ethtool_get_settings, |
b863ceb7 | 525 | .get_rx_csum = macvlan_ethtool_get_rx_csum, |
b863ceb7 | 526 | .get_drvinfo = macvlan_ethtool_get_drvinfo, |
9edb8bb6 | 527 | .get_flags = macvlan_ethtool_get_flags, |
b863ceb7 PM |
528 | }; |
529 | ||
54a30c97 SH |
530 | static const struct net_device_ops macvlan_netdev_ops = { |
531 | .ndo_init = macvlan_init, | |
fccaf710 | 532 | .ndo_uninit = macvlan_uninit, |
54a30c97 SH |
533 | .ndo_open = macvlan_open, |
534 | .ndo_stop = macvlan_stop, | |
00829823 | 535 | .ndo_start_xmit = macvlan_start_xmit, |
54a30c97 SH |
536 | .ndo_change_mtu = macvlan_change_mtu, |
537 | .ndo_change_rx_flags = macvlan_change_rx_flags, | |
538 | .ndo_set_mac_address = macvlan_set_mac_address, | |
539 | .ndo_set_multicast_list = macvlan_set_multicast_list, | |
fccaf710 | 540 | .ndo_get_stats = macvlan_dev_get_stats, |
54a30c97 SH |
541 | .ndo_validate_addr = eth_validate_addr, |
542 | }; | |
543 | ||
b863ceb7 PM |
544 | static void macvlan_setup(struct net_device *dev) |
545 | { | |
546 | ether_setup(dev); | |
547 | ||
93f154b5 | 548 | dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; |
54a30c97 | 549 | dev->netdev_ops = &macvlan_netdev_ops; |
b863ceb7 | 550 | dev->destructor = free_netdev; |
3b04ddde | 551 | dev->header_ops = &macvlan_hard_header_ops, |
b863ceb7 PM |
552 | dev->ethtool_ops = &macvlan_ethtool_ops; |
553 | dev->tx_queue_len = 0; | |
554 | } | |
555 | ||
556 | static int macvlan_port_create(struct net_device *dev) | |
557 | { | |
558 | struct macvlan_port *port; | |
559 | unsigned int i; | |
560 | ||
561 | if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK) | |
562 | return -EINVAL; | |
563 | ||
564 | port = kzalloc(sizeof(*port), GFP_KERNEL); | |
565 | if (port == NULL) | |
566 | return -ENOMEM; | |
567 | ||
568 | port->dev = dev; | |
569 | INIT_LIST_HEAD(&port->vlans); | |
570 | for (i = 0; i < MACVLAN_HASH_SIZE; i++) | |
571 | INIT_HLIST_HEAD(&port->vlan_hash[i]); | |
572 | rcu_assign_pointer(dev->macvlan_port, port); | |
573 | return 0; | |
574 | } | |
575 | ||
576 | static void macvlan_port_destroy(struct net_device *dev) | |
577 | { | |
578 | struct macvlan_port *port = dev->macvlan_port; | |
579 | ||
580 | rcu_assign_pointer(dev->macvlan_port, NULL); | |
581 | synchronize_rcu(); | |
582 | kfree(port); | |
583 | } | |
584 | ||
b863ceb7 PM |
585 | static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[]) |
586 | { | |
587 | if (tb[IFLA_ADDRESS]) { | |
588 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) | |
589 | return -EINVAL; | |
590 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) | |
591 | return -EADDRNOTAVAIL; | |
592 | } | |
27c0b1a8 AB |
593 | |
594 | if (data && data[IFLA_MACVLAN_MODE]) { | |
595 | switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) { | |
596 | case MACVLAN_MODE_PRIVATE: | |
597 | case MACVLAN_MODE_VEPA: | |
598 | case MACVLAN_MODE_BRIDGE: | |
599 | break; | |
600 | default: | |
601 | return -EINVAL; | |
602 | } | |
603 | } | |
b863ceb7 PM |
604 | return 0; |
605 | } | |
606 | ||
2c114553 ED |
607 | static int macvlan_get_tx_queues(struct net *net, |
608 | struct nlattr *tb[], | |
609 | unsigned int *num_tx_queues, | |
610 | unsigned int *real_num_tx_queues) | |
611 | { | |
612 | struct net_device *real_dev; | |
613 | ||
614 | if (!tb[IFLA_LINK]) | |
615 | return -EINVAL; | |
616 | ||
617 | real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK])); | |
618 | if (!real_dev) | |
619 | return -ENODEV; | |
620 | ||
621 | *num_tx_queues = real_dev->num_tx_queues; | |
622 | *real_num_tx_queues = real_dev->real_num_tx_queues; | |
623 | return 0; | |
624 | } | |
625 | ||
81adee47 | 626 | static int macvlan_newlink(struct net *src_net, struct net_device *dev, |
b863ceb7 PM |
627 | struct nlattr *tb[], struct nlattr *data[]) |
628 | { | |
629 | struct macvlan_dev *vlan = netdev_priv(dev); | |
630 | struct macvlan_port *port; | |
631 | struct net_device *lowerdev; | |
632 | int err; | |
633 | ||
634 | if (!tb[IFLA_LINK]) | |
635 | return -EINVAL; | |
636 | ||
81adee47 | 637 | lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK])); |
b863ceb7 PM |
638 | if (lowerdev == NULL) |
639 | return -ENODEV; | |
640 | ||
b0832a29 EB |
641 | /* When creating macvlans on top of other macvlans - use |
642 | * the real device as the lowerdev. | |
a6ca5f1d | 643 | */ |
b0832a29 EB |
644 | if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) { |
645 | struct macvlan_dev *lowervlan = netdev_priv(lowerdev); | |
646 | lowerdev = lowervlan->lowerdev; | |
647 | } | |
a6ca5f1d | 648 | |
b863ceb7 PM |
649 | if (!tb[IFLA_MTU]) |
650 | dev->mtu = lowerdev->mtu; | |
651 | else if (dev->mtu > lowerdev->mtu) | |
652 | return -EINVAL; | |
653 | ||
654 | if (!tb[IFLA_ADDRESS]) | |
655 | random_ether_addr(dev->dev_addr); | |
656 | ||
657 | if (lowerdev->macvlan_port == NULL) { | |
658 | err = macvlan_port_create(lowerdev); | |
659 | if (err < 0) | |
660 | return err; | |
661 | } | |
662 | port = lowerdev->macvlan_port; | |
663 | ||
664 | vlan->lowerdev = lowerdev; | |
665 | vlan->dev = dev; | |
666 | vlan->port = port; | |
667 | ||
27c0b1a8 AB |
668 | vlan->mode = MACVLAN_MODE_VEPA; |
669 | if (data && data[IFLA_MACVLAN_MODE]) | |
670 | vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]); | |
671 | ||
b863ceb7 PM |
672 | err = register_netdevice(dev); |
673 | if (err < 0) | |
674 | return err; | |
675 | ||
676 | list_add_tail(&vlan->list, &port->vlans); | |
fc4a7489 | 677 | netif_stacked_transfer_operstate(lowerdev, dev); |
b863ceb7 PM |
678 | return 0; |
679 | } | |
680 | ||
23289a37 | 681 | static void macvlan_dellink(struct net_device *dev, struct list_head *head) |
b863ceb7 PM |
682 | { |
683 | struct macvlan_dev *vlan = netdev_priv(dev); | |
684 | struct macvlan_port *port = vlan->port; | |
685 | ||
686 | list_del(&vlan->list); | |
23289a37 | 687 | unregister_netdevice_queue(dev, head); |
b863ceb7 PM |
688 | |
689 | if (list_empty(&port->vlans)) | |
73120964 | 690 | macvlan_port_destroy(port->dev); |
b863ceb7 PM |
691 | } |
692 | ||
27c0b1a8 AB |
693 | static int macvlan_changelink(struct net_device *dev, |
694 | struct nlattr *tb[], struct nlattr *data[]) | |
695 | { | |
696 | struct macvlan_dev *vlan = netdev_priv(dev); | |
697 | if (data && data[IFLA_MACVLAN_MODE]) | |
698 | vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]); | |
699 | return 0; | |
700 | } | |
701 | ||
702 | static size_t macvlan_get_size(const struct net_device *dev) | |
703 | { | |
704 | return nla_total_size(4); | |
705 | } | |
706 | ||
707 | static int macvlan_fill_info(struct sk_buff *skb, | |
708 | const struct net_device *dev) | |
709 | { | |
710 | struct macvlan_dev *vlan = netdev_priv(dev); | |
711 | ||
712 | NLA_PUT_U32(skb, IFLA_MACVLAN_MODE, vlan->mode); | |
713 | return 0; | |
714 | ||
715 | nla_put_failure: | |
716 | return -EMSGSIZE; | |
717 | } | |
718 | ||
719 | static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = { | |
720 | [IFLA_MACVLAN_MODE] = { .type = NLA_U32 }, | |
721 | }; | |
722 | ||
b863ceb7 PM |
723 | static struct rtnl_link_ops macvlan_link_ops __read_mostly = { |
724 | .kind = "macvlan", | |
725 | .priv_size = sizeof(struct macvlan_dev), | |
2c114553 | 726 | .get_tx_queues = macvlan_get_tx_queues, |
b863ceb7 PM |
727 | .setup = macvlan_setup, |
728 | .validate = macvlan_validate, | |
729 | .newlink = macvlan_newlink, | |
730 | .dellink = macvlan_dellink, | |
27c0b1a8 AB |
731 | .maxtype = IFLA_MACVLAN_MAX, |
732 | .policy = macvlan_policy, | |
733 | .changelink = macvlan_changelink, | |
734 | .get_size = macvlan_get_size, | |
735 | .fill_info = macvlan_fill_info, | |
b863ceb7 PM |
736 | }; |
737 | ||
738 | static int macvlan_device_event(struct notifier_block *unused, | |
739 | unsigned long event, void *ptr) | |
740 | { | |
741 | struct net_device *dev = ptr; | |
742 | struct macvlan_dev *vlan, *next; | |
743 | struct macvlan_port *port; | |
744 | ||
745 | port = dev->macvlan_port; | |
746 | if (port == NULL) | |
747 | return NOTIFY_DONE; | |
748 | ||
749 | switch (event) { | |
750 | case NETDEV_CHANGE: | |
751 | list_for_each_entry(vlan, &port->vlans, list) | |
fc4a7489 PM |
752 | netif_stacked_transfer_operstate(vlan->lowerdev, |
753 | vlan->dev); | |
b863ceb7 PM |
754 | break; |
755 | case NETDEV_FEAT_CHANGE: | |
756 | list_for_each_entry(vlan, &port->vlans, list) { | |
757 | vlan->dev->features = dev->features & MACVLAN_FEATURES; | |
8c2acc53 | 758 | vlan->dev->gso_max_size = dev->gso_max_size; |
b863ceb7 PM |
759 | netdev_features_change(vlan->dev); |
760 | } | |
761 | break; | |
762 | case NETDEV_UNREGISTER: | |
763 | list_for_each_entry_safe(vlan, next, &port->vlans, list) | |
23289a37 | 764 | macvlan_dellink(vlan->dev, NULL); |
b863ceb7 PM |
765 | break; |
766 | } | |
767 | return NOTIFY_DONE; | |
768 | } | |
769 | ||
770 | static struct notifier_block macvlan_notifier_block __read_mostly = { | |
771 | .notifier_call = macvlan_device_event, | |
772 | }; | |
773 | ||
774 | static int __init macvlan_init_module(void) | |
775 | { | |
776 | int err; | |
777 | ||
778 | register_netdevice_notifier(&macvlan_notifier_block); | |
779 | macvlan_handle_frame_hook = macvlan_handle_frame; | |
780 | ||
781 | err = rtnl_link_register(&macvlan_link_ops); | |
782 | if (err < 0) | |
783 | goto err1; | |
784 | return 0; | |
785 | err1: | |
52913246 | 786 | macvlan_handle_frame_hook = NULL; |
b863ceb7 PM |
787 | unregister_netdevice_notifier(&macvlan_notifier_block); |
788 | return err; | |
789 | } | |
790 | ||
791 | static void __exit macvlan_cleanup_module(void) | |
792 | { | |
793 | rtnl_link_unregister(&macvlan_link_ops); | |
794 | macvlan_handle_frame_hook = NULL; | |
795 | unregister_netdevice_notifier(&macvlan_notifier_block); | |
796 | } | |
797 | ||
798 | module_init(macvlan_init_module); | |
799 | module_exit(macvlan_cleanup_module); | |
800 | ||
801 | MODULE_LICENSE("GPL"); | |
802 | MODULE_AUTHOR("Patrick McHardy <[email protected]>"); | |
803 | MODULE_DESCRIPTION("Driver for MAC address based VLANs"); | |
804 | MODULE_ALIAS_RTNL_LINK("macvlan"); |