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