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