Root/
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> |
23 | #include <linux/rculist.h> |
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_vlan.h> |
30 | #include <linux/if_link.h> |
31 | #include <linux/if_macvlan.h> |
32 | #include <linux/hash.h> |
33 | #include <net/rtnetlink.h> |
34 | #include <net/xfrm.h> |
35 | |
36 | #define MACVLAN_HASH_SIZE (1 << BITS_PER_BYTE) |
37 | |
38 | struct macvlan_port { |
39 | struct net_device *dev; |
40 | struct hlist_head vlan_hash[MACVLAN_HASH_SIZE]; |
41 | struct list_head vlans; |
42 | struct rcu_head rcu; |
43 | bool passthru; |
44 | int count; |
45 | }; |
46 | |
47 | static void macvlan_port_destroy(struct net_device *dev); |
48 | |
49 | #define macvlan_port_get_rcu(dev) \ |
50 | ((struct macvlan_port *) rcu_dereference(dev->rx_handler_data)) |
51 | #define macvlan_port_get(dev) ((struct macvlan_port *) dev->rx_handler_data) |
52 | #define macvlan_port_exists(dev) (dev->priv_flags & IFF_MACVLAN_PORT) |
53 | |
54 | static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port, |
55 | const unsigned char *addr) |
56 | { |
57 | struct macvlan_dev *vlan; |
58 | |
59 | hlist_for_each_entry_rcu(vlan, &port->vlan_hash[addr[5]], hlist) { |
60 | if (ether_addr_equal_64bits(vlan->dev->dev_addr, addr)) |
61 | return vlan; |
62 | } |
63 | return NULL; |
64 | } |
65 | |
66 | static void macvlan_hash_add(struct macvlan_dev *vlan) |
67 | { |
68 | struct macvlan_port *port = vlan->port; |
69 | const unsigned char *addr = vlan->dev->dev_addr; |
70 | |
71 | hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]); |
72 | } |
73 | |
74 | static void macvlan_hash_del(struct macvlan_dev *vlan, bool sync) |
75 | { |
76 | hlist_del_rcu(&vlan->hlist); |
77 | if (sync) |
78 | synchronize_rcu(); |
79 | } |
80 | |
81 | static void macvlan_hash_change_addr(struct macvlan_dev *vlan, |
82 | const unsigned char *addr) |
83 | { |
84 | macvlan_hash_del(vlan, true); |
85 | /* Now that we are unhashed it is safe to change the device |
86 | * address without confusing packet delivery. |
87 | */ |
88 | memcpy(vlan->dev->dev_addr, addr, ETH_ALEN); |
89 | macvlan_hash_add(vlan); |
90 | } |
91 | |
92 | static int macvlan_addr_busy(const struct macvlan_port *port, |
93 | const unsigned char *addr) |
94 | { |
95 | /* Test to see if the specified multicast address is |
96 | * currently in use by the underlying device or |
97 | * another macvlan. |
98 | */ |
99 | if (ether_addr_equal_64bits(port->dev->dev_addr, addr)) |
100 | return 1; |
101 | |
102 | if (macvlan_hash_lookup(port, addr)) |
103 | return 1; |
104 | |
105 | return 0; |
106 | } |
107 | |
108 | |
109 | static int macvlan_broadcast_one(struct sk_buff *skb, |
110 | const struct macvlan_dev *vlan, |
111 | const struct ethhdr *eth, bool local) |
112 | { |
113 | struct net_device *dev = vlan->dev; |
114 | if (!skb) |
115 | return NET_RX_DROP; |
116 | |
117 | if (local) |
118 | return vlan->forward(dev, skb); |
119 | |
120 | skb->dev = dev; |
121 | if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast)) |
122 | skb->pkt_type = PACKET_BROADCAST; |
123 | else |
124 | skb->pkt_type = PACKET_MULTICAST; |
125 | |
126 | return vlan->receive(skb); |
127 | } |
128 | |
129 | static u32 macvlan_hash_mix(const struct macvlan_dev *vlan) |
130 | { |
131 | return (u32)(((unsigned long)vlan) >> L1_CACHE_SHIFT); |
132 | } |
133 | |
134 | |
135 | static unsigned int mc_hash(const struct macvlan_dev *vlan, |
136 | const unsigned char *addr) |
137 | { |
138 | u32 val = __get_unaligned_cpu32(addr + 2); |
139 | |
140 | val ^= macvlan_hash_mix(vlan); |
141 | return hash_32(val, MACVLAN_MC_FILTER_BITS); |
142 | } |
143 | |
144 | static void macvlan_broadcast(struct sk_buff *skb, |
145 | const struct macvlan_port *port, |
146 | struct net_device *src, |
147 | enum macvlan_mode mode) |
148 | { |
149 | const struct ethhdr *eth = eth_hdr(skb); |
150 | const struct macvlan_dev *vlan; |
151 | struct sk_buff *nskb; |
152 | unsigned int i; |
153 | int err; |
154 | unsigned int hash; |
155 | |
156 | if (skb->protocol == htons(ETH_P_PAUSE)) |
157 | return; |
158 | |
159 | for (i = 0; i < MACVLAN_HASH_SIZE; i++) { |
160 | hlist_for_each_entry_rcu(vlan, &port->vlan_hash[i], hlist) { |
161 | if (vlan->dev == src || !(vlan->mode & mode)) |
162 | continue; |
163 | |
164 | hash = mc_hash(vlan, eth->h_dest); |
165 | if (!test_bit(hash, vlan->mc_filter)) |
166 | continue; |
167 | nskb = skb_clone(skb, GFP_ATOMIC); |
168 | err = macvlan_broadcast_one(nskb, vlan, eth, |
169 | mode == MACVLAN_MODE_BRIDGE); |
170 | macvlan_count_rx(vlan, skb->len + ETH_HLEN, |
171 | err == NET_RX_SUCCESS, 1); |
172 | } |
173 | } |
174 | } |
175 | |
176 | /* called under rcu_read_lock() from netif_receive_skb */ |
177 | static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb) |
178 | { |
179 | struct macvlan_port *port; |
180 | struct sk_buff *skb = *pskb; |
181 | const struct ethhdr *eth = eth_hdr(skb); |
182 | const struct macvlan_dev *vlan; |
183 | const struct macvlan_dev *src; |
184 | struct net_device *dev; |
185 | unsigned int len = 0; |
186 | int ret = NET_RX_DROP; |
187 | |
188 | port = macvlan_port_get_rcu(skb->dev); |
189 | if (is_multicast_ether_addr(eth->h_dest)) { |
190 | skb = ip_check_defrag(skb, IP_DEFRAG_MACVLAN); |
191 | if (!skb) |
192 | return RX_HANDLER_CONSUMED; |
193 | eth = eth_hdr(skb); |
194 | src = macvlan_hash_lookup(port, eth->h_source); |
195 | if (!src) |
196 | /* frame comes from an external address */ |
197 | macvlan_broadcast(skb, port, NULL, |
198 | MACVLAN_MODE_PRIVATE | |
199 | MACVLAN_MODE_VEPA | |
200 | MACVLAN_MODE_PASSTHRU| |
201 | MACVLAN_MODE_BRIDGE); |
202 | else if (src->mode == MACVLAN_MODE_VEPA) |
203 | /* flood to everyone except source */ |
204 | macvlan_broadcast(skb, port, src->dev, |
205 | MACVLAN_MODE_VEPA | |
206 | MACVLAN_MODE_BRIDGE); |
207 | else if (src->mode == MACVLAN_MODE_BRIDGE) |
208 | /* |
209 | * flood only to VEPA ports, bridge ports |
210 | * already saw the frame on the way out. |
211 | */ |
212 | macvlan_broadcast(skb, port, src->dev, |
213 | MACVLAN_MODE_VEPA); |
214 | else { |
215 | /* forward to original port. */ |
216 | vlan = src; |
217 | ret = macvlan_broadcast_one(skb, vlan, eth, 0); |
218 | goto out; |
219 | } |
220 | |
221 | return RX_HANDLER_PASS; |
222 | } |
223 | |
224 | if (port->passthru) |
225 | vlan = list_first_entry(&port->vlans, struct macvlan_dev, list); |
226 | else |
227 | vlan = macvlan_hash_lookup(port, eth->h_dest); |
228 | if (vlan == NULL) |
229 | return RX_HANDLER_PASS; |
230 | |
231 | dev = vlan->dev; |
232 | if (unlikely(!(dev->flags & IFF_UP))) { |
233 | kfree_skb(skb); |
234 | return RX_HANDLER_CONSUMED; |
235 | } |
236 | len = skb->len + ETH_HLEN; |
237 | skb = skb_share_check(skb, GFP_ATOMIC); |
238 | if (!skb) |
239 | goto out; |
240 | |
241 | skb->dev = dev; |
242 | skb->pkt_type = PACKET_HOST; |
243 | |
244 | ret = vlan->receive(skb); |
245 | |
246 | out: |
247 | macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, 0); |
248 | return RX_HANDLER_CONSUMED; |
249 | } |
250 | |
251 | static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev) |
252 | { |
253 | const struct macvlan_dev *vlan = netdev_priv(dev); |
254 | const struct macvlan_port *port = vlan->port; |
255 | const struct macvlan_dev *dest; |
256 | __u8 ip_summed = skb->ip_summed; |
257 | |
258 | if (vlan->mode == MACVLAN_MODE_BRIDGE) { |
259 | const struct ethhdr *eth = (void *)skb->data; |
260 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
261 | |
262 | /* send to other bridge ports directly */ |
263 | if (is_multicast_ether_addr(eth->h_dest)) { |
264 | macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE); |
265 | goto xmit_world; |
266 | } |
267 | |
268 | dest = macvlan_hash_lookup(port, eth->h_dest); |
269 | if (dest && dest->mode == MACVLAN_MODE_BRIDGE) { |
270 | /* send to lowerdev first for its network taps */ |
271 | dev_forward_skb(vlan->lowerdev, skb); |
272 | |
273 | return NET_XMIT_SUCCESS; |
274 | } |
275 | } |
276 | |
277 | xmit_world: |
278 | skb->ip_summed = ip_summed; |
279 | skb->dev = vlan->lowerdev; |
280 | return dev_queue_xmit(skb); |
281 | } |
282 | |
283 | netdev_tx_t macvlan_start_xmit(struct sk_buff *skb, |
284 | struct net_device *dev) |
285 | { |
286 | unsigned int len = skb->len; |
287 | int ret; |
288 | const struct macvlan_dev *vlan = netdev_priv(dev); |
289 | |
290 | ret = macvlan_queue_xmit(skb, dev); |
291 | if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) { |
292 | struct macvlan_pcpu_stats *pcpu_stats; |
293 | |
294 | pcpu_stats = this_cpu_ptr(vlan->pcpu_stats); |
295 | u64_stats_update_begin(&pcpu_stats->syncp); |
296 | pcpu_stats->tx_packets++; |
297 | pcpu_stats->tx_bytes += len; |
298 | u64_stats_update_end(&pcpu_stats->syncp); |
299 | } else { |
300 | this_cpu_inc(vlan->pcpu_stats->tx_dropped); |
301 | } |
302 | return ret; |
303 | } |
304 | EXPORT_SYMBOL_GPL(macvlan_start_xmit); |
305 | |
306 | static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev, |
307 | unsigned short type, const void *daddr, |
308 | const void *saddr, unsigned len) |
309 | { |
310 | const struct macvlan_dev *vlan = netdev_priv(dev); |
311 | struct net_device *lowerdev = vlan->lowerdev; |
312 | |
313 | return dev_hard_header(skb, lowerdev, type, daddr, |
314 | saddr ? : dev->dev_addr, len); |
315 | } |
316 | |
317 | static const struct header_ops macvlan_hard_header_ops = { |
318 | .create = macvlan_hard_header, |
319 | .rebuild = eth_rebuild_header, |
320 | .parse = eth_header_parse, |
321 | .cache = eth_header_cache, |
322 | .cache_update = eth_header_cache_update, |
323 | }; |
324 | |
325 | static int macvlan_open(struct net_device *dev) |
326 | { |
327 | struct macvlan_dev *vlan = netdev_priv(dev); |
328 | struct net_device *lowerdev = vlan->lowerdev; |
329 | int err; |
330 | |
331 | if (vlan->port->passthru) { |
332 | if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) |
333 | dev_set_promiscuity(lowerdev, 1); |
334 | goto hash_add; |
335 | } |
336 | |
337 | err = -EBUSY; |
338 | if (macvlan_addr_busy(vlan->port, dev->dev_addr)) |
339 | goto out; |
340 | |
341 | err = dev_uc_add(lowerdev, dev->dev_addr); |
342 | if (err < 0) |
343 | goto out; |
344 | if (dev->flags & IFF_ALLMULTI) { |
345 | err = dev_set_allmulti(lowerdev, 1); |
346 | if (err < 0) |
347 | goto del_unicast; |
348 | } |
349 | |
350 | hash_add: |
351 | macvlan_hash_add(vlan); |
352 | return 0; |
353 | |
354 | del_unicast: |
355 | dev_uc_del(lowerdev, dev->dev_addr); |
356 | out: |
357 | return err; |
358 | } |
359 | |
360 | static int macvlan_stop(struct net_device *dev) |
361 | { |
362 | struct macvlan_dev *vlan = netdev_priv(dev); |
363 | struct net_device *lowerdev = vlan->lowerdev; |
364 | |
365 | dev_uc_unsync(lowerdev, dev); |
366 | dev_mc_unsync(lowerdev, dev); |
367 | |
368 | if (vlan->port->passthru) { |
369 | if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) |
370 | dev_set_promiscuity(lowerdev, -1); |
371 | goto hash_del; |
372 | } |
373 | |
374 | if (dev->flags & IFF_ALLMULTI) |
375 | dev_set_allmulti(lowerdev, -1); |
376 | |
377 | dev_uc_del(lowerdev, dev->dev_addr); |
378 | |
379 | hash_del: |
380 | macvlan_hash_del(vlan, !dev->dismantle); |
381 | return 0; |
382 | } |
383 | |
384 | static int macvlan_set_mac_address(struct net_device *dev, void *p) |
385 | { |
386 | struct macvlan_dev *vlan = netdev_priv(dev); |
387 | struct net_device *lowerdev = vlan->lowerdev; |
388 | struct sockaddr *addr = p; |
389 | int err; |
390 | |
391 | if (!is_valid_ether_addr(addr->sa_data)) |
392 | return -EADDRNOTAVAIL; |
393 | |
394 | if (!(dev->flags & IFF_UP)) { |
395 | /* Just copy in the new address */ |
396 | memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN); |
397 | } else { |
398 | /* Rehash and update the device filters */ |
399 | if (macvlan_addr_busy(vlan->port, addr->sa_data)) |
400 | return -EBUSY; |
401 | |
402 | err = dev_uc_add(lowerdev, addr->sa_data); |
403 | if (err) |
404 | return err; |
405 | |
406 | dev_uc_del(lowerdev, dev->dev_addr); |
407 | |
408 | macvlan_hash_change_addr(vlan, addr->sa_data); |
409 | } |
410 | return 0; |
411 | } |
412 | |
413 | static void macvlan_change_rx_flags(struct net_device *dev, int change) |
414 | { |
415 | struct macvlan_dev *vlan = netdev_priv(dev); |
416 | struct net_device *lowerdev = vlan->lowerdev; |
417 | |
418 | if (change & IFF_ALLMULTI) |
419 | dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1); |
420 | } |
421 | |
422 | static void macvlan_set_mac_lists(struct net_device *dev) |
423 | { |
424 | struct macvlan_dev *vlan = netdev_priv(dev); |
425 | |
426 | if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) { |
427 | bitmap_fill(vlan->mc_filter, MACVLAN_MC_FILTER_SZ); |
428 | } else { |
429 | struct netdev_hw_addr *ha; |
430 | DECLARE_BITMAP(filter, MACVLAN_MC_FILTER_SZ); |
431 | |
432 | bitmap_zero(filter, MACVLAN_MC_FILTER_SZ); |
433 | netdev_for_each_mc_addr(ha, dev) { |
434 | __set_bit(mc_hash(vlan, ha->addr), filter); |
435 | } |
436 | |
437 | __set_bit(mc_hash(vlan, dev->broadcast), filter); |
438 | |
439 | bitmap_copy(vlan->mc_filter, filter, MACVLAN_MC_FILTER_SZ); |
440 | } |
441 | dev_uc_sync(vlan->lowerdev, dev); |
442 | dev_mc_sync(vlan->lowerdev, dev); |
443 | } |
444 | |
445 | static int macvlan_change_mtu(struct net_device *dev, int new_mtu) |
446 | { |
447 | struct macvlan_dev *vlan = netdev_priv(dev); |
448 | |
449 | if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu) |
450 | return -EINVAL; |
451 | dev->mtu = new_mtu; |
452 | return 0; |
453 | } |
454 | |
455 | /* |
456 | * macvlan network devices have devices nesting below it and are a special |
457 | * "super class" of normal network devices; split their locks off into a |
458 | * separate class since they always nest. |
459 | */ |
460 | static struct lock_class_key macvlan_netdev_xmit_lock_key; |
461 | static struct lock_class_key macvlan_netdev_addr_lock_key; |
462 | |
463 | #define MACVLAN_FEATURES \ |
464 | (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \ |
465 | NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \ |
466 | NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \ |
467 | NETIF_F_HW_VLAN_FILTER) |
468 | |
469 | #define MACVLAN_STATE_MASK \ |
470 | ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT)) |
471 | |
472 | static void macvlan_set_lockdep_class_one(struct net_device *dev, |
473 | struct netdev_queue *txq, |
474 | void *_unused) |
475 | { |
476 | lockdep_set_class(&txq->_xmit_lock, |
477 | &macvlan_netdev_xmit_lock_key); |
478 | } |
479 | |
480 | static void macvlan_set_lockdep_class(struct net_device *dev) |
481 | { |
482 | lockdep_set_class(&dev->addr_list_lock, |
483 | &macvlan_netdev_addr_lock_key); |
484 | netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL); |
485 | } |
486 | |
487 | static int macvlan_init(struct net_device *dev) |
488 | { |
489 | struct macvlan_dev *vlan = netdev_priv(dev); |
490 | const struct net_device *lowerdev = vlan->lowerdev; |
491 | |
492 | dev->state = (dev->state & ~MACVLAN_STATE_MASK) | |
493 | (lowerdev->state & MACVLAN_STATE_MASK); |
494 | dev->features = lowerdev->features & MACVLAN_FEATURES; |
495 | dev->features |= NETIF_F_LLTX; |
496 | dev->gso_max_size = lowerdev->gso_max_size; |
497 | dev->iflink = lowerdev->ifindex; |
498 | dev->hard_header_len = lowerdev->hard_header_len; |
499 | |
500 | macvlan_set_lockdep_class(dev); |
501 | |
502 | vlan->pcpu_stats = alloc_percpu(struct macvlan_pcpu_stats); |
503 | if (!vlan->pcpu_stats) |
504 | return -ENOMEM; |
505 | |
506 | return 0; |
507 | } |
508 | |
509 | static void macvlan_uninit(struct net_device *dev) |
510 | { |
511 | struct macvlan_dev *vlan = netdev_priv(dev); |
512 | struct macvlan_port *port = vlan->port; |
513 | |
514 | free_percpu(vlan->pcpu_stats); |
515 | |
516 | port->count -= 1; |
517 | if (!port->count) |
518 | macvlan_port_destroy(port->dev); |
519 | } |
520 | |
521 | static struct rtnl_link_stats64 *macvlan_dev_get_stats64(struct net_device *dev, |
522 | struct rtnl_link_stats64 *stats) |
523 | { |
524 | struct macvlan_dev *vlan = netdev_priv(dev); |
525 | |
526 | if (vlan->pcpu_stats) { |
527 | struct macvlan_pcpu_stats *p; |
528 | u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes; |
529 | u32 rx_errors = 0, tx_dropped = 0; |
530 | unsigned int start; |
531 | int i; |
532 | |
533 | for_each_possible_cpu(i) { |
534 | p = per_cpu_ptr(vlan->pcpu_stats, i); |
535 | do { |
536 | start = u64_stats_fetch_begin_bh(&p->syncp); |
537 | rx_packets = p->rx_packets; |
538 | rx_bytes = p->rx_bytes; |
539 | rx_multicast = p->rx_multicast; |
540 | tx_packets = p->tx_packets; |
541 | tx_bytes = p->tx_bytes; |
542 | } while (u64_stats_fetch_retry_bh(&p->syncp, start)); |
543 | |
544 | stats->rx_packets += rx_packets; |
545 | stats->rx_bytes += rx_bytes; |
546 | stats->multicast += rx_multicast; |
547 | stats->tx_packets += tx_packets; |
548 | stats->tx_bytes += tx_bytes; |
549 | /* rx_errors & tx_dropped are u32, updated |
550 | * without syncp protection. |
551 | */ |
552 | rx_errors += p->rx_errors; |
553 | tx_dropped += p->tx_dropped; |
554 | } |
555 | stats->rx_errors = rx_errors; |
556 | stats->rx_dropped = rx_errors; |
557 | stats->tx_dropped = tx_dropped; |
558 | } |
559 | return stats; |
560 | } |
561 | |
562 | static int macvlan_vlan_rx_add_vid(struct net_device *dev, |
563 | unsigned short vid) |
564 | { |
565 | struct macvlan_dev *vlan = netdev_priv(dev); |
566 | struct net_device *lowerdev = vlan->lowerdev; |
567 | |
568 | return vlan_vid_add(lowerdev, vid); |
569 | } |
570 | |
571 | static int macvlan_vlan_rx_kill_vid(struct net_device *dev, |
572 | unsigned short vid) |
573 | { |
574 | struct macvlan_dev *vlan = netdev_priv(dev); |
575 | struct net_device *lowerdev = vlan->lowerdev; |
576 | |
577 | vlan_vid_del(lowerdev, vid); |
578 | return 0; |
579 | } |
580 | |
581 | static int macvlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], |
582 | struct net_device *dev, |
583 | const unsigned char *addr, |
584 | u16 flags) |
585 | { |
586 | struct macvlan_dev *vlan = netdev_priv(dev); |
587 | int err = -EINVAL; |
588 | |
589 | if (!vlan->port->passthru) |
590 | return -EOPNOTSUPP; |
591 | |
592 | if (is_unicast_ether_addr(addr)) |
593 | err = dev_uc_add_excl(dev, addr); |
594 | else if (is_multicast_ether_addr(addr)) |
595 | err = dev_mc_add_excl(dev, addr); |
596 | |
597 | return err; |
598 | } |
599 | |
600 | static int macvlan_fdb_del(struct ndmsg *ndm, struct nlattr *tb[], |
601 | struct net_device *dev, |
602 | const unsigned char *addr) |
603 | { |
604 | struct macvlan_dev *vlan = netdev_priv(dev); |
605 | int err = -EINVAL; |
606 | |
607 | if (!vlan->port->passthru) |
608 | return -EOPNOTSUPP; |
609 | |
610 | if (is_unicast_ether_addr(addr)) |
611 | err = dev_uc_del(dev, addr); |
612 | else if (is_multicast_ether_addr(addr)) |
613 | err = dev_mc_del(dev, addr); |
614 | |
615 | return err; |
616 | } |
617 | |
618 | static void macvlan_ethtool_get_drvinfo(struct net_device *dev, |
619 | struct ethtool_drvinfo *drvinfo) |
620 | { |
621 | strlcpy(drvinfo->driver, "macvlan", sizeof(drvinfo->driver)); |
622 | strlcpy(drvinfo->version, "0.1", sizeof(drvinfo->version)); |
623 | } |
624 | |
625 | static int macvlan_ethtool_get_settings(struct net_device *dev, |
626 | struct ethtool_cmd *cmd) |
627 | { |
628 | const struct macvlan_dev *vlan = netdev_priv(dev); |
629 | |
630 | return __ethtool_get_settings(vlan->lowerdev, cmd); |
631 | } |
632 | |
633 | static const struct ethtool_ops macvlan_ethtool_ops = { |
634 | .get_link = ethtool_op_get_link, |
635 | .get_settings = macvlan_ethtool_get_settings, |
636 | .get_drvinfo = macvlan_ethtool_get_drvinfo, |
637 | }; |
638 | |
639 | static const struct net_device_ops macvlan_netdev_ops = { |
640 | .ndo_init = macvlan_init, |
641 | .ndo_uninit = macvlan_uninit, |
642 | .ndo_open = macvlan_open, |
643 | .ndo_stop = macvlan_stop, |
644 | .ndo_start_xmit = macvlan_start_xmit, |
645 | .ndo_change_mtu = macvlan_change_mtu, |
646 | .ndo_change_rx_flags = macvlan_change_rx_flags, |
647 | .ndo_set_mac_address = macvlan_set_mac_address, |
648 | .ndo_set_rx_mode = macvlan_set_mac_lists, |
649 | .ndo_get_stats64 = macvlan_dev_get_stats64, |
650 | .ndo_validate_addr = eth_validate_addr, |
651 | .ndo_vlan_rx_add_vid = macvlan_vlan_rx_add_vid, |
652 | .ndo_vlan_rx_kill_vid = macvlan_vlan_rx_kill_vid, |
653 | .ndo_fdb_add = macvlan_fdb_add, |
654 | .ndo_fdb_del = macvlan_fdb_del, |
655 | .ndo_fdb_dump = ndo_dflt_fdb_dump, |
656 | }; |
657 | |
658 | void macvlan_common_setup(struct net_device *dev) |
659 | { |
660 | ether_setup(dev); |
661 | |
662 | dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING); |
663 | dev->priv_flags |= IFF_UNICAST_FLT; |
664 | dev->netdev_ops = &macvlan_netdev_ops; |
665 | dev->destructor = free_netdev; |
666 | dev->header_ops = &macvlan_hard_header_ops, |
667 | dev->ethtool_ops = &macvlan_ethtool_ops; |
668 | } |
669 | EXPORT_SYMBOL_GPL(macvlan_common_setup); |
670 | |
671 | static void macvlan_setup(struct net_device *dev) |
672 | { |
673 | macvlan_common_setup(dev); |
674 | dev->tx_queue_len = 0; |
675 | } |
676 | |
677 | static int macvlan_port_create(struct net_device *dev) |
678 | { |
679 | struct macvlan_port *port; |
680 | unsigned int i; |
681 | int err; |
682 | |
683 | if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK) |
684 | return -EINVAL; |
685 | |
686 | port = kzalloc(sizeof(*port), GFP_KERNEL); |
687 | if (port == NULL) |
688 | return -ENOMEM; |
689 | |
690 | port->passthru = false; |
691 | port->dev = dev; |
692 | INIT_LIST_HEAD(&port->vlans); |
693 | for (i = 0; i < MACVLAN_HASH_SIZE; i++) |
694 | INIT_HLIST_HEAD(&port->vlan_hash[i]); |
695 | |
696 | err = netdev_rx_handler_register(dev, macvlan_handle_frame, port); |
697 | if (err) |
698 | kfree(port); |
699 | else |
700 | dev->priv_flags |= IFF_MACVLAN_PORT; |
701 | return err; |
702 | } |
703 | |
704 | static void macvlan_port_destroy(struct net_device *dev) |
705 | { |
706 | struct macvlan_port *port = macvlan_port_get(dev); |
707 | |
708 | dev->priv_flags &= ~IFF_MACVLAN_PORT; |
709 | netdev_rx_handler_unregister(dev); |
710 | kfree_rcu(port, rcu); |
711 | } |
712 | |
713 | static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[]) |
714 | { |
715 | if (tb[IFLA_ADDRESS]) { |
716 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) |
717 | return -EINVAL; |
718 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) |
719 | return -EADDRNOTAVAIL; |
720 | } |
721 | |
722 | if (data && data[IFLA_MACVLAN_MODE]) { |
723 | switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) { |
724 | case MACVLAN_MODE_PRIVATE: |
725 | case MACVLAN_MODE_VEPA: |
726 | case MACVLAN_MODE_BRIDGE: |
727 | case MACVLAN_MODE_PASSTHRU: |
728 | break; |
729 | default: |
730 | return -EINVAL; |
731 | } |
732 | } |
733 | return 0; |
734 | } |
735 | |
736 | int macvlan_common_newlink(struct net *src_net, struct net_device *dev, |
737 | struct nlattr *tb[], struct nlattr *data[], |
738 | int (*receive)(struct sk_buff *skb), |
739 | int (*forward)(struct net_device *dev, |
740 | struct sk_buff *skb)) |
741 | { |
742 | struct macvlan_dev *vlan = netdev_priv(dev); |
743 | struct macvlan_port *port; |
744 | struct net_device *lowerdev; |
745 | int err; |
746 | |
747 | if (!tb[IFLA_LINK]) |
748 | return -EINVAL; |
749 | |
750 | lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK])); |
751 | if (lowerdev == NULL) |
752 | return -ENODEV; |
753 | |
754 | /* When creating macvlans on top of other macvlans - use |
755 | * the real device as the lowerdev. |
756 | */ |
757 | if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) { |
758 | struct macvlan_dev *lowervlan = netdev_priv(lowerdev); |
759 | lowerdev = lowervlan->lowerdev; |
760 | } |
761 | |
762 | if (!tb[IFLA_MTU]) |
763 | dev->mtu = lowerdev->mtu; |
764 | else if (dev->mtu > lowerdev->mtu) |
765 | return -EINVAL; |
766 | |
767 | if (!tb[IFLA_ADDRESS]) |
768 | eth_hw_addr_random(dev); |
769 | |
770 | if (!macvlan_port_exists(lowerdev)) { |
771 | err = macvlan_port_create(lowerdev); |
772 | if (err < 0) |
773 | return err; |
774 | } |
775 | port = macvlan_port_get(lowerdev); |
776 | |
777 | /* Only 1 macvlan device can be created in passthru mode */ |
778 | if (port->passthru) |
779 | return -EINVAL; |
780 | |
781 | vlan->lowerdev = lowerdev; |
782 | vlan->dev = dev; |
783 | vlan->port = port; |
784 | vlan->receive = receive; |
785 | vlan->forward = forward; |
786 | |
787 | vlan->mode = MACVLAN_MODE_VEPA; |
788 | if (data && data[IFLA_MACVLAN_MODE]) |
789 | vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]); |
790 | |
791 | if (data && data[IFLA_MACVLAN_FLAGS]) |
792 | vlan->flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]); |
793 | |
794 | if (vlan->mode == MACVLAN_MODE_PASSTHRU) { |
795 | if (port->count) |
796 | return -EINVAL; |
797 | port->passthru = true; |
798 | memcpy(dev->dev_addr, lowerdev->dev_addr, ETH_ALEN); |
799 | } |
800 | |
801 | err = netdev_upper_dev_link(lowerdev, dev); |
802 | if (err) |
803 | goto destroy_port; |
804 | |
805 | port->count += 1; |
806 | err = register_netdevice(dev); |
807 | if (err < 0) |
808 | goto upper_dev_unlink; |
809 | |
810 | list_add_tail(&vlan->list, &port->vlans); |
811 | netif_stacked_transfer_operstate(lowerdev, dev); |
812 | |
813 | return 0; |
814 | |
815 | upper_dev_unlink: |
816 | netdev_upper_dev_unlink(lowerdev, dev); |
817 | destroy_port: |
818 | port->count -= 1; |
819 | if (!port->count) |
820 | macvlan_port_destroy(lowerdev); |
821 | |
822 | return err; |
823 | } |
824 | EXPORT_SYMBOL_GPL(macvlan_common_newlink); |
825 | |
826 | static int macvlan_newlink(struct net *src_net, struct net_device *dev, |
827 | struct nlattr *tb[], struct nlattr *data[]) |
828 | { |
829 | return macvlan_common_newlink(src_net, dev, tb, data, |
830 | netif_rx, |
831 | dev_forward_skb); |
832 | } |
833 | |
834 | void macvlan_dellink(struct net_device *dev, struct list_head *head) |
835 | { |
836 | struct macvlan_dev *vlan = netdev_priv(dev); |
837 | |
838 | list_del(&vlan->list); |
839 | unregister_netdevice_queue(dev, head); |
840 | netdev_upper_dev_unlink(vlan->lowerdev, dev); |
841 | } |
842 | EXPORT_SYMBOL_GPL(macvlan_dellink); |
843 | |
844 | static int macvlan_changelink(struct net_device *dev, |
845 | struct nlattr *tb[], struct nlattr *data[]) |
846 | { |
847 | struct macvlan_dev *vlan = netdev_priv(dev); |
848 | if (data && data[IFLA_MACVLAN_MODE]) |
849 | vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]); |
850 | if (data && data[IFLA_MACVLAN_FLAGS]) { |
851 | __u16 flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]); |
852 | bool promisc = (flags ^ vlan->flags) & MACVLAN_FLAG_NOPROMISC; |
853 | |
854 | if (promisc && (flags & MACVLAN_FLAG_NOPROMISC)) |
855 | dev_set_promiscuity(vlan->lowerdev, -1); |
856 | else if (promisc && !(flags & MACVLAN_FLAG_NOPROMISC)) |
857 | dev_set_promiscuity(vlan->lowerdev, 1); |
858 | vlan->flags = flags; |
859 | } |
860 | return 0; |
861 | } |
862 | |
863 | static size_t macvlan_get_size(const struct net_device *dev) |
864 | { |
865 | return (0 |
866 | + nla_total_size(4) /* IFLA_MACVLAN_MODE */ |
867 | + nla_total_size(2) /* IFLA_MACVLAN_FLAGS */ |
868 | ); |
869 | } |
870 | |
871 | static int macvlan_fill_info(struct sk_buff *skb, |
872 | const struct net_device *dev) |
873 | { |
874 | struct macvlan_dev *vlan = netdev_priv(dev); |
875 | |
876 | if (nla_put_u32(skb, IFLA_MACVLAN_MODE, vlan->mode)) |
877 | goto nla_put_failure; |
878 | if (nla_put_u16(skb, IFLA_MACVLAN_FLAGS, vlan->flags)) |
879 | goto nla_put_failure; |
880 | return 0; |
881 | |
882 | nla_put_failure: |
883 | return -EMSGSIZE; |
884 | } |
885 | |
886 | static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = { |
887 | [IFLA_MACVLAN_MODE] = { .type = NLA_U32 }, |
888 | [IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 }, |
889 | }; |
890 | |
891 | int macvlan_link_register(struct rtnl_link_ops *ops) |
892 | { |
893 | /* common fields */ |
894 | ops->priv_size = sizeof(struct macvlan_dev); |
895 | ops->validate = macvlan_validate; |
896 | ops->maxtype = IFLA_MACVLAN_MAX; |
897 | ops->policy = macvlan_policy; |
898 | ops->changelink = macvlan_changelink; |
899 | ops->get_size = macvlan_get_size; |
900 | ops->fill_info = macvlan_fill_info; |
901 | |
902 | return rtnl_link_register(ops); |
903 | }; |
904 | EXPORT_SYMBOL_GPL(macvlan_link_register); |
905 | |
906 | static struct rtnl_link_ops macvlan_link_ops = { |
907 | .kind = "macvlan", |
908 | .setup = macvlan_setup, |
909 | .newlink = macvlan_newlink, |
910 | .dellink = macvlan_dellink, |
911 | }; |
912 | |
913 | static int macvlan_device_event(struct notifier_block *unused, |
914 | unsigned long event, void *ptr) |
915 | { |
916 | struct net_device *dev = ptr; |
917 | struct macvlan_dev *vlan, *next; |
918 | struct macvlan_port *port; |
919 | LIST_HEAD(list_kill); |
920 | |
921 | if (!macvlan_port_exists(dev)) |
922 | return NOTIFY_DONE; |
923 | |
924 | port = macvlan_port_get(dev); |
925 | |
926 | switch (event) { |
927 | case NETDEV_CHANGE: |
928 | list_for_each_entry(vlan, &port->vlans, list) |
929 | netif_stacked_transfer_operstate(vlan->lowerdev, |
930 | vlan->dev); |
931 | break; |
932 | case NETDEV_FEAT_CHANGE: |
933 | list_for_each_entry(vlan, &port->vlans, list) { |
934 | vlan->dev->features = dev->features & MACVLAN_FEATURES; |
935 | vlan->dev->gso_max_size = dev->gso_max_size; |
936 | netdev_features_change(vlan->dev); |
937 | } |
938 | break; |
939 | case NETDEV_UNREGISTER: |
940 | /* twiddle thumbs on netns device moves */ |
941 | if (dev->reg_state != NETREG_UNREGISTERING) |
942 | break; |
943 | |
944 | list_for_each_entry_safe(vlan, next, &port->vlans, list) |
945 | vlan->dev->rtnl_link_ops->dellink(vlan->dev, &list_kill); |
946 | unregister_netdevice_many(&list_kill); |
947 | list_del(&list_kill); |
948 | break; |
949 | case NETDEV_PRE_TYPE_CHANGE: |
950 | /* Forbid underlaying device to change its type. */ |
951 | return NOTIFY_BAD; |
952 | } |
953 | return NOTIFY_DONE; |
954 | } |
955 | |
956 | static struct notifier_block macvlan_notifier_block __read_mostly = { |
957 | .notifier_call = macvlan_device_event, |
958 | }; |
959 | |
960 | static int __init macvlan_init_module(void) |
961 | { |
962 | int err; |
963 | |
964 | register_netdevice_notifier(&macvlan_notifier_block); |
965 | |
966 | err = macvlan_link_register(&macvlan_link_ops); |
967 | if (err < 0) |
968 | goto err1; |
969 | return 0; |
970 | err1: |
971 | unregister_netdevice_notifier(&macvlan_notifier_block); |
972 | return err; |
973 | } |
974 | |
975 | static void __exit macvlan_cleanup_module(void) |
976 | { |
977 | rtnl_link_unregister(&macvlan_link_ops); |
978 | unregister_netdevice_notifier(&macvlan_notifier_block); |
979 | } |
980 | |
981 | module_init(macvlan_init_module); |
982 | module_exit(macvlan_cleanup_module); |
983 | |
984 | MODULE_LICENSE("GPL"); |
985 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); |
986 | MODULE_DESCRIPTION("Driver for MAC address based VLANs"); |
987 | MODULE_ALIAS_RTNL_LINK("macvlan"); |
988 |
Branches:
ben-wpan
ben-wpan-stefan
javiroman/ks7010
jz-2.6.34
jz-2.6.34-rc5
jz-2.6.34-rc6
jz-2.6.34-rc7
jz-2.6.35
jz-2.6.36
jz-2.6.37
jz-2.6.38
jz-2.6.39
jz-3.0
jz-3.1
jz-3.11
jz-3.12
jz-3.13
jz-3.15
jz-3.16
jz-3.18-dt
jz-3.2
jz-3.3
jz-3.4
jz-3.5
jz-3.6
jz-3.6-rc2-pwm
jz-3.9
jz-3.9-clk
jz-3.9-rc8
jz47xx
jz47xx-2.6.38
master
Tags:
od-2011-09-04
od-2011-09-18
v2.6.34-rc5
v2.6.34-rc6
v2.6.34-rc7
v3.9