1
2
3
4
5
6
7
8
9
10
11
12
13
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
37#define MACVLAN_HASH_SIZE (1 << BITS_PER_BYTE)
38#define MACVLAN_BC_QUEUE_LEN 1000
39
40struct macvlan_port {
41 struct net_device *dev;
42 struct hlist_head vlan_hash[MACVLAN_HASH_SIZE];
43 struct list_head vlans;
44 struct rcu_head rcu;
45 struct sk_buff_head bc_queue;
46 struct work_struct bc_work;
47 bool passthru;
48 int count;
49};
50
51struct macvlan_skb_cb {
52 const struct macvlan_dev *src;
53};
54
55#define MACVLAN_SKB_CB(__skb) ((struct macvlan_skb_cb *)&((__skb)->cb[0]))
56
57static void macvlan_port_destroy(struct net_device *dev);
58
59static struct macvlan_port *macvlan_port_get_rcu(const struct net_device *dev)
60{
61 return rcu_dereference(dev->rx_handler_data);
62}
63
64static struct macvlan_port *macvlan_port_get_rtnl(const struct net_device *dev)
65{
66 return rtnl_dereference(dev->rx_handler_data);
67}
68
69#define macvlan_port_exists(dev) (dev->priv_flags & IFF_MACVLAN_PORT)
70
71static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
72 const unsigned char *addr)
73{
74 struct macvlan_dev *vlan;
75
76 hlist_for_each_entry_rcu(vlan, &port->vlan_hash[addr[5]], hlist) {
77 if (ether_addr_equal_64bits(vlan->dev->dev_addr, addr))
78 return vlan;
79 }
80 return NULL;
81}
82
83static void macvlan_hash_add(struct macvlan_dev *vlan)
84{
85 struct macvlan_port *port = vlan->port;
86 const unsigned char *addr = vlan->dev->dev_addr;
87
88 hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]);
89}
90
91static void macvlan_hash_del(struct macvlan_dev *vlan, bool sync)
92{
93 hlist_del_rcu(&vlan->hlist);
94 if (sync)
95 synchronize_rcu();
96}
97
98static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
99 const unsigned char *addr)
100{
101 macvlan_hash_del(vlan, true);
102
103
104
105 memcpy(vlan->dev->dev_addr, addr, ETH_ALEN);
106 macvlan_hash_add(vlan);
107}
108
109static int macvlan_addr_busy(const struct macvlan_port *port,
110 const unsigned char *addr)
111{
112
113
114
115
116 if (ether_addr_equal_64bits(port->dev->dev_addr, addr))
117 return 1;
118
119 if (macvlan_hash_lookup(port, addr))
120 return 1;
121
122 return 0;
123}
124
125
126static int macvlan_broadcast_one(struct sk_buff *skb,
127 const struct macvlan_dev *vlan,
128 const struct ethhdr *eth, bool local)
129{
130 struct net_device *dev = vlan->dev;
131 if (!skb)
132 return NET_RX_DROP;
133
134 if (local)
135 return __dev_forward_skb(dev, skb);
136
137 skb->dev = dev;
138 if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast))
139 skb->pkt_type = PACKET_BROADCAST;
140 else
141 skb->pkt_type = PACKET_MULTICAST;
142
143 return 0;
144}
145
146static u32 macvlan_hash_mix(const struct macvlan_dev *vlan)
147{
148 return (u32)(((unsigned long)vlan) >> L1_CACHE_SHIFT);
149}
150
151
152static unsigned int mc_hash(const struct macvlan_dev *vlan,
153 const unsigned char *addr)
154{
155 u32 val = __get_unaligned_cpu32(addr + 2);
156
157 val ^= macvlan_hash_mix(vlan);
158 return hash_32(val, MACVLAN_MC_FILTER_BITS);
159}
160
161static void macvlan_broadcast(struct sk_buff *skb,
162 const struct macvlan_port *port,
163 struct net_device *src,
164 enum macvlan_mode mode)
165{
166 const struct ethhdr *eth = eth_hdr(skb);
167 const struct macvlan_dev *vlan;
168 struct sk_buff *nskb;
169 unsigned int i;
170 int err;
171 unsigned int hash;
172
173 if (skb->protocol == htons(ETH_P_PAUSE))
174 return;
175
176 for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
177 hlist_for_each_entry_rcu(vlan, &port->vlan_hash[i], hlist) {
178 if (vlan->dev == src || !(vlan->mode & mode))
179 continue;
180
181 hash = mc_hash(vlan, eth->h_dest);
182 if (!test_bit(hash, vlan->mc_filter))
183 continue;
184 nskb = skb_clone(skb, GFP_ATOMIC);
185 err = macvlan_broadcast_one(
186 nskb, vlan, eth,
187 mode == MACVLAN_MODE_BRIDGE) ?:
188 netif_rx_ni(nskb);
189 macvlan_count_rx(vlan, skb->len + ETH_HLEN,
190 err == NET_RX_SUCCESS, true);
191 }
192 }
193}
194
195static void macvlan_process_broadcast(struct work_struct *w)
196{
197 struct macvlan_port *port = container_of(w, struct macvlan_port,
198 bc_work);
199 struct sk_buff *skb;
200 struct sk_buff_head list;
201
202 __skb_queue_head_init(&list);
203
204 spin_lock_bh(&port->bc_queue.lock);
205 skb_queue_splice_tail_init(&port->bc_queue, &list);
206 spin_unlock_bh(&port->bc_queue.lock);
207
208 while ((skb = __skb_dequeue(&list))) {
209 const struct macvlan_dev *src = MACVLAN_SKB_CB(skb)->src;
210
211 rcu_read_lock();
212
213 if (!src)
214
215 macvlan_broadcast(skb, port, NULL,
216 MACVLAN_MODE_PRIVATE |
217 MACVLAN_MODE_VEPA |
218 MACVLAN_MODE_PASSTHRU|
219 MACVLAN_MODE_BRIDGE);
220 else if (src->mode == MACVLAN_MODE_VEPA)
221
222 macvlan_broadcast(skb, port, src->dev,
223 MACVLAN_MODE_VEPA |
224 MACVLAN_MODE_BRIDGE);
225 else
226
227
228
229
230 macvlan_broadcast(skb, port, src->dev,
231 MACVLAN_MODE_VEPA);
232
233 rcu_read_unlock();
234
235 kfree_skb(skb);
236 }
237}
238
239static void macvlan_broadcast_enqueue(struct macvlan_port *port,
240 struct sk_buff *skb)
241{
242 struct sk_buff *nskb;
243 int err = -ENOMEM;
244
245 nskb = skb_clone(skb, GFP_ATOMIC);
246 if (!nskb)
247 goto err;
248
249 spin_lock(&port->bc_queue.lock);
250 if (skb_queue_len(&port->bc_queue) < MACVLAN_BC_QUEUE_LEN) {
251 __skb_queue_tail(&port->bc_queue, nskb);
252 err = 0;
253 }
254 spin_unlock(&port->bc_queue.lock);
255
256 if (err)
257 goto free_nskb;
258
259 schedule_work(&port->bc_work);
260 return;
261
262free_nskb:
263 kfree_skb(nskb);
264err:
265 atomic_long_inc(&skb->dev->rx_dropped);
266}
267
268
269static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb)
270{
271 struct macvlan_port *port;
272 struct sk_buff *skb = *pskb;
273 const struct ethhdr *eth = eth_hdr(skb);
274 const struct macvlan_dev *vlan;
275 const struct macvlan_dev *src;
276 struct net_device *dev;
277 unsigned int len = 0;
278 int ret;
279 rx_handler_result_t handle_res;
280
281 port = macvlan_port_get_rcu(skb->dev);
282 if (is_multicast_ether_addr(eth->h_dest)) {
283 skb = ip_check_defrag(skb, IP_DEFRAG_MACVLAN);
284 if (!skb)
285 return RX_HANDLER_CONSUMED;
286 *pskb = skb;
287 eth = eth_hdr(skb);
288 src = macvlan_hash_lookup(port, eth->h_source);
289 if (src && src->mode != MACVLAN_MODE_VEPA &&
290 src->mode != MACVLAN_MODE_BRIDGE) {
291
292 vlan = src;
293 ret = macvlan_broadcast_one(skb, vlan, eth, 0) ?:
294 netif_rx(skb);
295 handle_res = RX_HANDLER_CONSUMED;
296 goto out;
297 }
298
299 MACVLAN_SKB_CB(skb)->src = src;
300 macvlan_broadcast_enqueue(port, skb);
301
302 return RX_HANDLER_PASS;
303 }
304
305 if (port->passthru)
306 vlan = list_first_or_null_rcu(&port->vlans,
307 struct macvlan_dev, list);
308 else
309 vlan = macvlan_hash_lookup(port, eth->h_dest);
310 if (vlan == NULL)
311 return RX_HANDLER_PASS;
312
313 dev = vlan->dev;
314 if (unlikely(!(dev->flags & IFF_UP))) {
315 kfree_skb(skb);
316 return RX_HANDLER_CONSUMED;
317 }
318 len = skb->len + ETH_HLEN;
319 skb = skb_share_check(skb, GFP_ATOMIC);
320 if (!skb) {
321 ret = NET_RX_DROP;
322 handle_res = RX_HANDLER_CONSUMED;
323 goto out;
324 }
325
326 *pskb = skb;
327 skb->dev = dev;
328 skb->pkt_type = PACKET_HOST;
329
330 ret = NET_RX_SUCCESS;
331 handle_res = RX_HANDLER_ANOTHER;
332out:
333 macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, false);
334 return handle_res;
335}
336
337static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
338{
339 const struct macvlan_dev *vlan = netdev_priv(dev);
340 const struct macvlan_port *port = vlan->port;
341 const struct macvlan_dev *dest;
342
343 if (vlan->mode == MACVLAN_MODE_BRIDGE) {
344 const struct ethhdr *eth = (void *)skb->data;
345
346
347 if (is_multicast_ether_addr(eth->h_dest)) {
348 macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE);
349 goto xmit_world;
350 }
351
352 dest = macvlan_hash_lookup(port, eth->h_dest);
353 if (dest && dest->mode == MACVLAN_MODE_BRIDGE) {
354
355 dev_forward_skb(vlan->lowerdev, skb);
356
357 return NET_XMIT_SUCCESS;
358 }
359 }
360
361xmit_world:
362 skb->dev = vlan->lowerdev;
363 return dev_queue_xmit(skb);
364}
365
366netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
367 struct net_device *dev)
368{
369 unsigned int len = skb->len;
370 int ret;
371 const struct macvlan_dev *vlan = netdev_priv(dev);
372
373 if (vlan->fwd_priv) {
374 skb->dev = vlan->lowerdev;
375 ret = dev_queue_xmit_accel(skb, vlan->fwd_priv);
376 } else {
377 ret = macvlan_queue_xmit(skb, dev);
378 }
379
380 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
381 struct macvlan_pcpu_stats *pcpu_stats;
382
383 pcpu_stats = this_cpu_ptr(vlan->pcpu_stats);
384 u64_stats_update_begin(&pcpu_stats->syncp);
385 pcpu_stats->tx_packets++;
386 pcpu_stats->tx_bytes += len;
387 u64_stats_update_end(&pcpu_stats->syncp);
388 } else {
389 this_cpu_inc(vlan->pcpu_stats->tx_dropped);
390 }
391 return ret;
392}
393EXPORT_SYMBOL_GPL(macvlan_start_xmit);
394
395static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
396 unsigned short type, const void *daddr,
397 const void *saddr, unsigned len)
398{
399 const struct macvlan_dev *vlan = netdev_priv(dev);
400 struct net_device *lowerdev = vlan->lowerdev;
401
402 return dev_hard_header(skb, lowerdev, type, daddr,
403 saddr ? : dev->dev_addr, len);
404}
405
406static const struct header_ops macvlan_hard_header_ops = {
407 .create = macvlan_hard_header,
408 .rebuild = eth_rebuild_header,
409 .parse = eth_header_parse,
410 .cache = eth_header_cache,
411 .cache_update = eth_header_cache_update,
412};
413
414static int macvlan_open(struct net_device *dev)
415{
416 struct macvlan_dev *vlan = netdev_priv(dev);
417 struct net_device *lowerdev = vlan->lowerdev;
418 int err;
419
420 if (vlan->port->passthru) {
421 if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC))
422 dev_set_promiscuity(lowerdev, 1);
423 goto hash_add;
424 }
425
426 if (lowerdev->features & NETIF_F_HW_L2FW_DOFFLOAD) {
427 vlan->fwd_priv =
428 get_ndo_ext(lowerdev->netdev_ops, ndo_dfwd_add_station)(lowerdev, dev);
429
430
431
432
433 if (IS_ERR_OR_NULL(vlan->fwd_priv)) {
434 vlan->fwd_priv = NULL;
435 } else
436 return 0;
437 }
438
439 err = -EBUSY;
440 if (macvlan_addr_busy(vlan->port, dev->dev_addr))
441 goto out;
442
443 err = dev_uc_add(lowerdev, dev->dev_addr);
444 if (err < 0)
445 goto out;
446 if (dev->flags & IFF_ALLMULTI) {
447 err = dev_set_allmulti(lowerdev, 1);
448 if (err < 0)
449 goto del_unicast;
450 }
451
452hash_add:
453 macvlan_hash_add(vlan);
454 return 0;
455
456del_unicast:
457 dev_uc_del(lowerdev, dev->dev_addr);
458out:
459 if (vlan->fwd_priv) {
460 get_ndo_ext(lowerdev->netdev_ops, ndo_dfwd_del_station)(lowerdev,
461 vlan->fwd_priv);
462 vlan->fwd_priv = NULL;
463 }
464 return err;
465}
466
467static int macvlan_stop(struct net_device *dev)
468{
469 struct macvlan_dev *vlan = netdev_priv(dev);
470 struct net_device *lowerdev = vlan->lowerdev;
471
472 if (vlan->fwd_priv) {
473 get_ndo_ext(lowerdev->netdev_ops, ndo_dfwd_del_station)(lowerdev,
474 vlan->fwd_priv);
475 vlan->fwd_priv = NULL;
476 return 0;
477 }
478
479 dev_uc_unsync(lowerdev, dev);
480 dev_mc_unsync(lowerdev, dev);
481
482 if (vlan->port->passthru) {
483 if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC))
484 dev_set_promiscuity(lowerdev, -1);
485 goto hash_del;
486 }
487
488 if (dev->flags & IFF_ALLMULTI)
489 dev_set_allmulti(lowerdev, -1);
490
491 dev_uc_del(lowerdev, dev->dev_addr);
492
493hash_del:
494 macvlan_hash_del(vlan, !dev->dismantle);
495 return 0;
496}
497
498static int macvlan_set_mac_address(struct net_device *dev, void *p)
499{
500 struct macvlan_dev *vlan = netdev_priv(dev);
501 struct net_device *lowerdev = vlan->lowerdev;
502 struct sockaddr *addr = p;
503 int err;
504
505 if (!is_valid_ether_addr(addr->sa_data))
506 return -EADDRNOTAVAIL;
507
508 if (!(dev->flags & IFF_UP)) {
509
510 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
511 } else {
512
513 if (macvlan_addr_busy(vlan->port, addr->sa_data))
514 return -EBUSY;
515
516 err = dev_uc_add(lowerdev, addr->sa_data);
517 if (err)
518 return err;
519
520 dev_uc_del(lowerdev, dev->dev_addr);
521
522 macvlan_hash_change_addr(vlan, addr->sa_data);
523 }
524 return 0;
525}
526
527static void macvlan_change_rx_flags(struct net_device *dev, int change)
528{
529 struct macvlan_dev *vlan = netdev_priv(dev);
530 struct net_device *lowerdev = vlan->lowerdev;
531
532 if (change & IFF_ALLMULTI)
533 dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
534}
535
536static void macvlan_set_mac_lists(struct net_device *dev)
537{
538 struct macvlan_dev *vlan = netdev_priv(dev);
539
540 if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
541 bitmap_fill(vlan->mc_filter, MACVLAN_MC_FILTER_SZ);
542 } else {
543 struct netdev_hw_addr *ha;
544 DECLARE_BITMAP(filter, MACVLAN_MC_FILTER_SZ);
545
546 bitmap_zero(filter, MACVLAN_MC_FILTER_SZ);
547 netdev_for_each_mc_addr(ha, dev) {
548 __set_bit(mc_hash(vlan, ha->addr), filter);
549 }
550
551 __set_bit(mc_hash(vlan, dev->broadcast), filter);
552
553 bitmap_copy(vlan->mc_filter, filter, MACVLAN_MC_FILTER_SZ);
554 }
555 dev_uc_sync(vlan->lowerdev, dev);
556 dev_mc_sync(vlan->lowerdev, dev);
557}
558
559static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
560{
561 struct macvlan_dev *vlan = netdev_priv(dev);
562
563 if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
564 return -EINVAL;
565 dev->mtu = new_mtu;
566 return 0;
567}
568
569
570
571
572
573
574static struct lock_class_key macvlan_netdev_xmit_lock_key;
575static struct lock_class_key macvlan_netdev_addr_lock_key;
576
577#define ALWAYS_ON_OFFLOADS \
578 (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE)
579
580#define ALWAYS_ON_FEATURES (ALWAYS_ON_OFFLOADS | NETIF_F_LLTX)
581
582#define MACVLAN_FEATURES \
583 (NETIF_F_SG | NETIF_F_CSUM_MASK | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
584 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
585 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
586 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
587
588#define MACVLAN_STATE_MASK \
589 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
590
591static void macvlan_set_lockdep_class_one(struct net_device *dev,
592 struct netdev_queue *txq,
593 void *_unused)
594{
595 lockdep_set_class(&txq->_xmit_lock,
596 &macvlan_netdev_xmit_lock_key);
597}
598
599static void macvlan_set_lockdep_class(struct net_device *dev)
600{
601 lockdep_set_class(&dev->addr_list_lock,
602 &macvlan_netdev_addr_lock_key);
603 netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL);
604}
605
606static int macvlan_init(struct net_device *dev)
607{
608 struct macvlan_dev *vlan = netdev_priv(dev);
609 const struct net_device *lowerdev = vlan->lowerdev;
610
611 dev->state = (dev->state & ~MACVLAN_STATE_MASK) |
612 (lowerdev->state & MACVLAN_STATE_MASK);
613 dev->features = lowerdev->features & MACVLAN_FEATURES;
614 dev->features |= ALWAYS_ON_FEATURES;
615 dev->vlan_features = lowerdev->vlan_features & MACVLAN_FEATURES;
616 dev->vlan_features |= ALWAYS_ON_OFFLOADS;
617 dev->gso_max_size = lowerdev->gso_max_size;
618 dev->hard_header_len = lowerdev->hard_header_len;
619
620 macvlan_set_lockdep_class(dev);
621
622 vlan->pcpu_stats = netdev_alloc_pcpu_stats(struct macvlan_pcpu_stats);
623 if (!vlan->pcpu_stats)
624 return -ENOMEM;
625
626 return 0;
627}
628
629static void macvlan_uninit(struct net_device *dev)
630{
631 struct macvlan_dev *vlan = netdev_priv(dev);
632 struct macvlan_port *port = vlan->port;
633
634 free_percpu(vlan->pcpu_stats);
635
636 port->count -= 1;
637 if (!port->count)
638 macvlan_port_destroy(port->dev);
639}
640
641static void macvlan_dev_get_stats64(struct net_device *dev,
642 struct rtnl_link_stats64 *stats)
643{
644 struct macvlan_dev *vlan = netdev_priv(dev);
645
646 if (vlan->pcpu_stats) {
647 struct macvlan_pcpu_stats *p;
648 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
649 u32 rx_errors = 0, tx_dropped = 0;
650 unsigned int start;
651 int i;
652
653 for_each_possible_cpu(i) {
654 p = per_cpu_ptr(vlan->pcpu_stats, i);
655 do {
656 start = u64_stats_fetch_begin_irq(&p->syncp);
657 rx_packets = p->rx_packets;
658 rx_bytes = p->rx_bytes;
659 rx_multicast = p->rx_multicast;
660 tx_packets = p->tx_packets;
661 tx_bytes = p->tx_bytes;
662 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
663
664 stats->rx_packets += rx_packets;
665 stats->rx_bytes += rx_bytes;
666 stats->multicast += rx_multicast;
667 stats->tx_packets += tx_packets;
668 stats->tx_bytes += tx_bytes;
669
670
671
672 rx_errors += p->rx_errors;
673 tx_dropped += p->tx_dropped;
674 }
675 stats->rx_errors = rx_errors;
676 stats->rx_dropped = rx_errors;
677 stats->tx_dropped = tx_dropped;
678 }
679}
680
681static int macvlan_vlan_rx_add_vid(struct net_device *dev,
682 __be16 proto, u16 vid)
683{
684 struct macvlan_dev *vlan = netdev_priv(dev);
685 struct net_device *lowerdev = vlan->lowerdev;
686
687 return vlan_vid_add(lowerdev, proto, vid);
688}
689
690static int macvlan_vlan_rx_kill_vid(struct net_device *dev,
691 __be16 proto, u16 vid)
692{
693 struct macvlan_dev *vlan = netdev_priv(dev);
694 struct net_device *lowerdev = vlan->lowerdev;
695
696 vlan_vid_del(lowerdev, proto, vid);
697 return 0;
698}
699
700static int macvlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
701 struct net_device *dev,
702 const unsigned char *addr, u16 vid,
703 u16 flags)
704{
705 struct macvlan_dev *vlan = netdev_priv(dev);
706 int err = -EINVAL;
707
708
709
710
711 if (!vlan->port->passthru && is_unicast_ether_addr(addr))
712 return -EOPNOTSUPP;
713
714 if (is_unicast_ether_addr(addr))
715 err = dev_uc_add_excl(dev, addr);
716 else if (is_multicast_ether_addr(addr))
717 err = dev_mc_add_excl(dev, addr);
718
719 return err;
720}
721
722static int macvlan_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
723 struct net_device *dev,
724 const unsigned char *addr, u16 vid)
725{
726 struct macvlan_dev *vlan = netdev_priv(dev);
727 int err = -EINVAL;
728
729
730
731
732 if (!vlan->port->passthru && is_unicast_ether_addr(addr))
733 return -EOPNOTSUPP;
734
735 if (is_unicast_ether_addr(addr))
736 err = dev_uc_del(dev, addr);
737 else if (is_multicast_ether_addr(addr))
738 err = dev_mc_del(dev, addr);
739
740 return err;
741}
742
743static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
744 struct ethtool_drvinfo *drvinfo)
745{
746 strlcpy(drvinfo->driver, "macvlan", sizeof(drvinfo->driver));
747 strlcpy(drvinfo->version, "0.1", sizeof(drvinfo->version));
748}
749
750static int macvlan_ethtool_get_link_ksettings(struct net_device *dev,
751 struct ethtool_link_ksettings *cmd)
752{
753 const struct macvlan_dev *vlan = netdev_priv(dev);
754
755 return __ethtool_get_link_ksettings(vlan->lowerdev, cmd);
756}
757
758static netdev_features_t macvlan_fix_features(struct net_device *dev,
759 netdev_features_t features)
760{
761 struct macvlan_dev *vlan = netdev_priv(dev);
762 netdev_features_t mask;
763
764 features |= NETIF_F_ALL_FOR_ALL;
765 features &= (vlan->set_features | ~MACVLAN_FEATURES);
766 mask = features;
767
768 features = netdev_increment_features(vlan->lowerdev->features,
769 features,
770 mask);
771 features |= ALWAYS_ON_FEATURES;
772 features &= ~NETIF_F_NETNS_LOCAL;
773
774 return features;
775}
776
777static int macvlan_dev_get_iflink(const struct net_device *dev)
778{
779 struct macvlan_dev *vlan = netdev_priv(dev);
780
781 return vlan->lowerdev->ifindex;
782}
783
784static const struct ethtool_ops macvlan_ethtool_ops = {
785 .get_link = ethtool_op_get_link,
786 .get_link_ksettings = macvlan_ethtool_get_link_ksettings,
787 .get_drvinfo = macvlan_ethtool_get_drvinfo,
788};
789
790static const struct net_device_ops macvlan_netdev_ops = {
791 .ndo_size = sizeof(struct net_device_ops),
792 .ndo_init = macvlan_init,
793 .ndo_uninit = macvlan_uninit,
794 .ndo_open = macvlan_open,
795 .ndo_stop = macvlan_stop,
796 .ndo_start_xmit = macvlan_start_xmit,
797 .ndo_change_mtu_rh74 = macvlan_change_mtu,
798 .ndo_fix_features = macvlan_fix_features,
799 .ndo_change_rx_flags = macvlan_change_rx_flags,
800 .ndo_set_mac_address = macvlan_set_mac_address,
801 .ndo_set_rx_mode = macvlan_set_mac_lists,
802 .ndo_get_stats64 = macvlan_dev_get_stats64,
803 .ndo_validate_addr = eth_validate_addr,
804 .ndo_vlan_rx_add_vid = macvlan_vlan_rx_add_vid,
805 .ndo_vlan_rx_kill_vid = macvlan_vlan_rx_kill_vid,
806 .ndo_fdb_add = macvlan_fdb_add,
807 .ndo_fdb_del = macvlan_fdb_del,
808 .extended.ndo_fdb_dump = ndo_dflt_fdb_dump,
809 .ndo_get_iflink = macvlan_dev_get_iflink,
810};
811
812void macvlan_common_setup(struct net_device *dev)
813{
814 ether_setup(dev);
815
816 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
817 netif_keep_dst(dev);
818 dev->priv_flags |= IFF_UNICAST_FLT;
819 dev->netdev_ops = &macvlan_netdev_ops;
820 dev->extended->needs_free_netdev = true;
821 dev->header_ops = &macvlan_hard_header_ops;
822 dev->ethtool_ops = &macvlan_ethtool_ops;
823}
824EXPORT_SYMBOL_GPL(macvlan_common_setup);
825
826static void macvlan_setup(struct net_device *dev)
827{
828 macvlan_common_setup(dev);
829 dev->priv_flags |= IFF_NO_QUEUE;
830}
831
832static int macvlan_port_create(struct net_device *dev)
833{
834 struct macvlan_port *port;
835 unsigned int i;
836 int err;
837
838 if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
839 return -EINVAL;
840
841 port = kzalloc(sizeof(*port), GFP_KERNEL);
842 if (port == NULL)
843 return -ENOMEM;
844
845 port->passthru = false;
846 port->dev = dev;
847 INIT_LIST_HEAD(&port->vlans);
848 for (i = 0; i < MACVLAN_HASH_SIZE; i++)
849 INIT_HLIST_HEAD(&port->vlan_hash[i]);
850
851 skb_queue_head_init(&port->bc_queue);
852 INIT_WORK(&port->bc_work, macvlan_process_broadcast);
853
854 err = netdev_rx_handler_register(dev, macvlan_handle_frame, port);
855 if (err)
856 kfree(port);
857 else
858 dev->priv_flags |= IFF_MACVLAN_PORT;
859 return err;
860}
861
862static void macvlan_port_destroy(struct net_device *dev)
863{
864 struct macvlan_port *port = macvlan_port_get_rtnl(dev);
865
866 dev->priv_flags &= ~IFF_MACVLAN_PORT;
867 netdev_rx_handler_unregister(dev);
868
869
870
871
872 cancel_work_sync(&port->bc_work);
873 __skb_queue_purge(&port->bc_queue);
874
875 kfree_rcu(port, rcu);
876}
877
878static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
879{
880 if (tb[IFLA_ADDRESS]) {
881 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
882 return -EINVAL;
883 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
884 return -EADDRNOTAVAIL;
885 }
886
887 if (data && data[IFLA_MACVLAN_FLAGS] &&
888 nla_get_u16(data[IFLA_MACVLAN_FLAGS]) & ~MACVLAN_FLAG_NOPROMISC)
889 return -EINVAL;
890
891 if (data && data[IFLA_MACVLAN_MODE]) {
892 switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) {
893 case MACVLAN_MODE_PRIVATE:
894 case MACVLAN_MODE_VEPA:
895 case MACVLAN_MODE_BRIDGE:
896 case MACVLAN_MODE_PASSTHRU:
897 break;
898 default:
899 return -EINVAL;
900 }
901 }
902 return 0;
903}
904
905int macvlan_common_newlink(struct net *src_net, struct net_device *dev,
906 struct nlattr *tb[], struct nlattr *data[])
907{
908 struct macvlan_dev *vlan = netdev_priv(dev);
909 struct macvlan_port *port;
910 struct net_device *lowerdev;
911 int err;
912
913 if (!tb[IFLA_LINK])
914 return -EINVAL;
915
916 lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
917 if (lowerdev == NULL)
918 return -ENODEV;
919
920
921
922
923 if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) {
924 struct macvlan_dev *lowervlan = netdev_priv(lowerdev);
925 lowerdev = lowervlan->lowerdev;
926 }
927
928 if (!tb[IFLA_MTU])
929 dev->mtu = lowerdev->mtu;
930 else if (dev->mtu > lowerdev->mtu)
931 return -EINVAL;
932
933 if (!tb[IFLA_ADDRESS])
934 eth_hw_addr_random(dev);
935
936 if (!macvlan_port_exists(lowerdev)) {
937 err = macvlan_port_create(lowerdev);
938 if (err < 0)
939 return err;
940 }
941 port = macvlan_port_get_rtnl(lowerdev);
942
943
944 if (port->passthru)
945 return -EINVAL;
946
947 vlan->lowerdev = lowerdev;
948 vlan->dev = dev;
949 vlan->port = port;
950 vlan->set_features = MACVLAN_FEATURES;
951
952 vlan->mode = MACVLAN_MODE_VEPA;
953 if (data && data[IFLA_MACVLAN_MODE])
954 vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
955
956 if (data && data[IFLA_MACVLAN_FLAGS])
957 vlan->flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
958
959 if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
960 if (port->count)
961 return -EINVAL;
962 port->passthru = true;
963 memcpy(dev->dev_addr, lowerdev->dev_addr, ETH_ALEN);
964 }
965
966 port->count += 1;
967 err = register_netdevice(dev);
968 if (err < 0)
969 goto destroy_port;
970
971 err = netdev_upper_dev_link(lowerdev, dev);
972 if (err)
973 goto unregister_netdev;
974
975 dev->priv_flags |= IFF_MACVLAN;
976 list_add_tail_rcu(&vlan->list, &port->vlans);
977 netif_stacked_transfer_operstate(lowerdev, dev);
978
979 return 0;
980
981unregister_netdev:
982 unregister_netdevice(dev);
983destroy_port:
984 port->count -= 1;
985 if (!port->count)
986 macvlan_port_destroy(lowerdev);
987
988 return err;
989}
990EXPORT_SYMBOL_GPL(macvlan_common_newlink);
991
992static int macvlan_newlink(struct net *src_net, struct net_device *dev,
993 struct nlattr *tb[], struct nlattr *data[])
994{
995 return macvlan_common_newlink(src_net, dev, tb, data);
996}
997
998void macvlan_dellink(struct net_device *dev, struct list_head *head)
999{
1000 struct macvlan_dev *vlan = netdev_priv(dev);
1001
1002 list_del_rcu(&vlan->list);
1003 unregister_netdevice_queue(dev, head);
1004 netdev_upper_dev_unlink(vlan->lowerdev, dev);
1005}
1006EXPORT_SYMBOL_GPL(macvlan_dellink);
1007
1008static int macvlan_changelink(struct net_device *dev,
1009 struct nlattr *tb[], struct nlattr *data[])
1010{
1011 struct macvlan_dev *vlan = netdev_priv(dev);
1012
1013 if (data && data[IFLA_MACVLAN_FLAGS]) {
1014 __u16 flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
1015 bool promisc = (flags ^ vlan->flags) & MACVLAN_FLAG_NOPROMISC;
1016 if (vlan->port->passthru && promisc) {
1017 int err;
1018
1019 if (flags & MACVLAN_FLAG_NOPROMISC)
1020 err = dev_set_promiscuity(vlan->lowerdev, -1);
1021 else
1022 err = dev_set_promiscuity(vlan->lowerdev, 1);
1023 if (err < 0)
1024 return err;
1025 }
1026 vlan->flags = flags;
1027 }
1028 if (data && data[IFLA_MACVLAN_MODE])
1029 vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
1030 return 0;
1031}
1032
1033static size_t macvlan_get_size(const struct net_device *dev)
1034{
1035 return (0
1036 + nla_total_size(4)
1037 + nla_total_size(2)
1038 );
1039}
1040
1041static int macvlan_fill_info(struct sk_buff *skb,
1042 const struct net_device *dev)
1043{
1044 struct macvlan_dev *vlan = netdev_priv(dev);
1045
1046 if (nla_put_u32(skb, IFLA_MACVLAN_MODE, vlan->mode))
1047 goto nla_put_failure;
1048 if (nla_put_u16(skb, IFLA_MACVLAN_FLAGS, vlan->flags))
1049 goto nla_put_failure;
1050 return 0;
1051
1052nla_put_failure:
1053 return -EMSGSIZE;
1054}
1055
1056static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = {
1057 [IFLA_MACVLAN_MODE] = { .type = NLA_U32 },
1058 [IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 },
1059};
1060
1061int macvlan_link_register(struct rtnl_link_ops *ops)
1062{
1063
1064 ops->priv_size = sizeof(struct macvlan_dev);
1065 ops->validate = macvlan_validate;
1066 ops->maxtype = IFLA_MACVLAN_MAX;
1067 ops->policy = macvlan_policy;
1068 ops->changelink = macvlan_changelink;
1069 ops->get_size = macvlan_get_size;
1070 ops->fill_info = macvlan_fill_info;
1071
1072 return rtnl_link_register(ops);
1073};
1074EXPORT_SYMBOL_GPL(macvlan_link_register);
1075
1076static struct net *macvlan_get_link_net(const struct net_device *dev)
1077{
1078 return dev_net(macvlan_dev_real_dev(dev));
1079}
1080
1081static struct rtnl_link_ops macvlan_link_ops = {
1082 .kind = "macvlan",
1083 .setup = macvlan_setup,
1084 .newlink = macvlan_newlink,
1085 .dellink = macvlan_dellink,
1086 .get_link_net = macvlan_get_link_net,
1087};
1088
1089static int macvlan_device_event(struct notifier_block *unused,
1090 unsigned long event, void *ptr)
1091{
1092 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1093 struct macvlan_dev *vlan, *next;
1094 struct macvlan_port *port;
1095 LIST_HEAD(list_kill);
1096
1097 if (!macvlan_port_exists(dev))
1098 return NOTIFY_DONE;
1099
1100 port = macvlan_port_get_rtnl(dev);
1101
1102 switch (event) {
1103 case NETDEV_CHANGE:
1104 list_for_each_entry(vlan, &port->vlans, list)
1105 netif_stacked_transfer_operstate(vlan->lowerdev,
1106 vlan->dev);
1107 break;
1108 case NETDEV_FEAT_CHANGE:
1109 list_for_each_entry(vlan, &port->vlans, list) {
1110 vlan->dev->gso_max_size = dev->gso_max_size;
1111 netdev_update_features(vlan->dev);
1112 }
1113 break;
1114 case NETDEV_UNREGISTER:
1115
1116 if (dev->reg_state != NETREG_UNREGISTERING)
1117 break;
1118
1119 list_for_each_entry_safe(vlan, next, &port->vlans, list)
1120 vlan->dev->rtnl_link_ops->dellink(vlan->dev, &list_kill);
1121 unregister_netdevice_many(&list_kill);
1122 list_del(&list_kill);
1123 break;
1124 case NETDEV_PRE_TYPE_CHANGE:
1125
1126 return NOTIFY_BAD;
1127 }
1128 return NOTIFY_DONE;
1129}
1130
1131static struct notifier_block macvlan_notifier_block __read_mostly = {
1132 .notifier_call = macvlan_device_event,
1133};
1134
1135static int __init macvlan_init_module(void)
1136{
1137 int err;
1138
1139 register_netdevice_notifier_rh(&macvlan_notifier_block);
1140
1141 err = macvlan_link_register(&macvlan_link_ops);
1142 if (err < 0)
1143 goto err1;
1144 return 0;
1145err1:
1146 unregister_netdevice_notifier_rh(&macvlan_notifier_block);
1147 return err;
1148}
1149
1150static void __exit macvlan_cleanup_module(void)
1151{
1152 rtnl_link_unregister(&macvlan_link_ops);
1153 unregister_netdevice_notifier_rh(&macvlan_notifier_block);
1154}
1155
1156module_init(macvlan_init_module);
1157module_exit(macvlan_cleanup_module);
1158
1159MODULE_LICENSE("GPL");
1160MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
1161MODULE_DESCRIPTION("Driver for MAC address based VLANs");
1162MODULE_ALIAS_RTNL_LINK("macvlan");
1163