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