blob: b7f41c52766f66866652fe2cf2d1ff43f4b84807 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
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 <linux/workqueue.h>
34#include <net/rtnetlink.h>
35#include <net/xfrm.h>
36#include <linux/netpoll.h>
37
38#define MACVLAN_HASH_BITS 8
39#define MACVLAN_HASH_SIZE (1<<MACVLAN_HASH_BITS)
40#define MACVLAN_BC_QUEUE_LEN 1000
41
42#define MACVLAN_F_PASSTHRU 1
43#define MACVLAN_F_ADDRCHANGE 2
44
45struct macvlan_port {
46 struct net_device *dev;
47 struct hlist_head vlan_hash[MACVLAN_HASH_SIZE];
48 struct list_head vlans;
49 struct sk_buff_head bc_queue;
50 struct work_struct bc_work;
51 u32 flags;
52 int count;
53 struct hlist_head vlan_source_hash[MACVLAN_HASH_SIZE];
54 DECLARE_BITMAP(mc_filter, MACVLAN_MC_FILTER_SZ);
55 unsigned char perm_addr[ETH_ALEN];
56};
57
58struct macvlan_source_entry {
59 struct hlist_node hlist;
60 struct macvlan_dev *vlan;
61 unsigned char addr[6+2] __aligned(sizeof(u16));
62 struct rcu_head rcu;
63};
64
65struct macvlan_skb_cb {
66 const struct macvlan_dev *src;
67};
68
69#define MACVLAN_SKB_CB(__skb) ((struct macvlan_skb_cb *)&((__skb)->cb[0]))
70
71static void macvlan_port_destroy(struct net_device *dev);
72
73static inline bool macvlan_passthru(const struct macvlan_port *port)
74{
75 return port->flags & MACVLAN_F_PASSTHRU;
76}
77
78static inline void macvlan_set_passthru(struct macvlan_port *port)
79{
80 port->flags |= MACVLAN_F_PASSTHRU;
81}
82
83static inline bool macvlan_addr_change(const struct macvlan_port *port)
84{
85 return port->flags & MACVLAN_F_ADDRCHANGE;
86}
87
88static inline void macvlan_set_addr_change(struct macvlan_port *port)
89{
90 port->flags |= MACVLAN_F_ADDRCHANGE;
91}
92
93static inline void macvlan_clear_addr_change(struct macvlan_port *port)
94{
95 port->flags &= ~MACVLAN_F_ADDRCHANGE;
96}
97
98/* Hash Ethernet address */
99static u32 macvlan_eth_hash(const unsigned char *addr)
100{
101 u64 value = get_unaligned((u64 *)addr);
102
103 /* only want 6 bytes */
104#ifdef __BIG_ENDIAN
105 value >>= 16;
106#else
107 value <<= 16;
108#endif
109 return hash_64(value, MACVLAN_HASH_BITS);
110}
111
112static struct macvlan_port *macvlan_port_get_rcu(const struct net_device *dev)
113{
114 return rcu_dereference(dev->rx_handler_data);
115}
116
117static struct macvlan_port *macvlan_port_get_rtnl(const struct net_device *dev)
118{
119 return rtnl_dereference(dev->rx_handler_data);
120}
121
122#define macvlan_port_exists(dev) (dev->priv_flags & IFF_MACVLAN_PORT)
123
124static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
125 const unsigned char *addr)
126{
127 struct macvlan_dev *vlan;
128 u32 idx = macvlan_eth_hash(addr);
129
130 hlist_for_each_entry_rcu(vlan, &port->vlan_hash[idx], hlist) {
131 if (ether_addr_equal_64bits(vlan->dev->dev_addr, addr))
132 return vlan;
133 }
134 return NULL;
135}
136
137static struct macvlan_source_entry *macvlan_hash_lookup_source(
138 const struct macvlan_dev *vlan,
139 const unsigned char *addr)
140{
141 struct macvlan_source_entry *entry;
142 u32 idx = macvlan_eth_hash(addr);
143 struct hlist_head *h = &vlan->port->vlan_source_hash[idx];
144
145 hlist_for_each_entry_rcu(entry, h, hlist) {
146 if (ether_addr_equal_64bits(entry->addr, addr) &&
147 entry->vlan == vlan)
148 return entry;
149 }
150 return NULL;
151}
152
153static int macvlan_hash_add_source(struct macvlan_dev *vlan,
154 const unsigned char *addr)
155{
156 struct macvlan_port *port = vlan->port;
157 struct macvlan_source_entry *entry;
158 struct hlist_head *h;
159
160 entry = macvlan_hash_lookup_source(vlan, addr);
161 if (entry)
162 return 0;
163
164 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
165 if (!entry)
166 return -ENOMEM;
167
168 ether_addr_copy(entry->addr, addr);
169 entry->vlan = vlan;
170 h = &port->vlan_source_hash[macvlan_eth_hash(addr)];
171 hlist_add_head_rcu(&entry->hlist, h);
172 vlan->macaddr_count++;
173
174 return 0;
175}
176
177static void macvlan_hash_add(struct macvlan_dev *vlan)
178{
179 struct macvlan_port *port = vlan->port;
180 const unsigned char *addr = vlan->dev->dev_addr;
181 u32 idx = macvlan_eth_hash(addr);
182
183 hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[idx]);
184}
185
186static void macvlan_hash_del_source(struct macvlan_source_entry *entry)
187{
188 hlist_del_rcu(&entry->hlist);
189 kfree_rcu(entry, rcu);
190}
191
192static void macvlan_hash_del(struct macvlan_dev *vlan, bool sync)
193{
194 hlist_del_rcu(&vlan->hlist);
195 if (sync)
196 synchronize_rcu();
197}
198
199static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
200 const unsigned char *addr)
201{
202 macvlan_hash_del(vlan, true);
203 /* Now that we are unhashed it is safe to change the device
204 * address without confusing packet delivery.
205 */
206 memcpy(vlan->dev->dev_addr, addr, ETH_ALEN);
207 macvlan_hash_add(vlan);
208}
209
210static bool macvlan_addr_busy(const struct macvlan_port *port,
211 const unsigned char *addr)
212{
213 /* Test to see if the specified address is
214 * currently in use by the underlying device or
215 * another macvlan.
216 */
217 if (!macvlan_passthru(port) && !macvlan_addr_change(port) &&
218 ether_addr_equal_64bits(port->dev->dev_addr, addr))
219 return true;
220
221 if (macvlan_hash_lookup(port, addr))
222 return true;
223
224 return false;
225}
226
227
228static int macvlan_broadcast_one(struct sk_buff *skb,
229 const struct macvlan_dev *vlan,
230 const struct ethhdr *eth, bool local)
231{
232 struct net_device *dev = vlan->dev;
233
234 if (local)
235 return __dev_forward_skb(dev, skb);
236
237 skb->dev = dev;
238 if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast))
239 skb->pkt_type = PACKET_BROADCAST;
240 else
241 skb->pkt_type = PACKET_MULTICAST;
242
243 return 0;
244}
245
246static u32 macvlan_hash_mix(const struct macvlan_dev *vlan)
247{
248 return (u32)(((unsigned long)vlan) >> L1_CACHE_SHIFT);
249}
250
251
252static unsigned int mc_hash(const struct macvlan_dev *vlan,
253 const unsigned char *addr)
254{
255 u32 val = __get_unaligned_cpu32(addr + 2);
256
257 val ^= macvlan_hash_mix(vlan);
258 return hash_32(val, MACVLAN_MC_FILTER_BITS);
259}
260
261static void macvlan_broadcast(struct sk_buff *skb,
262 const struct macvlan_port *port,
263 struct net_device *src,
264 enum macvlan_mode mode)
265{
266 const struct ethhdr *eth = eth_hdr(skb);
267 const struct macvlan_dev *vlan;
268 struct sk_buff *nskb;
269 unsigned int i;
270 int err;
271 unsigned int hash;
272
273 if (skb->protocol == htons(ETH_P_PAUSE))
274 return;
275
276 for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
277 hlist_for_each_entry_rcu(vlan, &port->vlan_hash[i], hlist) {
278 if (vlan->dev == src || !(vlan->mode & mode))
279 continue;
280
281 hash = mc_hash(vlan, eth->h_dest);
282 if (!test_bit(hash, vlan->mc_filter))
283 continue;
284
285 err = NET_RX_DROP;
286 nskb = skb_clone(skb, GFP_ATOMIC);
287 if (likely(nskb))
288 err = macvlan_broadcast_one(
289 nskb, vlan, eth,
290 mode == MACVLAN_MODE_BRIDGE) ?:
291 netif_rx_ni(nskb);
292 macvlan_count_rx(vlan, skb->len + ETH_HLEN,
293 err == NET_RX_SUCCESS, true);
294 }
295 }
296}
297
298static void macvlan_process_broadcast(struct work_struct *w)
299{
300 struct macvlan_port *port = container_of(w, struct macvlan_port,
301 bc_work);
302 struct sk_buff *skb;
303 struct sk_buff_head list;
304
305 __skb_queue_head_init(&list);
306
307 spin_lock_bh(&port->bc_queue.lock);
308 skb_queue_splice_tail_init(&port->bc_queue, &list);
309 spin_unlock_bh(&port->bc_queue.lock);
310
311 while ((skb = __skb_dequeue(&list))) {
312 const struct macvlan_dev *src = MACVLAN_SKB_CB(skb)->src;
313
314 rcu_read_lock();
315
316 if (!src)
317 /* frame comes from an external address */
318 macvlan_broadcast(skb, port, NULL,
319 MACVLAN_MODE_PRIVATE |
320 MACVLAN_MODE_VEPA |
321 MACVLAN_MODE_PASSTHRU|
322 MACVLAN_MODE_BRIDGE);
323 else if (src->mode == MACVLAN_MODE_VEPA)
324 /* flood to everyone except source */
325 macvlan_broadcast(skb, port, src->dev,
326 MACVLAN_MODE_VEPA |
327 MACVLAN_MODE_BRIDGE);
328 else
329 /*
330 * flood only to VEPA ports, bridge ports
331 * already saw the frame on the way out.
332 */
333 macvlan_broadcast(skb, port, src->dev,
334 MACVLAN_MODE_VEPA);
335
336 rcu_read_unlock();
337
338 if (src)
339 dev_put(src->dev);
340 kfree_skb(skb);
341
342 cond_resched();
343 }
344}
345
346static void macvlan_broadcast_enqueue(struct macvlan_port *port,
347 const struct macvlan_dev *src,
348 struct sk_buff *skb)
349{
350 struct sk_buff *nskb;
351 int err = -ENOMEM;
352
353 nskb = skb_clone(skb, GFP_ATOMIC);
354 if (!nskb)
355 goto err;
356
357 MACVLAN_SKB_CB(nskb)->src = src;
358
359 spin_lock(&port->bc_queue.lock);
360 if (skb_queue_len(&port->bc_queue) < MACVLAN_BC_QUEUE_LEN) {
361 if (src)
362 dev_hold(src->dev);
363 __skb_queue_tail(&port->bc_queue, nskb);
364 err = 0;
365 }
366 spin_unlock(&port->bc_queue.lock);
367
368 schedule_work(&port->bc_work);
369
370 if (err)
371 goto free_nskb;
372
373 return;
374
375free_nskb:
376 kfree_skb(nskb);
377err:
378 atomic_long_inc(&skb->dev->rx_dropped);
379}
380
381static void macvlan_flush_sources(struct macvlan_port *port,
382 struct macvlan_dev *vlan)
383{
384 int i;
385
386 for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
387 struct hlist_node *h, *n;
388
389 hlist_for_each_safe(h, n, &port->vlan_source_hash[i]) {
390 struct macvlan_source_entry *entry;
391
392 entry = hlist_entry(h, struct macvlan_source_entry,
393 hlist);
394 if (entry->vlan == vlan)
395 macvlan_hash_del_source(entry);
396 }
397 }
398 vlan->macaddr_count = 0;
399}
400
401static void macvlan_forward_source_one(struct sk_buff *skb,
402 struct macvlan_dev *vlan)
403{
404 struct sk_buff *nskb;
405 struct net_device *dev;
406 int len;
407 int ret;
408
409 dev = vlan->dev;
410 if (unlikely(!(dev->flags & IFF_UP)))
411 return;
412
413 nskb = skb_clone(skb, GFP_ATOMIC);
414 if (!nskb)
415 return;
416
417 len = nskb->len + ETH_HLEN;
418 nskb->dev = dev;
419 nskb->pkt_type = PACKET_HOST;
420
421 ret = netif_rx(nskb);
422 macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, false);
423}
424
425static void macvlan_forward_source(struct sk_buff *skb,
426 struct macvlan_port *port,
427 const unsigned char *addr)
428{
429 struct macvlan_source_entry *entry;
430 u32 idx = macvlan_eth_hash(addr);
431 struct hlist_head *h = &port->vlan_source_hash[idx];
432
433 hlist_for_each_entry_rcu(entry, h, hlist) {
434 if (ether_addr_equal_64bits(entry->addr, addr))
435 macvlan_forward_source_one(skb, entry->vlan);
436 }
437}
438
439/* called under rcu_read_lock() from netif_receive_skb */
440static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb)
441{
442 struct macvlan_port *port;
443 struct sk_buff *skb = *pskb;
444 const struct ethhdr *eth = eth_hdr(skb);
445 const struct macvlan_dev *vlan;
446 const struct macvlan_dev *src;
447 struct net_device *dev;
448 unsigned int len = 0;
449 int ret;
450 rx_handler_result_t handle_res;
451
452 /* Packets from dev_loopback_xmit() do not have L2 header, bail out */
453 if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
454 return RX_HANDLER_PASS;
455
456 port = macvlan_port_get_rcu(skb->dev);
457 if (is_multicast_ether_addr(eth->h_dest)) {
458 unsigned int hash;
459
460 skb = ip_check_defrag(dev_net(skb->dev), skb, IP_DEFRAG_MACVLAN);
461 if (!skb)
462 return RX_HANDLER_CONSUMED;
463 *pskb = skb;
464 eth = eth_hdr(skb);
465 macvlan_forward_source(skb, port, eth->h_source);
466 src = macvlan_hash_lookup(port, eth->h_source);
467 if (src && src->mode != MACVLAN_MODE_VEPA &&
468 src->mode != MACVLAN_MODE_BRIDGE) {
469 /* forward to original port. */
470 vlan = src;
471 ret = macvlan_broadcast_one(skb, vlan, eth, 0) ?:
472 netif_rx(skb);
473 handle_res = RX_HANDLER_CONSUMED;
474 goto out;
475 }
476
477 hash = mc_hash(NULL, eth->h_dest);
478 if (test_bit(hash, port->mc_filter))
479 macvlan_broadcast_enqueue(port, src, skb);
480
481 return RX_HANDLER_PASS;
482 }
483
484 macvlan_forward_source(skb, port, eth->h_source);
485 if (macvlan_passthru(port))
486 vlan = list_first_or_null_rcu(&port->vlans,
487 struct macvlan_dev, list);
488 else
489 vlan = macvlan_hash_lookup(port, eth->h_dest);
490 if (!vlan || vlan->mode == MACVLAN_MODE_SOURCE)
491 return RX_HANDLER_PASS;
492
493 dev = vlan->dev;
494 if (unlikely(!(dev->flags & IFF_UP))) {
495 kfree_skb(skb);
496 return RX_HANDLER_CONSUMED;
497 }
498 len = skb->len + ETH_HLEN;
499 skb = skb_share_check(skb, GFP_ATOMIC);
500 if (!skb) {
501 ret = NET_RX_DROP;
502 handle_res = RX_HANDLER_CONSUMED;
503 goto out;
504 }
505
506 *pskb = skb;
507 skb->dev = dev;
508 skb->pkt_type = PACKET_HOST;
509
510 ret = NET_RX_SUCCESS;
511 handle_res = RX_HANDLER_ANOTHER;
512out:
513 macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, false);
514 return handle_res;
515}
516
517static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
518{
519 const struct macvlan_dev *vlan = netdev_priv(dev);
520 const struct macvlan_port *port = vlan->port;
521 const struct macvlan_dev *dest;
522
523 if (vlan->mode == MACVLAN_MODE_BRIDGE) {
524 const struct ethhdr *eth = skb_eth_hdr(skb);
525
526 /* send to other bridge ports directly */
527 if (is_multicast_ether_addr(eth->h_dest)) {
528 skb_reset_mac_header(skb);
529 macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE);
530 goto xmit_world;
531 }
532
533 dest = macvlan_hash_lookup(port, eth->h_dest);
534 if (dest && dest->mode == MACVLAN_MODE_BRIDGE) {
535 /* send to lowerdev first for its network taps */
536 dev_forward_skb(vlan->lowerdev, skb);
537
538 return NET_XMIT_SUCCESS;
539 }
540 }
541
542xmit_world:
543 skb->dev = vlan->lowerdev;
544 return dev_queue_xmit(skb);
545}
546
547static inline netdev_tx_t macvlan_netpoll_send_skb(struct macvlan_dev *vlan, struct sk_buff *skb)
548{
549#ifdef CONFIG_NET_POLL_CONTROLLER
550 if (vlan->netpoll)
551 netpoll_send_skb(vlan->netpoll, skb);
552#else
553 BUG();
554#endif
555 return NETDEV_TX_OK;
556}
557
558static netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
559 struct net_device *dev)
560{
561 unsigned int len = skb->len;
562 int ret;
563 struct macvlan_dev *vlan = netdev_priv(dev);
564
565 if (unlikely(netpoll_tx_running(dev)))
566 return macvlan_netpoll_send_skb(vlan, skb);
567
568 if (vlan->fwd_priv) {
569 skb->dev = vlan->lowerdev;
570 ret = dev_queue_xmit_accel(skb, vlan->fwd_priv);
571 } else {
572 ret = macvlan_queue_xmit(skb, dev);
573 }
574
575 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
576 struct vlan_pcpu_stats *pcpu_stats;
577
578 pcpu_stats = this_cpu_ptr(vlan->pcpu_stats);
579 u64_stats_update_begin(&pcpu_stats->syncp);
580 pcpu_stats->tx_packets++;
581 pcpu_stats->tx_bytes += len;
582 u64_stats_update_end(&pcpu_stats->syncp);
583 } else {
584 this_cpu_inc(vlan->pcpu_stats->tx_dropped);
585 }
586 return ret;
587}
588
589static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
590 unsigned short type, const void *daddr,
591 const void *saddr, unsigned len)
592{
593 const struct macvlan_dev *vlan = netdev_priv(dev);
594 struct net_device *lowerdev = vlan->lowerdev;
595
596 return dev_hard_header(skb, lowerdev, type, daddr,
597 saddr ? : dev->dev_addr, len);
598}
599
600static const struct header_ops macvlan_hard_header_ops = {
601 .create = macvlan_hard_header,
602 .parse = eth_header_parse,
603 .cache = eth_header_cache,
604 .cache_update = eth_header_cache_update,
605};
606
607static struct rtnl_link_ops macvlan_link_ops;
608
609static int macvlan_open(struct net_device *dev)
610{
611 struct macvlan_dev *vlan = netdev_priv(dev);
612 struct net_device *lowerdev = vlan->lowerdev;
613 int err;
614
615 if (macvlan_passthru(vlan->port)) {
616 if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) {
617 err = dev_set_promiscuity(lowerdev, 1);
618 if (err < 0)
619 goto out;
620 }
621 goto hash_add;
622 }
623
624 if (lowerdev->features & NETIF_F_HW_L2FW_DOFFLOAD &&
625 dev->rtnl_link_ops == &macvlan_link_ops) {
626 vlan->fwd_priv =
627 lowerdev->netdev_ops->ndo_dfwd_add_station(lowerdev, dev);
628
629 /* If we get a NULL pointer back, or if we get an error
630 * then we should just fall through to the non accelerated path
631 */
632 if (IS_ERR_OR_NULL(vlan->fwd_priv)) {
633 vlan->fwd_priv = NULL;
634 } else
635 return 0;
636 }
637
638 err = -EBUSY;
639 if (macvlan_addr_busy(vlan->port, dev->dev_addr))
640 goto out;
641
642 err = dev_uc_add(lowerdev, dev->dev_addr);
643 if (err < 0)
644 goto out;
645 if (dev->flags & IFF_ALLMULTI) {
646 err = dev_set_allmulti(lowerdev, 1);
647 if (err < 0)
648 goto del_unicast;
649 }
650
651 if (dev->flags & IFF_PROMISC) {
652 err = dev_set_promiscuity(lowerdev, 1);
653 if (err < 0)
654 goto clear_multi;
655 }
656
657hash_add:
658 macvlan_hash_add(vlan);
659 return 0;
660
661clear_multi:
662 if (dev->flags & IFF_ALLMULTI)
663 dev_set_allmulti(lowerdev, -1);
664del_unicast:
665 dev_uc_del(lowerdev, dev->dev_addr);
666out:
667 if (vlan->fwd_priv) {
668 lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev,
669 vlan->fwd_priv);
670 vlan->fwd_priv = NULL;
671 }
672 return err;
673}
674
675static int macvlan_stop(struct net_device *dev)
676{
677 struct macvlan_dev *vlan = netdev_priv(dev);
678 struct net_device *lowerdev = vlan->lowerdev;
679
680 if (vlan->fwd_priv) {
681 lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev,
682 vlan->fwd_priv);
683 vlan->fwd_priv = NULL;
684 return 0;
685 }
686
687 dev_uc_unsync(lowerdev, dev);
688 dev_mc_unsync(lowerdev, dev);
689
690 if (macvlan_passthru(vlan->port)) {
691 if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC))
692 dev_set_promiscuity(lowerdev, -1);
693 goto hash_del;
694 }
695
696 if (dev->flags & IFF_ALLMULTI)
697 dev_set_allmulti(lowerdev, -1);
698
699 if (dev->flags & IFF_PROMISC)
700 dev_set_promiscuity(lowerdev, -1);
701
702 dev_uc_del(lowerdev, dev->dev_addr);
703
704hash_del:
705 macvlan_hash_del(vlan, !dev->dismantle);
706 return 0;
707}
708
709static int macvlan_sync_address(struct net_device *dev, unsigned char *addr)
710{
711 struct macvlan_dev *vlan = netdev_priv(dev);
712 struct net_device *lowerdev = vlan->lowerdev;
713 struct macvlan_port *port = vlan->port;
714 int err;
715
716 if (!(dev->flags & IFF_UP)) {
717 /* Just copy in the new address */
718 ether_addr_copy(dev->dev_addr, addr);
719 } else {
720 /* Rehash and update the device filters */
721 if (macvlan_addr_busy(vlan->port, addr))
722 return -EBUSY;
723
724 if (!macvlan_passthru(port)) {
725 err = dev_uc_add(lowerdev, addr);
726 if (err)
727 return err;
728
729 dev_uc_del(lowerdev, dev->dev_addr);
730 }
731
732 macvlan_hash_change_addr(vlan, addr);
733 }
734 if (macvlan_passthru(port) && !macvlan_addr_change(port)) {
735 /* Since addr_change isn't set, we are here due to lower
736 * device change. Save the lower-dev address so we can
737 * restore it later.
738 */
739 ether_addr_copy(vlan->port->perm_addr,
740 lowerdev->dev_addr);
741 }
742 macvlan_clear_addr_change(port);
743 return 0;
744}
745
746static int macvlan_set_mac_address(struct net_device *dev, void *p)
747{
748 struct macvlan_dev *vlan = netdev_priv(dev);
749 struct sockaddr *addr = p;
750
751 if (!is_valid_ether_addr(addr->sa_data))
752 return -EADDRNOTAVAIL;
753
754 /* If the addresses are the same, this is a no-op */
755 if (ether_addr_equal(dev->dev_addr, addr->sa_data))
756 return 0;
757
758 if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
759 macvlan_set_addr_change(vlan->port);
760 return dev_set_mac_address(vlan->lowerdev, addr);
761 }
762
763 return macvlan_sync_address(dev, addr->sa_data);
764}
765
766static void macvlan_change_rx_flags(struct net_device *dev, int change)
767{
768 struct macvlan_dev *vlan = netdev_priv(dev);
769 struct net_device *lowerdev = vlan->lowerdev;
770
771 if (dev->flags & IFF_UP) {
772 if (change & IFF_ALLMULTI)
773 dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
774 if (change & IFF_PROMISC)
775 dev_set_promiscuity(lowerdev,
776 dev->flags & IFF_PROMISC ? 1 : -1);
777
778 }
779}
780
781static void macvlan_compute_filter(unsigned long *mc_filter,
782 struct net_device *dev,
783 struct macvlan_dev *vlan)
784{
785 if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
786 bitmap_fill(mc_filter, MACVLAN_MC_FILTER_SZ);
787 } else {
788 struct netdev_hw_addr *ha;
789 DECLARE_BITMAP(filter, MACVLAN_MC_FILTER_SZ);
790
791 bitmap_zero(filter, MACVLAN_MC_FILTER_SZ);
792 netdev_for_each_mc_addr(ha, dev) {
793 __set_bit(mc_hash(vlan, ha->addr), filter);
794 }
795
796 __set_bit(mc_hash(vlan, dev->broadcast), filter);
797
798 bitmap_copy(mc_filter, filter, MACVLAN_MC_FILTER_SZ);
799 }
800}
801
802static void macvlan_set_mac_lists(struct net_device *dev)
803{
804 struct macvlan_dev *vlan = netdev_priv(dev);
805
806 macvlan_compute_filter(vlan->mc_filter, dev, vlan);
807
808 dev_uc_sync(vlan->lowerdev, dev);
809 dev_mc_sync(vlan->lowerdev, dev);
810
811 /* This is slightly inaccurate as we're including the subscription
812 * list of vlan->lowerdev too.
813 *
814 * Bug alert: This only works if everyone has the same broadcast
815 * address as lowerdev. As soon as someone changes theirs this
816 * will break.
817 *
818 * However, this is already broken as when you change your broadcast
819 * address we don't get called.
820 *
821 * The solution is to maintain a list of broadcast addresses like
822 * we do for uc/mc, if you care.
823 */
824 macvlan_compute_filter(vlan->port->mc_filter, vlan->lowerdev, NULL);
825}
826
827static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
828{
829 struct macvlan_dev *vlan = netdev_priv(dev);
830
831 if (vlan->lowerdev->mtu < new_mtu)
832 return -EINVAL;
833 dev->mtu = new_mtu;
834 return 0;
835}
836
837/*
838 * macvlan network devices have devices nesting below it and are a special
839 * "super class" of normal network devices; split their locks off into a
840 * separate class since they always nest.
841 */
842static struct lock_class_key macvlan_netdev_addr_lock_key;
843
844#define ALWAYS_ON_OFFLOADS \
845 (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE | \
846 NETIF_F_GSO_ROBUST | NETIF_F_GSO_ENCAP_ALL)
847
848#define ALWAYS_ON_FEATURES (ALWAYS_ON_OFFLOADS | NETIF_F_LLTX)
849
850#define MACVLAN_FEATURES \
851 (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
852 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_LRO | \
853 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
854 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
855
856#define MACVLAN_STATE_MASK \
857 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
858
859static int macvlan_get_nest_level(struct net_device *dev)
860{
861 return ((struct macvlan_dev *)netdev_priv(dev))->nest_level;
862}
863
864static void macvlan_set_lockdep_class(struct net_device *dev)
865{
866 netdev_lockdep_set_classes(dev);
867 lockdep_set_class_and_subclass(&dev->addr_list_lock,
868 &macvlan_netdev_addr_lock_key,
869 macvlan_get_nest_level(dev));
870}
871
872static int macvlan_init(struct net_device *dev)
873{
874 struct macvlan_dev *vlan = netdev_priv(dev);
875 const struct net_device *lowerdev = vlan->lowerdev;
876 struct macvlan_port *port = vlan->port;
877
878 dev->state = (dev->state & ~MACVLAN_STATE_MASK) |
879 (lowerdev->state & MACVLAN_STATE_MASK);
880 dev->features = lowerdev->features & MACVLAN_FEATURES;
881 dev->features |= ALWAYS_ON_FEATURES;
882 dev->hw_features |= NETIF_F_LRO;
883 dev->vlan_features = lowerdev->vlan_features & MACVLAN_FEATURES;
884 dev->vlan_features |= ALWAYS_ON_OFFLOADS;
885 dev->hw_enc_features |= dev->features;
886 dev->gso_max_size = lowerdev->gso_max_size;
887 dev->gso_max_segs = lowerdev->gso_max_segs;
888 dev->hard_header_len = lowerdev->hard_header_len;
889
890 macvlan_set_lockdep_class(dev);
891
892 vlan->pcpu_stats = netdev_alloc_pcpu_stats(struct vlan_pcpu_stats);
893 if (!vlan->pcpu_stats)
894 return -ENOMEM;
895
896 port->count += 1;
897
898 return 0;
899}
900
901static void macvlan_uninit(struct net_device *dev)
902{
903 struct macvlan_dev *vlan = netdev_priv(dev);
904 struct macvlan_port *port = vlan->port;
905
906 free_percpu(vlan->pcpu_stats);
907
908 macvlan_flush_sources(port, vlan);
909 port->count -= 1;
910 if (!port->count)
911 macvlan_port_destroy(port->dev);
912}
913
914static void macvlan_dev_get_stats64(struct net_device *dev,
915 struct rtnl_link_stats64 *stats)
916{
917 struct macvlan_dev *vlan = netdev_priv(dev);
918
919 if (vlan->pcpu_stats) {
920 struct vlan_pcpu_stats *p;
921 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
922 u32 rx_errors = 0, tx_dropped = 0;
923 unsigned int start;
924 int i;
925
926 for_each_possible_cpu(i) {
927 p = per_cpu_ptr(vlan->pcpu_stats, i);
928 do {
929 start = u64_stats_fetch_begin_irq(&p->syncp);
930 rx_packets = p->rx_packets;
931 rx_bytes = p->rx_bytes;
932 rx_multicast = p->rx_multicast;
933 tx_packets = p->tx_packets;
934 tx_bytes = p->tx_bytes;
935 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
936
937 stats->rx_packets += rx_packets;
938 stats->rx_bytes += rx_bytes;
939 stats->multicast += rx_multicast;
940 stats->tx_packets += tx_packets;
941 stats->tx_bytes += tx_bytes;
942 /* rx_errors & tx_dropped are u32, updated
943 * without syncp protection.
944 */
945 rx_errors += p->rx_errors;
946 tx_dropped += p->tx_dropped;
947 }
948 stats->rx_errors = rx_errors;
949 stats->rx_dropped = rx_errors;
950 stats->tx_dropped = tx_dropped;
951 }
952}
953
954static int macvlan_vlan_rx_add_vid(struct net_device *dev,
955 __be16 proto, u16 vid)
956{
957 struct macvlan_dev *vlan = netdev_priv(dev);
958 struct net_device *lowerdev = vlan->lowerdev;
959
960 return vlan_vid_add(lowerdev, proto, vid);
961}
962
963static int macvlan_vlan_rx_kill_vid(struct net_device *dev,
964 __be16 proto, u16 vid)
965{
966 struct macvlan_dev *vlan = netdev_priv(dev);
967 struct net_device *lowerdev = vlan->lowerdev;
968
969 vlan_vid_del(lowerdev, proto, vid);
970 return 0;
971}
972
973static int macvlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
974 struct net_device *dev,
975 const unsigned char *addr, u16 vid,
976 u16 flags)
977{
978 struct macvlan_dev *vlan = netdev_priv(dev);
979 int err = -EINVAL;
980
981 /* Support unicast filter only on passthru devices.
982 * Multicast filter should be allowed on all devices.
983 */
984 if (!macvlan_passthru(vlan->port) && is_unicast_ether_addr(addr))
985 return -EOPNOTSUPP;
986
987 if (flags & NLM_F_REPLACE)
988 return -EOPNOTSUPP;
989
990 if (is_unicast_ether_addr(addr))
991 err = dev_uc_add_excl(dev, addr);
992 else if (is_multicast_ether_addr(addr))
993 err = dev_mc_add_excl(dev, addr);
994
995 return err;
996}
997
998static int macvlan_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
999 struct net_device *dev,
1000 const unsigned char *addr, u16 vid)
1001{
1002 struct macvlan_dev *vlan = netdev_priv(dev);
1003 int err = -EINVAL;
1004
1005 /* Support unicast filter only on passthru devices.
1006 * Multicast filter should be allowed on all devices.
1007 */
1008 if (!macvlan_passthru(vlan->port) && is_unicast_ether_addr(addr))
1009 return -EOPNOTSUPP;
1010
1011 if (is_unicast_ether_addr(addr))
1012 err = dev_uc_del(dev, addr);
1013 else if (is_multicast_ether_addr(addr))
1014 err = dev_mc_del(dev, addr);
1015
1016 return err;
1017}
1018
1019static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
1020 struct ethtool_drvinfo *drvinfo)
1021{
1022 strlcpy(drvinfo->driver, "macvlan", sizeof(drvinfo->driver));
1023 strlcpy(drvinfo->version, "0.1", sizeof(drvinfo->version));
1024}
1025
1026static int macvlan_ethtool_get_link_ksettings(struct net_device *dev,
1027 struct ethtool_link_ksettings *cmd)
1028{
1029 const struct macvlan_dev *vlan = netdev_priv(dev);
1030
1031 return __ethtool_get_link_ksettings(vlan->lowerdev, cmd);
1032}
1033
1034static netdev_features_t macvlan_fix_features(struct net_device *dev,
1035 netdev_features_t features)
1036{
1037 struct macvlan_dev *vlan = netdev_priv(dev);
1038 netdev_features_t lowerdev_features = vlan->lowerdev->features;
1039 netdev_features_t mask;
1040
1041 features |= NETIF_F_ALL_FOR_ALL;
1042 features &= (vlan->set_features | ~MACVLAN_FEATURES);
1043 mask = features;
1044
1045 lowerdev_features &= (features | ~NETIF_F_LRO);
1046 features = netdev_increment_features(lowerdev_features, features, mask);
1047 features |= ALWAYS_ON_FEATURES;
1048 features &= (ALWAYS_ON_FEATURES | MACVLAN_FEATURES);
1049
1050 return features;
1051}
1052
1053#ifdef CONFIG_NET_POLL_CONTROLLER
1054static void macvlan_dev_poll_controller(struct net_device *dev)
1055{
1056 return;
1057}
1058
1059static int macvlan_dev_netpoll_setup(struct net_device *dev, struct netpoll_info *npinfo)
1060{
1061 struct macvlan_dev *vlan = netdev_priv(dev);
1062 struct net_device *real_dev = vlan->lowerdev;
1063 struct netpoll *netpoll;
1064 int err = 0;
1065
1066 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
1067 err = -ENOMEM;
1068 if (!netpoll)
1069 goto out;
1070
1071 err = __netpoll_setup(netpoll, real_dev);
1072 if (err) {
1073 kfree(netpoll);
1074 goto out;
1075 }
1076
1077 vlan->netpoll = netpoll;
1078
1079out:
1080 return err;
1081}
1082
1083static void macvlan_dev_netpoll_cleanup(struct net_device *dev)
1084{
1085 struct macvlan_dev *vlan = netdev_priv(dev);
1086 struct netpoll *netpoll = vlan->netpoll;
1087
1088 if (!netpoll)
1089 return;
1090
1091 vlan->netpoll = NULL;
1092
1093 __netpoll_free_async(netpoll);
1094}
1095#endif /* CONFIG_NET_POLL_CONTROLLER */
1096
1097static int macvlan_dev_get_iflink(const struct net_device *dev)
1098{
1099 struct macvlan_dev *vlan = netdev_priv(dev);
1100
1101 return vlan->lowerdev->ifindex;
1102}
1103
1104static const struct ethtool_ops macvlan_ethtool_ops = {
1105 .get_link = ethtool_op_get_link,
1106 .get_link_ksettings = macvlan_ethtool_get_link_ksettings,
1107 .get_drvinfo = macvlan_ethtool_get_drvinfo,
1108};
1109
1110static const struct net_device_ops macvlan_netdev_ops = {
1111 .ndo_init = macvlan_init,
1112 .ndo_uninit = macvlan_uninit,
1113 .ndo_open = macvlan_open,
1114 .ndo_stop = macvlan_stop,
1115 .ndo_start_xmit = macvlan_start_xmit,
1116 .ndo_change_mtu = macvlan_change_mtu,
1117 .ndo_fix_features = macvlan_fix_features,
1118 .ndo_change_rx_flags = macvlan_change_rx_flags,
1119 .ndo_set_mac_address = macvlan_set_mac_address,
1120 .ndo_set_rx_mode = macvlan_set_mac_lists,
1121 .ndo_get_stats64 = macvlan_dev_get_stats64,
1122 .ndo_validate_addr = eth_validate_addr,
1123 .ndo_vlan_rx_add_vid = macvlan_vlan_rx_add_vid,
1124 .ndo_vlan_rx_kill_vid = macvlan_vlan_rx_kill_vid,
1125 .ndo_fdb_add = macvlan_fdb_add,
1126 .ndo_fdb_del = macvlan_fdb_del,
1127 .ndo_fdb_dump = ndo_dflt_fdb_dump,
1128 .ndo_get_lock_subclass = macvlan_get_nest_level,
1129#ifdef CONFIG_NET_POLL_CONTROLLER
1130 .ndo_poll_controller = macvlan_dev_poll_controller,
1131 .ndo_netpoll_setup = macvlan_dev_netpoll_setup,
1132 .ndo_netpoll_cleanup = macvlan_dev_netpoll_cleanup,
1133#endif
1134 .ndo_get_iflink = macvlan_dev_get_iflink,
1135 .ndo_features_check = passthru_features_check,
1136};
1137
1138void macvlan_common_setup(struct net_device *dev)
1139{
1140 ether_setup(dev);
1141
1142 dev->min_mtu = 0;
1143 dev->max_mtu = ETH_MAX_MTU;
1144 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1145 netif_keep_dst(dev);
1146 dev->priv_flags |= IFF_UNICAST_FLT;
1147 dev->netdev_ops = &macvlan_netdev_ops;
1148 dev->needs_free_netdev = true;
1149 dev->header_ops = &macvlan_hard_header_ops;
1150 dev->ethtool_ops = &macvlan_ethtool_ops;
1151}
1152EXPORT_SYMBOL_GPL(macvlan_common_setup);
1153
1154static void macvlan_setup(struct net_device *dev)
1155{
1156 macvlan_common_setup(dev);
1157 dev->priv_flags |= IFF_NO_QUEUE;
1158}
1159
1160static int macvlan_port_create(struct net_device *dev)
1161{
1162 struct macvlan_port *port;
1163 unsigned int i;
1164 int err;
1165
1166 if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
1167 return -EINVAL;
1168
1169 if (netdev_is_rx_handler_busy(dev))
1170 return -EBUSY;
1171
1172 port = kzalloc(sizeof(*port), GFP_KERNEL);
1173 if (port == NULL)
1174 return -ENOMEM;
1175
1176 port->dev = dev;
1177 ether_addr_copy(port->perm_addr, dev->dev_addr);
1178 INIT_LIST_HEAD(&port->vlans);
1179 for (i = 0; i < MACVLAN_HASH_SIZE; i++)
1180 INIT_HLIST_HEAD(&port->vlan_hash[i]);
1181 for (i = 0; i < MACVLAN_HASH_SIZE; i++)
1182 INIT_HLIST_HEAD(&port->vlan_source_hash[i]);
1183
1184 skb_queue_head_init(&port->bc_queue);
1185 INIT_WORK(&port->bc_work, macvlan_process_broadcast);
1186
1187 err = netdev_rx_handler_register(dev, macvlan_handle_frame, port);
1188 if (err)
1189 kfree(port);
1190 else
1191 dev->priv_flags |= IFF_MACVLAN_PORT;
1192 return err;
1193}
1194
1195static void macvlan_port_destroy(struct net_device *dev)
1196{
1197 struct macvlan_port *port = macvlan_port_get_rtnl(dev);
1198 struct sk_buff *skb;
1199
1200 dev->priv_flags &= ~IFF_MACVLAN_PORT;
1201 netdev_rx_handler_unregister(dev);
1202
1203 /* After this point, no packet can schedule bc_work anymore,
1204 * but we need to cancel it and purge left skbs if any.
1205 */
1206 cancel_work_sync(&port->bc_work);
1207
1208 while ((skb = __skb_dequeue(&port->bc_queue))) {
1209 const struct macvlan_dev *src = MACVLAN_SKB_CB(skb)->src;
1210
1211 if (src)
1212 dev_put(src->dev);
1213
1214 kfree_skb(skb);
1215 }
1216
1217 /* If the lower device address has been changed by passthru
1218 * macvlan, put it back.
1219 */
1220 if (macvlan_passthru(port) &&
1221 !ether_addr_equal(port->dev->dev_addr, port->perm_addr)) {
1222 struct sockaddr sa;
1223
1224 sa.sa_family = port->dev->type;
1225 memcpy(&sa.sa_data, port->perm_addr, port->dev->addr_len);
1226 dev_set_mac_address(port->dev, &sa);
1227 }
1228
1229 kfree(port);
1230}
1231
1232static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[],
1233 struct netlink_ext_ack *extack)
1234{
1235 if (tb[IFLA_ADDRESS]) {
1236 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1237 return -EINVAL;
1238 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1239 return -EADDRNOTAVAIL;
1240 }
1241
1242 if (data && data[IFLA_MACVLAN_FLAGS] &&
1243 nla_get_u16(data[IFLA_MACVLAN_FLAGS]) & ~MACVLAN_FLAG_NOPROMISC)
1244 return -EINVAL;
1245
1246 if (data && data[IFLA_MACVLAN_MODE]) {
1247 switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) {
1248 case MACVLAN_MODE_PRIVATE:
1249 case MACVLAN_MODE_VEPA:
1250 case MACVLAN_MODE_BRIDGE:
1251 case MACVLAN_MODE_PASSTHRU:
1252 case MACVLAN_MODE_SOURCE:
1253 break;
1254 default:
1255 return -EINVAL;
1256 }
1257 }
1258
1259 if (data && data[IFLA_MACVLAN_MACADDR_MODE]) {
1260 switch (nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE])) {
1261 case MACVLAN_MACADDR_ADD:
1262 case MACVLAN_MACADDR_DEL:
1263 case MACVLAN_MACADDR_FLUSH:
1264 case MACVLAN_MACADDR_SET:
1265 break;
1266 default:
1267 return -EINVAL;
1268 }
1269 }
1270
1271 if (data && data[IFLA_MACVLAN_MACADDR]) {
1272 if (nla_len(data[IFLA_MACVLAN_MACADDR]) != ETH_ALEN)
1273 return -EINVAL;
1274
1275 if (!is_valid_ether_addr(nla_data(data[IFLA_MACVLAN_MACADDR])))
1276 return -EADDRNOTAVAIL;
1277 }
1278
1279 if (data && data[IFLA_MACVLAN_MACADDR_COUNT])
1280 return -EINVAL;
1281
1282 return 0;
1283}
1284
1285/**
1286 * reconfigure list of remote source mac address
1287 * (only for macvlan devices in source mode)
1288 * Note regarding alignment: all netlink data is aligned to 4 Byte, which
1289 * suffices for both ether_addr_copy and ether_addr_equal_64bits usage.
1290 */
1291static int macvlan_changelink_sources(struct macvlan_dev *vlan, u32 mode,
1292 struct nlattr *data[])
1293{
1294 char *addr = NULL;
1295 int ret, rem, len;
1296 struct nlattr *nla, *head;
1297 struct macvlan_source_entry *entry;
1298
1299 if (data[IFLA_MACVLAN_MACADDR])
1300 addr = nla_data(data[IFLA_MACVLAN_MACADDR]);
1301
1302 if (mode == MACVLAN_MACADDR_ADD) {
1303 if (!addr)
1304 return -EINVAL;
1305
1306 return macvlan_hash_add_source(vlan, addr);
1307
1308 } else if (mode == MACVLAN_MACADDR_DEL) {
1309 if (!addr)
1310 return -EINVAL;
1311
1312 entry = macvlan_hash_lookup_source(vlan, addr);
1313 if (entry) {
1314 macvlan_hash_del_source(entry);
1315 vlan->macaddr_count--;
1316 }
1317 } else if (mode == MACVLAN_MACADDR_FLUSH) {
1318 macvlan_flush_sources(vlan->port, vlan);
1319 } else if (mode == MACVLAN_MACADDR_SET) {
1320 macvlan_flush_sources(vlan->port, vlan);
1321
1322 if (addr) {
1323 ret = macvlan_hash_add_source(vlan, addr);
1324 if (ret)
1325 return ret;
1326 }
1327
1328 if (!data || !data[IFLA_MACVLAN_MACADDR_DATA])
1329 return 0;
1330
1331 head = nla_data(data[IFLA_MACVLAN_MACADDR_DATA]);
1332 len = nla_len(data[IFLA_MACVLAN_MACADDR_DATA]);
1333
1334 nla_for_each_attr(nla, head, len, rem) {
1335 if (nla_type(nla) != IFLA_MACVLAN_MACADDR ||
1336 nla_len(nla) != ETH_ALEN)
1337 continue;
1338
1339 addr = nla_data(nla);
1340 ret = macvlan_hash_add_source(vlan, addr);
1341 if (ret)
1342 return ret;
1343 }
1344 } else {
1345 return -EINVAL;
1346 }
1347
1348 return 0;
1349}
1350
1351int macvlan_common_newlink(struct net *src_net, struct net_device *dev,
1352 struct nlattr *tb[], struct nlattr *data[])
1353{
1354 struct macvlan_dev *vlan = netdev_priv(dev);
1355 struct macvlan_port *port;
1356 struct net_device *lowerdev;
1357 int err;
1358 int macmode;
1359 bool create = false;
1360
1361 if (!tb[IFLA_LINK])
1362 return -EINVAL;
1363
1364 lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
1365 if (lowerdev == NULL)
1366 return -ENODEV;
1367
1368 /* When creating macvlans or macvtaps on top of other macvlans - use
1369 * the real device as the lowerdev.
1370 */
1371 if (netif_is_macvlan(lowerdev))
1372 lowerdev = macvlan_dev_real_dev(lowerdev);
1373
1374 if (!tb[IFLA_MTU])
1375 dev->mtu = lowerdev->mtu;
1376 else if (dev->mtu > lowerdev->mtu)
1377 return -EINVAL;
1378
1379 /* MTU range: 68 - lowerdev->max_mtu */
1380 dev->min_mtu = ETH_MIN_MTU;
1381 dev->max_mtu = lowerdev->max_mtu;
1382
1383 if (!tb[IFLA_ADDRESS])
1384 eth_hw_addr_random(dev);
1385
1386 if (!macvlan_port_exists(lowerdev)) {
1387 err = macvlan_port_create(lowerdev);
1388 if (err < 0)
1389 return err;
1390 create = true;
1391 }
1392 port = macvlan_port_get_rtnl(lowerdev);
1393
1394 /* Only 1 macvlan device can be created in passthru mode */
1395 if (macvlan_passthru(port)) {
1396 /* The macvlan port must be not created this time,
1397 * still goto destroy_macvlan_port for readability.
1398 */
1399 err = -EINVAL;
1400 goto destroy_macvlan_port;
1401 }
1402
1403 vlan->lowerdev = lowerdev;
1404 vlan->dev = dev;
1405 vlan->port = port;
1406 vlan->set_features = MACVLAN_FEATURES;
1407 vlan->nest_level = dev_get_nest_level(lowerdev) + 1;
1408
1409 vlan->mode = MACVLAN_MODE_VEPA;
1410 if (data && data[IFLA_MACVLAN_MODE])
1411 vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
1412
1413 if (data && data[IFLA_MACVLAN_FLAGS])
1414 vlan->flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
1415
1416 if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
1417 if (port->count) {
1418 err = -EINVAL;
1419 goto destroy_macvlan_port;
1420 }
1421 macvlan_set_passthru(port);
1422 eth_hw_addr_inherit(dev, lowerdev);
1423 }
1424
1425 if (data && data[IFLA_MACVLAN_MACADDR_MODE]) {
1426 if (vlan->mode != MACVLAN_MODE_SOURCE) {
1427 err = -EINVAL;
1428 goto destroy_macvlan_port;
1429 }
1430 macmode = nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE]);
1431 err = macvlan_changelink_sources(vlan, macmode, data);
1432 if (err)
1433 goto destroy_macvlan_port;
1434 }
1435
1436 err = register_netdevice(dev);
1437 if (err < 0)
1438 goto destroy_macvlan_port;
1439
1440 dev->priv_flags |= IFF_MACVLAN;
1441 err = netdev_upper_dev_link(lowerdev, dev);
1442 if (err)
1443 goto unregister_netdev;
1444
1445 list_add_tail_rcu(&vlan->list, &port->vlans);
1446 netif_stacked_transfer_operstate(lowerdev, dev);
1447 linkwatch_fire_event(dev);
1448
1449 return 0;
1450
1451unregister_netdev:
1452 /* macvlan_uninit would free the macvlan port */
1453 unregister_netdevice(dev);
1454 return err;
1455destroy_macvlan_port:
1456 /* the macvlan port may be freed by macvlan_uninit when fail to register.
1457 * so we destroy the macvlan port only when it's valid.
1458 */
1459 if (create && macvlan_port_get_rtnl(lowerdev))
1460 macvlan_port_destroy(port->dev);
1461 return err;
1462}
1463EXPORT_SYMBOL_GPL(macvlan_common_newlink);
1464
1465static int macvlan_newlink(struct net *src_net, struct net_device *dev,
1466 struct nlattr *tb[], struct nlattr *data[],
1467 struct netlink_ext_ack *extack)
1468{
1469 return macvlan_common_newlink(src_net, dev, tb, data);
1470}
1471
1472void macvlan_dellink(struct net_device *dev, struct list_head *head)
1473{
1474 struct macvlan_dev *vlan = netdev_priv(dev);
1475
1476 if (vlan->mode == MACVLAN_MODE_SOURCE)
1477 macvlan_flush_sources(vlan->port, vlan);
1478 list_del_rcu(&vlan->list);
1479 unregister_netdevice_queue(dev, head);
1480 netdev_upper_dev_unlink(vlan->lowerdev, dev);
1481}
1482EXPORT_SYMBOL_GPL(macvlan_dellink);
1483
1484static int macvlan_changelink(struct net_device *dev,
1485 struct nlattr *tb[], struct nlattr *data[],
1486 struct netlink_ext_ack *extack)
1487{
1488 struct macvlan_dev *vlan = netdev_priv(dev);
1489 enum macvlan_mode mode;
1490 bool set_mode = false;
1491 enum macvlan_macaddr_mode macmode;
1492 int ret;
1493
1494 /* Validate mode, but don't set yet: setting flags may fail. */
1495 if (data && data[IFLA_MACVLAN_MODE]) {
1496 set_mode = true;
1497 mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
1498 /* Passthrough mode can't be set or cleared dynamically */
1499 if ((mode == MACVLAN_MODE_PASSTHRU) !=
1500 (vlan->mode == MACVLAN_MODE_PASSTHRU))
1501 return -EINVAL;
1502 if (vlan->mode == MACVLAN_MODE_SOURCE &&
1503 vlan->mode != mode)
1504 macvlan_flush_sources(vlan->port, vlan);
1505 }
1506
1507 if (data && data[IFLA_MACVLAN_FLAGS]) {
1508 __u16 flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
1509 bool promisc = (flags ^ vlan->flags) & MACVLAN_FLAG_NOPROMISC;
1510 if (macvlan_passthru(vlan->port) && promisc) {
1511 int err;
1512
1513 if (flags & MACVLAN_FLAG_NOPROMISC)
1514 err = dev_set_promiscuity(vlan->lowerdev, -1);
1515 else
1516 err = dev_set_promiscuity(vlan->lowerdev, 1);
1517 if (err < 0)
1518 return err;
1519 }
1520 vlan->flags = flags;
1521 }
1522 if (set_mode)
1523 vlan->mode = mode;
1524 if (data && data[IFLA_MACVLAN_MACADDR_MODE]) {
1525 if (vlan->mode != MACVLAN_MODE_SOURCE)
1526 return -EINVAL;
1527 macmode = nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE]);
1528 ret = macvlan_changelink_sources(vlan, macmode, data);
1529 if (ret)
1530 return ret;
1531 }
1532 return 0;
1533}
1534
1535static size_t macvlan_get_size_mac(const struct macvlan_dev *vlan)
1536{
1537 if (vlan->macaddr_count == 0)
1538 return 0;
1539 return nla_total_size(0) /* IFLA_MACVLAN_MACADDR_DATA */
1540 + vlan->macaddr_count * nla_total_size(sizeof(u8) * ETH_ALEN);
1541}
1542
1543static size_t macvlan_get_size(const struct net_device *dev)
1544{
1545 struct macvlan_dev *vlan = netdev_priv(dev);
1546
1547 return (0
1548 + nla_total_size(4) /* IFLA_MACVLAN_MODE */
1549 + nla_total_size(2) /* IFLA_MACVLAN_FLAGS */
1550 + nla_total_size(4) /* IFLA_MACVLAN_MACADDR_COUNT */
1551 + macvlan_get_size_mac(vlan) /* IFLA_MACVLAN_MACADDR */
1552 );
1553}
1554
1555static int macvlan_fill_info_macaddr(struct sk_buff *skb,
1556 const struct macvlan_dev *vlan,
1557 const int i)
1558{
1559 struct hlist_head *h = &vlan->port->vlan_source_hash[i];
1560 struct macvlan_source_entry *entry;
1561
1562 hlist_for_each_entry_rcu(entry, h, hlist) {
1563 if (entry->vlan != vlan)
1564 continue;
1565 if (nla_put(skb, IFLA_MACVLAN_MACADDR, ETH_ALEN, entry->addr))
1566 return 1;
1567 }
1568 return 0;
1569}
1570
1571static int macvlan_fill_info(struct sk_buff *skb,
1572 const struct net_device *dev)
1573{
1574 struct macvlan_dev *vlan = netdev_priv(dev);
1575 int i;
1576 struct nlattr *nest;
1577
1578 if (nla_put_u32(skb, IFLA_MACVLAN_MODE, vlan->mode))
1579 goto nla_put_failure;
1580 if (nla_put_u16(skb, IFLA_MACVLAN_FLAGS, vlan->flags))
1581 goto nla_put_failure;
1582 if (nla_put_u32(skb, IFLA_MACVLAN_MACADDR_COUNT, vlan->macaddr_count))
1583 goto nla_put_failure;
1584 if (vlan->macaddr_count > 0) {
1585 nest = nla_nest_start(skb, IFLA_MACVLAN_MACADDR_DATA);
1586 if (nest == NULL)
1587 goto nla_put_failure;
1588
1589 for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
1590 if (macvlan_fill_info_macaddr(skb, vlan, i))
1591 goto nla_put_failure;
1592 }
1593 nla_nest_end(skb, nest);
1594 }
1595 return 0;
1596
1597nla_put_failure:
1598 return -EMSGSIZE;
1599}
1600
1601static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = {
1602 [IFLA_MACVLAN_MODE] = { .type = NLA_U32 },
1603 [IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 },
1604 [IFLA_MACVLAN_MACADDR_MODE] = { .type = NLA_U32 },
1605 [IFLA_MACVLAN_MACADDR] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1606 [IFLA_MACVLAN_MACADDR_DATA] = { .type = NLA_NESTED },
1607 [IFLA_MACVLAN_MACADDR_COUNT] = { .type = NLA_U32 },
1608};
1609
1610int macvlan_link_register(struct rtnl_link_ops *ops)
1611{
1612 /* common fields */
1613 ops->validate = macvlan_validate;
1614 ops->maxtype = IFLA_MACVLAN_MAX;
1615 ops->policy = macvlan_policy;
1616 ops->changelink = macvlan_changelink;
1617 ops->get_size = macvlan_get_size;
1618 ops->fill_info = macvlan_fill_info;
1619
1620 return rtnl_link_register(ops);
1621};
1622EXPORT_SYMBOL_GPL(macvlan_link_register);
1623
1624static struct net *macvlan_get_link_net(const struct net_device *dev)
1625{
1626 return dev_net(macvlan_dev_real_dev(dev));
1627}
1628
1629static struct rtnl_link_ops macvlan_link_ops = {
1630 .kind = "macvlan",
1631 .setup = macvlan_setup,
1632 .newlink = macvlan_newlink,
1633 .dellink = macvlan_dellink,
1634 .get_link_net = macvlan_get_link_net,
1635 .priv_size = sizeof(struct macvlan_dev),
1636};
1637
1638static int macvlan_device_event(struct notifier_block *unused,
1639 unsigned long event, void *ptr)
1640{
1641 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1642 struct macvlan_dev *vlan, *next;
1643 struct macvlan_port *port;
1644 LIST_HEAD(list_kill);
1645
1646 if (!macvlan_port_exists(dev))
1647 return NOTIFY_DONE;
1648
1649 port = macvlan_port_get_rtnl(dev);
1650
1651 switch (event) {
1652 case NETDEV_UP:
1653 case NETDEV_CHANGE:
1654 list_for_each_entry(vlan, &port->vlans, list)
1655 netif_stacked_transfer_operstate(vlan->lowerdev,
1656 vlan->dev);
1657 break;
1658 case NETDEV_FEAT_CHANGE:
1659 list_for_each_entry(vlan, &port->vlans, list) {
1660 vlan->dev->gso_max_size = dev->gso_max_size;
1661 vlan->dev->gso_max_segs = dev->gso_max_segs;
1662 netdev_update_features(vlan->dev);
1663 }
1664 break;
1665 case NETDEV_CHANGEMTU:
1666 list_for_each_entry(vlan, &port->vlans, list) {
1667 if (vlan->dev->mtu <= dev->mtu)
1668 continue;
1669 dev_set_mtu(vlan->dev, dev->mtu);
1670 }
1671 break;
1672 case NETDEV_CHANGEADDR:
1673 if (!macvlan_passthru(port))
1674 return NOTIFY_DONE;
1675
1676 vlan = list_first_entry_or_null(&port->vlans,
1677 struct macvlan_dev,
1678 list);
1679
1680 if (vlan && macvlan_sync_address(vlan->dev, dev->dev_addr))
1681 return NOTIFY_BAD;
1682
1683 break;
1684 case NETDEV_UNREGISTER:
1685 /* twiddle thumbs on netns device moves */
1686 if (dev->reg_state != NETREG_UNREGISTERING)
1687 break;
1688
1689 list_for_each_entry_safe(vlan, next, &port->vlans, list)
1690 vlan->dev->rtnl_link_ops->dellink(vlan->dev, &list_kill);
1691 unregister_netdevice_many(&list_kill);
1692 break;
1693 case NETDEV_PRE_TYPE_CHANGE:
1694 /* Forbid underlaying device to change its type. */
1695 return NOTIFY_BAD;
1696
1697 case NETDEV_NOTIFY_PEERS:
1698 case NETDEV_BONDING_FAILOVER:
1699 case NETDEV_RESEND_IGMP:
1700 /* Propagate to all vlans */
1701 list_for_each_entry(vlan, &port->vlans, list)
1702 call_netdevice_notifiers(event, vlan->dev);
1703 }
1704 return NOTIFY_DONE;
1705}
1706
1707static struct notifier_block macvlan_notifier_block __read_mostly = {
1708 .notifier_call = macvlan_device_event,
1709};
1710
1711static int __init macvlan_init_module(void)
1712{
1713 int err;
1714
1715 register_netdevice_notifier(&macvlan_notifier_block);
1716
1717 err = macvlan_link_register(&macvlan_link_ops);
1718 if (err < 0)
1719 goto err1;
1720 return 0;
1721err1:
1722 unregister_netdevice_notifier(&macvlan_notifier_block);
1723 return err;
1724}
1725
1726static void __exit macvlan_cleanup_module(void)
1727{
1728 rtnl_link_unregister(&macvlan_link_ops);
1729 unregister_netdevice_notifier(&macvlan_notifier_block);
1730}
1731
1732module_init(macvlan_init_module);
1733module_exit(macvlan_cleanup_module);
1734
1735MODULE_LICENSE("GPL");
1736MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
1737MODULE_DESCRIPTION("Driver for MAC address based VLANs");
1738MODULE_ALIAS_RTNL_LINK("macvlan");